gocron/models/migration.go

195 lines
4.2 KiB
Go
Raw Normal View History

2017-03-10 09:24:06 +00:00
package models
2017-04-25 09:22:54 +00:00
import (
2017-09-16 09:58:33 +00:00
"errors"
"fmt"
"github.com/go-xorm/xorm"
"github.com/ouqiang/gocron/modules/logger"
"strconv"
2017-04-25 09:22:54 +00:00
)
2017-04-02 02:19:52 +00:00
type Migration struct{}
2017-03-10 09:24:06 +00:00
// 首次安装, 创建数据库表
func (migration *Migration) Install(dbName string) error {
2017-09-16 09:58:33 +00:00
if !isDatabaseExist(dbName) {
return errors.New("数据库不存在")
}
setting := new(Setting)
task := new(Task)
tables := []interface{}{
&User{}, task, &TaskLog{}, &Host{}, setting, &LoginLog{}, &TaskHost{},
}
for _, table := range tables {
exist, err := Db.IsTableExist(table)
if exist {
return errors.New("数据表已存在")
}
if err != nil {
return err
}
err = Db.Sync2(table)
if err != nil {
return err
}
}
setting.InitBasicField()
task.CreateTestTask()
return nil
}
// 判断数据库是否存在
2017-04-02 02:19:52 +00:00
func isDatabaseExist(name string) bool {
2017-09-16 09:58:33 +00:00
_, err := Db.Exec("use ?", name)
2017-09-16 09:58:33 +00:00
return err != nil
2017-04-02 02:19:52 +00:00
}
// 迭代升级数据库, 新建表、新增字段等
2017-09-16 09:58:33 +00:00
func (migration *Migration) Upgrade(oldVersionId int) {
// v1.2版本不支持升级
if oldVersionId == 120 {
return
}
versionIds := []int{110, 122, 130, 140}
2017-09-16 09:58:33 +00:00
upgradeFuncs := []func(*xorm.Session) error{
migration.upgradeFor110,
migration.upgradeFor122,
2017-10-01 13:49:46 +00:00
migration.upgradeFor130,
migration.upgradeFor140,
2017-09-16 09:58:33 +00:00
}
startIndex := -1
// 从当前版本的下一版本开始升级
for i, value := range versionIds {
if value > oldVersionId {
startIndex = i
break
}
}
if startIndex == -1 {
return
}
length := len(versionIds)
if startIndex >= length {
return
}
session := Db.NewSession()
err := session.Begin()
if err != nil {
logger.Fatalf("开启事务失败-%s", err.Error())
}
for startIndex < length {
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())
}
}
// 升级到v1.1版本
func (migration *Migration) upgradeFor110(session *xorm.Session) error {
2017-09-16 09:58:33 +00:00
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
}
2017-09-07 13:32:53 +00:00
// 升级到1.2.2版本
func (migration *Migration) upgradeFor122(session *xorm.Session) error {
2017-09-16 09:58:33 +00:00
logger.Info("开始升级到v1.2.2")
2017-09-07 13:32:53 +00:00
2017-09-16 09:58:33 +00:00
tableName := TablePrefix + "task"
// task表增加tag字段
_, err := session.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN tag VARCHAR(32) NOT NULL DEFAULT '' ", tableName))
2017-09-07 13:32:53 +00:00
2017-09-16 09:58:33 +00:00
logger.Info("已升级到v1.2.2\n")
2017-09-07 13:32:53 +00:00
2017-09-16 09:58:33 +00:00
return err
}
2017-10-01 13:49:46 +00:00
// 升级到v1.3版本
2017-10-01 13:49:46 +00:00
func (migration *Migration) upgradeFor130(session *xorm.Session) error {
logger.Info("开始升级到v1.3")
tableName := TablePrefix + "user"
// 删除user表deleted字段
_, err := session.Exec(fmt.Sprintf("ALTER TABLE %s DROP COLUMN deleted", tableName))
logger.Info("已升级到v1.3\n")
return err
}
// 升级到v1.4版本
func (migration *Migration) upgradeFor140(session *xorm.Session) error {
logger.Info("开始升级到v1.4")
tableName := TablePrefix + "task"
// task表增加字段
// retry_interval 重试间隔时间(秒)
2018-01-27 10:08:46 +00:00
// http_method http请求方法
sql := fmt.Sprintf(
"ALTER TABLE %s ADD COLUMN retry_interval SMALLINT NOT NULL DEFAULT 0,ADD COLUMN http_method TINYINT NOT NULL DEFAULT 1", tableName)
_, err := session.Exec(sql)
if err != nil {
return err
}
logger.Info("已升级到v1.4\n")
2017-10-01 13:49:46 +00:00
return err
}