func init() { MMs = make(map[string]*MM) orm.RegisterDataBase("default", "sqlite3", "data.db") o := orm.NewOrm() dr := o.Driver() fmt.Println(dr.Name() == "default") fmt.Println(dr.Type() == orm.DR_Sqlite) }
// execute the sql already add in the sql func (m *Migration) Exec(name, status string) error { o := orm.NewOrm() for _, s := range m.sqls { beego.Info("exec sql:", s) r := o.Raw(s) _, err := r.Exec() if err != nil { return err } } return m.addOrUpdateRecord(name, status) }
func isRollBack(name string) bool { o := orm.NewOrm() var maps []orm.Params num, err := o.Raw("select * from migrations where `name` = ? order by id_migration desc", name).Values(&maps) if err != nil { beego.Info("get name has error", err) return false } if num <= 0 { return false } if maps[0]["status"] == "rollback" { return true } return false }
func (m *Migration) addOrUpdateRecord(name, status string) error { o := orm.NewOrm() if status == "down" { status = "rollback" p, err := o.Raw("update migrations set `status` = ?, `rollback_statements` = ?, `created_at` = ? where name = ?").Prepare() if err != nil { return nil } _, err = p.Exec(status, strings.Join(m.sqls, "; "), time.Now().Format(M_DB_DATE_FORMAT), name) return err } else { status = "update" p, err := o.Raw("insert into migrations(`name`, `created_at`, `statements`, `status`) values(?,?,?,?)").Prepare() if err != nil { return err } _, err = p.Exec(name, time.Now().Format(M_DB_DATE_FORMAT), strings.Join(m.sqls, "; "), status) return err } }