gocron/models/migration.go

32 lines
577 B
Go
Raw Normal View History

2017-03-10 09:24:06 +00:00
package models
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("数据库不存在")
}
tables := []interface{}{
&User{}, &Task{}, &TaskLog{}, &Host{},
}
for _, table := range tables {
err := Db.Sync2(table)
if err != nil {
return err
}
}
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
}