gocron/models/migration.go

43 lines
844 B
Go
Raw Normal View History

2017-03-10 09:24:06 +00:00
package models
2017-04-25 09:22:54 +00:00
import (
"errors"
)
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-04-02 02:19:52 +00:00
func (migration *Migration) Exec(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-04-02 02:38:49 +00:00
tables := []interface{}{
2017-04-30 22:02:49 +00:00
&User{}, &Task{}, &TaskLog{}, &Host{}, setting,&LoginLog{},
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-03-10 09:24:06 +00:00
2017-04-02 02:38:49 +00:00
return nil
}
// 创建数据库
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-02 02:38:49 +00:00
return err != nil
2017-04-02 02:19:52 +00:00
}