1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 16:20:56 +00:00

migration: reset the up state sql

This commit is contained in:
astaxie 2014-08-14 13:37:48 +08:00
parent 14cd9e51ac
commit 57a9670b0a

View File

@ -39,6 +39,7 @@ const (
type Migrationer interface { type Migrationer interface {
Up() Up()
Down() Down()
Reset()
Exec(name, status string) error Exec(name, status string) error
GetCreated() int64 GetCreated() int64
} }
@ -72,6 +73,11 @@ func (m *Migration) Sql(sql string) {
m.sqls = append(m.sqls, sql) m.sqls = append(m.sqls, sql)
} }
// Reset the sqls
func (m *Migration) Reset() {
m.sqls = make([]string, 0)
}
// execute the sql already add in the sql // execute the sql already add in the sql
func (m *Migration) Exec(name, status string) error { func (m *Migration) Exec(name, status string) error {
o := orm.NewOrm() o := orm.NewOrm()
@ -90,11 +96,11 @@ func (m *Migration) addOrUpdateRecord(name, status string) error {
o := orm.NewOrm() o := orm.NewOrm()
if status == "down" { if status == "down" {
status = "rollback" status = "rollback"
p, err := o.Raw("update migrations set status = ?, rollback_statements = ? where name = ?").Prepare() p, err := o.Raw("update migrations set `status` = ?, `rollback_statements` = ?, `created_at` = ? where name = ?").Prepare()
if err != nil { if err != nil {
return nil return nil
} }
_, err = p.Exec(status, strings.Join(m.sqls, "; "), name) _, err = p.Exec(status, strings.Join(m.sqls, "; "), name, time.Now().Format(M_DB_DATE_FORMAT))
return err return err
} else { } else {
status = "update" status = "update"
@ -102,11 +108,7 @@ func (m *Migration) addOrUpdateRecord(name, status string) error {
if err != nil { if err != nil {
return err return err
} }
t, err := time.Parse(M_DATE_FORMAT, m.Created) _, err = p.Exec(name, time.Now().Format(M_DB_DATE_FORMAT), strings.Join(m.sqls, "; "), status)
if err != nil {
return err
}
_, err = p.Exec(name, t.Format(M_DB_DATE_FORMAT), strings.Join(m.sqls, "; "), status)
return err return err
} }
} }
@ -136,6 +138,7 @@ func Upgrade(lasttime int64) error {
for _, v := range sm { for _, v := range sm {
if v.created > lasttime { if v.created > lasttime {
beego.Info("start upgrade", v.name) beego.Info("start upgrade", v.name)
v.m.Reset()
v.m.Up() v.m.Up()
err := v.m.Exec(v.name, "up") err := v.m.Exec(v.name, "up")
if err != nil { if err != nil {
@ -156,6 +159,7 @@ func Upgrade(lasttime int64) error {
func Rollback(name string) error { func Rollback(name string) error {
if v, ok := migrationMap[name]; ok { if v, ok := migrationMap[name]; ok {
beego.Info("start rollback") beego.Info("start rollback")
v.Reset()
v.Down() v.Down()
err := v.Exec(name, "down") err := v.Exec(name, "down")
if err != nil { if err != nil {
@ -184,6 +188,7 @@ func Reset() error {
continue continue
} }
beego.Info("start reset:", v.name) beego.Info("start reset:", v.name)
v.m.Reset()
v.m.Down() v.m.Down()
err := v.m.Exec(v.name, "down") err := v.m.Exec(v.name, "down")
if err != nil { if err != nil {