Skip to content

Commit dc4afef

Browse files
lafriksappleboy
authored andcommitted
Fix column removal in MSSQL (#3638)
* Fix column removal in MSSQL * Use xorm session in MSSQL drop column operations * Add transaction as MSSQL alter table is transactional
1 parent 4009c24 commit dc4afef

File tree

2 files changed

+61
-21
lines changed

2 files changed

+61
-21
lines changed

models/migrations/migrations.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,66 @@ Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to curr
217217
return nil
218218
}
219219

220+
func dropTableColumns(x *xorm.Engine, tableName string, columnNames ...string) (err error) {
221+
if tableName == "" || len(columnNames) == 0 {
222+
return nil
223+
}
224+
225+
switch {
226+
case setting.UseSQLite3:
227+
log.Warn("Unable to drop columns in SQLite")
228+
case setting.UseMySQL, setting.UseTiDB, setting.UsePostgreSQL:
229+
cols := ""
230+
for _, col := range columnNames {
231+
if cols != "" {
232+
cols += ", "
233+
}
234+
cols += "DROP COLUMN `" + col + "`"
235+
}
236+
if _, err := x.Exec(fmt.Sprintf("ALTER TABLE `%s` %s", tableName, columnNames)); err != nil {
237+
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
238+
}
239+
case setting.UseMSSQL:
240+
sess := x.NewSession()
241+
defer sess.Close()
242+
243+
if err = sess.Begin(); err != nil {
244+
return err
245+
}
246+
247+
cols := ""
248+
for _, col := range columnNames {
249+
if cols != "" {
250+
cols += ", "
251+
}
252+
cols += "`" + strings.ToLower(col) + "`"
253+
}
254+
sql := fmt.Sprintf("SELECT Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('%[1]s') AND PARENT_COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
255+
tableName, strings.Replace(cols, "`", "'", -1))
256+
constraints := make([]string, 0)
257+
if err := sess.SQL(sql).Find(&constraints); err != nil {
258+
sess.Rollback()
259+
return fmt.Errorf("Find constraints: %v", err)
260+
}
261+
for _, constraint := range constraints {
262+
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT `%s`", tableName, constraint)); err != nil {
263+
sess.Rollback()
264+
return fmt.Errorf("Drop table `%s` constraint `%s`: %v", tableName, constraint, err)
265+
}
266+
}
267+
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP COLUMN %s", tableName, cols)); err != nil {
268+
sess.Rollback()
269+
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
270+
}
271+
272+
return sess.Commit()
273+
default:
274+
log.Fatal(4, "Unrecognized DB")
275+
}
276+
277+
return nil
278+
}
279+
220280
func fixLocaleFileLoadPanic(_ *xorm.Engine) error {
221281
cfg, err := ini.Load(setting.CustomConf)
222282
if err != nil {

models/migrations/v56.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,9 @@
55
package migrations
66

77
import (
8-
"fmt"
9-
10-
"code.gitea.io/gitea/modules/log"
11-
"code.gitea.io/gitea/modules/setting"
12-
138
"github.com/go-xorm/xorm"
149
)
1510

1611
func removeIsOwnerColumnFromOrgUser(x *xorm.Engine) (err error) {
17-
switch {
18-
case setting.UseSQLite3:
19-
log.Warn("Unable to drop columns in SQLite")
20-
case setting.UseMySQL, setting.UseTiDB, setting.UsePostgreSQL:
21-
if _, err := x.Exec("ALTER TABLE org_user DROP COLUMN is_owner, DROP COLUMN num_teams"); err != nil {
22-
return fmt.Errorf("DROP COLUMN org_user.is_owner, org_user.num_teams: %v", err)
23-
}
24-
case setting.UseMSSQL:
25-
if _, err := x.Exec("ALTER TABLE org_user DROP COLUMN is_owner, num_teams"); err != nil {
26-
return fmt.Errorf("DROP COLUMN org_user.is_owner, org_user.num_teams: %v", err)
27-
}
28-
default:
29-
log.Fatal(4, "Unrecognized DB")
30-
}
31-
32-
return nil
12+
return dropTableColumns(x, "org_user", "is_owner", "num_teams")
3313
}

0 commit comments

Comments
 (0)