6
6
package migrations
7
7
8
8
import (
9
+ "context"
9
10
"fmt"
10
11
"os"
11
12
"reflect"
@@ -16,6 +17,7 @@ import (
16
17
"code.gitea.io/gitea/modules/setting"
17
18
18
19
"xorm.io/xorm"
20
+ "xorm.io/xorm/schemas"
19
21
)
20
22
21
23
const minDBVersion = 70 // Gitea 1.5.3
@@ -273,6 +275,8 @@ var migrations = []Migration{
273
275
NewMigration ("Convert topic name from 25 to 50" , convertTopicNameFrom25To50 ),
274
276
// v164 -> v165
275
277
NewMigration ("Add scope and nonce columns to oauth2_grant table" , addScopeAndNonceColumnsToOAuth2Grant ),
278
+ // v165 -> v166
279
+ NewMigration ("Convert hook task type from char(16) to varchar(16) and trim the column" , convertHookTaskTypeToVarcharAndTrim ),
276
280
}
277
281
278
282
// GetCurrentDBVersion returns the current db version
@@ -737,3 +741,39 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
737
741
738
742
return nil
739
743
}
744
+
745
+ // modifyColumn will modify column's type or other propertity. SQLITE is not supported
746
+ func modifyColumn (x * xorm.Engine , tableName string , col * schemas.Column ) error {
747
+ var indexes map [string ]* schemas.Index
748
+ var err error
749
+ // MSSQL have to remove index at first, otherwise alter column will fail
750
+ // 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/
751
+ if x .Dialect ().URI ().DBType == schemas .MSSQL {
752
+ indexes , err = x .Dialect ().GetIndexes (x .DB (), context .Background (), tableName )
753
+ if err != nil {
754
+ return err
755
+ }
756
+
757
+ for _ , index := range indexes {
758
+ _ , err = x .Exec (x .Dialect ().DropIndexSQL (tableName , index ))
759
+ if err != nil {
760
+ return err
761
+ }
762
+ }
763
+ }
764
+
765
+ defer func () {
766
+ for _ , index := range indexes {
767
+ _ , err = x .Exec (x .Dialect ().CreateIndexSQL (tableName , index ))
768
+ if err != nil {
769
+ log .Error ("Create index %s on table %s failed: %v" , index .Name , tableName , err )
770
+ }
771
+ }
772
+ }()
773
+
774
+ alterSQL := x .Dialect ().ModifyColumnSQL (tableName , col )
775
+ if _ , err := x .Exec (alterSQL ); err != nil {
776
+ return err
777
+ }
778
+ return nil
779
+ }
0 commit comments