Skip to content

Commit 1343067

Browse files
committed
Fix mssql modify column failure
1 parent b400816 commit 1343067

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

models/migrations/v165.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,48 @@
55
package migrations
66

77
import (
8+
"context"
9+
10+
"code.gitea.io/gitea/modules/log"
811
"xorm.io/xorm"
912
"xorm.io/xorm/schemas"
1013
)
1114

15+
func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error {
16+
var indexes map[string]*schemas.Index
17+
var err error
18+
// MSSQL have to remove index at first, otherwise alter column will fail
19+
// ref. https://sqlzealots.com/2018/05/09/error-message-the-index-is-dependent-on-column-alter-table-alter-column-failed-because-one-or-more-objects-access-this-column/
20+
if x.Dialect().URI().DBType == schemas.MSSQL {
21+
indexes, err = x.Dialect().GetIndexes(x.DB(), context.Background(), tableName)
22+
if err != nil {
23+
return err
24+
}
25+
26+
for _, index := range indexes {
27+
_, err = x.Exec(x.Dialect().DropIndexSQL(tableName, index))
28+
if err != nil {
29+
return err
30+
}
31+
}
32+
}
33+
34+
defer func() {
35+
for _, index := range indexes {
36+
_, err = x.Exec(x.Dialect().CreateIndexSQL(tableName, index))
37+
if err != nil {
38+
log.Error("Create index %s on table %s failed: %v", index.Name, tableName, err)
39+
}
40+
}
41+
}()
42+
43+
alterSQL := x.Dialect().ModifyColumnSQL(tableName, col)
44+
if _, err := x.Exec(alterSQL); err != nil {
45+
return err
46+
}
47+
return nil
48+
}
49+
1250
func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
1351
dbType := x.Dialect().URI().DBType
1452
if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT
@@ -18,16 +56,15 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
1856
type HookTask struct {
1957
Typ string `xorm:"VARCHAR(16) index"`
2058
}
21-
alterSQL := x.Dialect().ModifyColumnSQL("hook_task", &schemas.Column{
22-
Name: "typ",
23-
TableName: "hook_task",
59+
60+
if err := modifyColumn(x, "hook_task", &schemas.Column{
61+
Name: "typ",
2462
SQLType: schemas.SQLType{
2563
Name: "VARCHAR",
2664
},
2765
Length: 16,
2866
Nullable: true, // To keep compatible as nullable
29-
})
30-
if _, err := x.Exec(alterSQL); err != nil {
67+
}); err != nil {
3168
return err
3269
}
3370

@@ -45,16 +82,14 @@ func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
4582
Type string `xorm:"VARCHAR(16) index"`
4683
}
4784

48-
alterSQL = x.Dialect().ModifyColumnSQL("webhook", &schemas.Column{
49-
Name: "type",
50-
TableName: "webhook",
85+
if err := modifyColumn(x, "webhook", &schemas.Column{
86+
Name: "type",
5187
SQLType: schemas.SQLType{
5288
Name: "VARCHAR",
5389
},
5490
Length: 16,
5591
Nullable: true, // To keep compatible as nullable
56-
})
57-
if _, err := x.Exec(alterSQL); err != nil {
92+
}); err != nil {
5893
return err
5994
}
6095

0 commit comments

Comments
 (0)