Skip to content

Commit 8d92ba4

Browse files
committed
Add transaction as MSSQL alter table is transactional
1 parent bce10b0 commit 8d92ba4

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

models/migrations/migrations.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ func dropTableColumns(x *xorm.Engine, tableName string, columnNames ...string) (
239239
case setting.UseMSSQL:
240240
sess := x.NewSession()
241241
defer sess.Close()
242+
243+
if err = sess.Begin(); err != nil {
244+
return err
245+
}
246+
242247
cols := ""
243248
for _, col := range columnNames {
244249
if cols != "" {
@@ -250,16 +255,21 @@ func dropTableColumns(x *xorm.Engine, tableName string, columnNames ...string) (
250255
tableName, strings.Replace(cols, "`", "'", -1))
251256
constraints := make([]string, 0)
252257
if err := sess.SQL(sql).Find(&constraints); err != nil {
258+
sess.Rollback()
253259
return fmt.Errorf("Find constraints: %v", err)
254260
}
255261
for _, constraint := range constraints {
256262
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT `%s`", tableName, constraint)); err != nil {
263+
sess.Rollback()
257264
return fmt.Errorf("Drop table `%s` constraint `%s`: %v", tableName, constraint, err)
258265
}
259266
}
260267
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP COLUMN %s", tableName, cols)); err != nil {
268+
sess.Rollback()
261269
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
262270
}
271+
272+
return sess.Commit()
263273
default:
264274
log.Fatal(4, "Unrecognized DB")
265275
}

0 commit comments

Comments
 (0)