66package migrations
77
88import (
9+ "context"
910 "fmt"
1011 "os"
1112 "reflect"
@@ -16,6 +17,7 @@ import (
1617 "code.gitea.io/gitea/modules/setting"
1718
1819 "xorm.io/xorm"
20+ "xorm.io/xorm/schemas"
1921)
2022
2123const minDBVersion = 70 // Gitea 1.5.3
@@ -273,6 +275,8 @@ var migrations = []Migration{
273275 NewMigration ("Convert topic name from 25 to 50" , convertTopicNameFrom25To50 ),
274276 // v164 -> v165
275277 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 ),
276280}
277281
278282// GetCurrentDBVersion returns the current db version
@@ -737,3 +741,39 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
737741
738742 return nil
739743}
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