Use transaction in V102 migration (#12395)

The code for dropTableColumns has a slightly confusing portion whereby the session is committed for MSSQL but not for other variants.

The v102 migration doesn't actually start a transaction so this weirdness does not affect it. However it probably should attempt to run this in a transaction.

Signed-off-by: Andrew Thornton art27@cantab.net
release/v1.15
zeripath 2020-08-06 19:16:49 +01:00 committed by GitHub
parent 8cd7e93b9a
commit e17e3f71f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 5 deletions

View File

@ -453,20 +453,16 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
tableName, strings.Replace(cols, "`", "'", -1)) tableName, strings.Replace(cols, "`", "'", -1))
constraints := make([]string, 0) constraints := make([]string, 0)
if err := sess.SQL(sql).Find(&constraints); err != nil { if err := sess.SQL(sql).Find(&constraints); err != nil {
sess.Rollback()
return fmt.Errorf("Find constraints: %v", err) return fmt.Errorf("Find constraints: %v", err)
} }
for _, constraint := range constraints { for _, constraint := range constraints {
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT `%s`", tableName, constraint)); err != nil { 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) 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 { 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 fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
} }
return sess.Commit()
default: default:
log.Fatal("Unrecognized DB") log.Fatal("Unrecognized DB")
} }

View File

@ -11,5 +11,11 @@ import (
func dropColumnHeadUserNameOnPullRequest(x *xorm.Engine) error { func dropColumnHeadUserNameOnPullRequest(x *xorm.Engine) error {
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() 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()
} }