fix(migration): fix for sqlstore migration, the execution of the migration and recording of the success of it was not done in same transaction, fixes #5315

This commit is contained in:
Torkel Ödegaard 2016-09-30 07:49:40 +02:00
parent 0ab2113fab
commit 2b8177e3e5

View File

@ -92,42 +92,48 @@ func (mg *Migrator) Start() error {
mg.Logger.Debug("Executing", "sql", sql) mg.Logger.Debug("Executing", "sql", sql)
if err := mg.exec(m); err != nil { err := mg.inTransaction(func(sess *xorm.Session) error {
mg.Logger.Error("Exec failed", "error", err, "sql", sql)
record.Error = err.Error() if err := mg.exec(m, sess); err != nil {
mg.x.Insert(&record) mg.Logger.Error("Exec failed", "error", err, "sql", sql)
record.Error = err.Error()
sess.Insert(&record)
return err
} else {
record.Success = true
sess.Insert(&record)
}
return nil
})
if err != nil {
return err return err
} else {
record.Success = true
mg.x.Insert(&record)
} }
} }
return nil return nil
} }
func (mg *Migrator) exec(m Migration) error { func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
mg.Logger.Info("Executing migration", "id", m.Id()) mg.Logger.Info("Executing migration", "id", m.Id())
err := mg.inTransaction(func(sess *xorm.Session) error { condition := m.GetCondition()
if condition != nil {
condition := m.GetCondition() sql, args := condition.Sql(mg.dialect)
if condition != nil { results, err := sess.Query(sql, args...)
sql, args := condition.Sql(mg.dialect) if err != nil || len(results) == 0 {
results, err := sess.Query(sql, args...) mg.Logger.Info("Skipping migration condition not fulfilled", "id", m.Id())
if err != nil || len(results) == 0 { return sess.Rollback()
mg.Logger.Info("Skipping migration condition not fulfilled", "id", m.Id())
return sess.Rollback()
}
} }
}
_, err := sess.Exec(m.Sql(mg.dialect)) _, err := sess.Exec(m.Sql(mg.dialect))
if err != nil { if err != nil {
mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err) mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err)
return err return err
} }
return nil return nil
})
if err != nil { if err != nil {
return err return err