2017-03-10 09:24:06 +00:00
|
|
|
package models
|
|
|
|
|
2017-04-25 09:22:54 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2017-09-05 06:11:18 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/ouqiang/gocron/modules/logger"
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
"strconv"
|
2017-04-25 09:22:54 +00:00
|
|
|
)
|
2017-04-01 12:28:30 +00:00
|
|
|
|
2017-03-10 09:24:06 +00:00
|
|
|
|
2017-04-02 02:19:52 +00:00
|
|
|
type Migration struct{}
|
2017-03-10 09:24:06 +00:00
|
|
|
|
2017-09-03 13:30:03 +00:00
|
|
|
// 首次安装, 创建数据库表
|
|
|
|
func (migration *Migration) Install(dbName string) error {
|
2017-04-02 02:38:49 +00:00
|
|
|
if !isDatabaseExist(dbName) {
|
|
|
|
return errors.New("数据库不存在")
|
|
|
|
}
|
2017-04-28 03:54:46 +00:00
|
|
|
setting := new(Setting)
|
2017-05-12 01:59:22 +00:00
|
|
|
task := new(Task)
|
2017-04-02 02:38:49 +00:00
|
|
|
tables := []interface{}{
|
2017-08-06 14:49:24 +00:00
|
|
|
&User{}, task, &TaskLog{}, &Host{}, setting,&LoginLog{},&TaskHost{},
|
2017-04-02 02:38:49 +00:00
|
|
|
}
|
|
|
|
for _, table := range tables {
|
2017-04-25 09:22:54 +00:00
|
|
|
exist, err:= Db.IsTableExist(table)
|
|
|
|
if exist {
|
|
|
|
return errors.New("数据表已存在")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = Db.Sync2(table)
|
2017-04-02 02:38:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-04-30 17:12:07 +00:00
|
|
|
setting.InitBasicField()
|
2017-05-12 01:59:22 +00:00
|
|
|
task.CreateTestTask()
|
2017-03-10 09:24:06 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
return nil
|
2017-04-01 12:28:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 13:30:03 +00:00
|
|
|
// 判断数据库是否存在
|
2017-04-02 02:19:52 +00:00
|
|
|
func isDatabaseExist(name string) bool {
|
2017-04-02 02:38:49 +00:00
|
|
|
_, err := Db.Exec("use ?", name)
|
2017-04-01 12:28:30 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
return err != nil
|
2017-04-02 02:19:52 +00:00
|
|
|
}
|
2017-09-03 13:30:03 +00:00
|
|
|
|
|
|
|
// 迭代升级数据库, 新建表、新增字段等
|
|
|
|
func (migration *Migration) Upgrade(oldVersionId int) {
|
2017-09-07 01:17:45 +00:00
|
|
|
versionIds := []int{110}
|
2017-09-05 06:11:18 +00:00
|
|
|
upgradeFuncs := []func(*xorm.Session) error {
|
|
|
|
migration.upgradeFor110,
|
|
|
|
}
|
2017-09-03 13:30:03 +00:00
|
|
|
|
2017-09-05 06:11:18 +00:00
|
|
|
// 默认当前版本为v1.0
|
2017-09-03 13:30:03 +00:00
|
|
|
startIndex := 0
|
2017-09-05 06:11:18 +00:00
|
|
|
// 从当前版本的下一版本开始升级
|
2017-09-03 13:30:03 +00:00
|
|
|
for i, value := range versionIds {
|
|
|
|
if oldVersionId == value {
|
|
|
|
startIndex = i + 1
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
length := len(versionIds)
|
2017-09-05 06:11:18 +00:00
|
|
|
if startIndex >= length {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session := Db.NewSession()
|
|
|
|
err := session.Begin()
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("开启事务失败-%s", err.Error())
|
|
|
|
}
|
2017-09-03 13:30:03 +00:00
|
|
|
for startIndex < length {
|
2017-09-05 06:11:18 +00:00
|
|
|
err = upgradeFuncs[startIndex](session)
|
|
|
|
if err == nil {
|
|
|
|
startIndex++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dbErr := session.Rollback()
|
|
|
|
if dbErr != nil {
|
|
|
|
logger.Fatalf("事务回滚失败-%s",dbErr.Error())
|
|
|
|
}
|
|
|
|
logger.Fatal(err)
|
|
|
|
}
|
|
|
|
err = session.Commit()
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("提交事务失败-%s", err.Error())
|
2017-09-03 13:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-05 06:11:18 +00:00
|
|
|
|
|
|
|
// 升级到v1.1版本
|
|
|
|
func (migration *Migration) upgradeFor110(session *xorm.Session) error {
|
|
|
|
logger.Info("开始升级到v1.1")
|
|
|
|
// 创建表task_host
|
|
|
|
err := session.Sync2(new(TaskHost))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tableName := TablePrefix + "task"
|
|
|
|
// 把task对应的host_id写入task_host表
|
|
|
|
sql := fmt.Sprintf("SELECT id, host_id FROM %s WHERE host_id > 0", tableName)
|
|
|
|
results, err := session.Query(sql)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range results {
|
|
|
|
taskHostModel := &TaskHost{}
|
|
|
|
taskId, err := strconv.Atoi(string(value["id"]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hostId, err := strconv.Atoi(string(value["host_id"]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
taskHostModel.TaskId = taskId
|
|
|
|
taskHostModel.HostId = int16(hostId)
|
|
|
|
_, err = session.Insert(taskHostModel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除task表host_id字段
|
|
|
|
_, err = session.Exec(fmt.Sprintf("ALTER TABLE %s DROP COLUMN host_id", tableName))
|
|
|
|
|
|
|
|
logger.Info("已升级到v1.1\n")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|