Skip to content

Use transaction in V102 migration #12395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 6, 2020
4 changes: 0 additions & 4 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,20 +453,16 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
tableName, strings.Replace(cols, "`", "'", -1))
constraints := make([]string, 0)
if err := sess.SQL(sql).Find(&constraints); err != nil {
sess.Rollback()
return fmt.Errorf("Find constraints: %v", err)
}
for _, constraint := range constraints {
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT `%s`", tableName, constraint)); err != nil {
sess.Rollback()
return fmt.Errorf("Drop table `%s` constraint `%s`: %v", tableName, constraint, err)
}
}
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP COLUMN %s", tableName, cols)); err != nil {
sess.Rollback()
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
}
return sess.Commit()
default:
log.Fatal("Unrecognized DB")
}
Expand Down
8 changes: 7 additions & 1 deletion models/migrations/v102.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ import (
func dropColumnHeadUserNameOnPullRequest(x *xorm.Engine) error {
sess := x.NewSession()
defer sess.Close()
return dropTableColumns(sess, "pull_request", "head_user_name")
if err := sess.Begin(); err != nil {
return err
}
if err := dropTableColumns(sess, "pull_request", "head_user_name"); err != nil {
return err
}
return sess.Commit()
}