Skip to content

Commit 00e2884

Browse files
committed
Restore compatibility with SQLServer 2008 R2 in migrations
This fixes two problems with MSSQL: * `ALTER TABLE DROP ... IF EXISTS ...` is only supported in SQL Server >16. The `IF EXISTS` here is a belt-and-braces and does not need to be present. Therefore can be dropped. Also stop attempting to drop the indexes as constraints as they're indexes! * System tables like: `sys.indexes` should be lowercase not uppercase because of collation issues. Fix go-gitea#13615 Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 20601f8 commit 00e2884

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

models/migrations/migrations.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
836836
}
837837
cols += "`" + strings.ToLower(col) + "`"
838838
}
839-
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'))",
839+
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'))",
840840
tableName, strings.ReplaceAll(cols, "`", "'"))
841841
constraints := make([]string, 0)
842842
if err := sess.SQL(sql).Find(&constraints); err != nil {
@@ -847,17 +847,14 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
847847
return fmt.Errorf("Drop table `%s` default constraint `%s`: %v", tableName, constraint, err)
848848
}
849849
}
850-
sql = fmt.Sprintf("SELECT DISTINCT Name FROM SYS.INDEXES INNER JOIN SYS.INDEX_COLUMNS ON INDEXES.INDEX_ID = INDEX_COLUMNS.INDEX_ID AND INDEXES.OBJECT_ID = INDEX_COLUMNS.OBJECT_ID WHERE INDEXES.OBJECT_ID = OBJECT_ID('%[1]s') AND INDEX_COLUMNS.COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
850+
sql = fmt.Sprintf("SELECT DISTINCT Name FROM sys.indexes INNER JOIN sys.index_columns ON indexes.index_id = index_columns.index_id AND indexes.object_id = index_columns.object_id WHERE indexes.object_id = OBJECT_ID('%[1]s') AND index_columns.column_id IN (SELECT column_id FROM sys.columns WHERE LOWER(name) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
851851
tableName, strings.ReplaceAll(cols, "`", "'"))
852852
constraints = make([]string, 0)
853853
if err := sess.SQL(sql).Find(&constraints); err != nil {
854854
return fmt.Errorf("Find constraints: %v", err)
855855
}
856856
for _, constraint := range constraints {
857-
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT IF EXISTS `%s`", tableName, constraint)); err != nil {
858-
return fmt.Errorf("Drop table `%s` index constraint `%s`: %v", tableName, constraint, err)
859-
}
860-
if _, err := sess.Exec(fmt.Sprintf("DROP INDEX IF EXISTS `%[2]s` ON `%[1]s`", tableName, constraint)); err != nil {
857+
if _, err := sess.Exec(fmt.Sprintf("DROP INDEX `%[2]s` ON `%[1]s`", tableName, constraint)); err != nil {
861858
return fmt.Errorf("Drop index `%[2]s` on `%[1]s`: %v", tableName, constraint, err)
862859
}
863860
}

0 commit comments

Comments
 (0)