You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gocron/models/migration.go

43 lines
844 B

package models
8 years ago
import (
"errors"
)
// 创建数据库表
8 years ago
type Migration struct{}
8 years ago
func (migration *Migration) Exec(dbName string) error {
if !isDatabaseExist(dbName) {
return errors.New("数据库不存在")
}
setting := new(Setting)
tables := []interface{}{
&User{}, &Task{}, &TaskLog{}, &Host{}, setting,&LoginLog{},
}
for _, table := range tables {
8 years ago
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()
return nil
}
// 创建数据库
8 years ago
func isDatabaseExist(name string) bool {
_, err := Db.Exec("use ?", name)
return err != nil
8 years ago
}