Fix wrong type on hooktask to convert typ from char(16) to varchar(16) (#14148)

* Fix wrong type on hooktask to convert typ from char(16) to varchar(16)

* Fix bugs

* Improve code

* Use different trim function for MSSQL

* Fix bug

* Removed wrong changed line

* Removed wrong changed line

* Fix nullable

* Fix lint

* Ignore sqlite on migration

* Fix mssql modify column failure

* Move modifyColumn to migrations.go so that other migrate function could use it
This commit is contained in:
Lunny Xiao 2021-01-06 23:11:23 +08:00 committed by GitHub
parent d2ee1221cc
commit 9f73cae635
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 8 deletions

View file

@ -6,6 +6,7 @@
package migrations
import (
"context"
"fmt"
"os"
"reflect"
@ -16,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)
const minDBVersion = 70 // Gitea 1.5.3
@ -273,6 +275,8 @@ var migrations = []Migration{
NewMigration("Convert topic name from 25 to 50", convertTopicNameFrom25To50),
// v164 -> v165
NewMigration("Add scope and nonce columns to oauth2_grant table", addScopeAndNonceColumnsToOAuth2Grant),
// v165 -> v166
NewMigration("Convert hook task type from char(16) to varchar(16) and trim the column", convertHookTaskTypeToVarcharAndTrim),
}
// GetCurrentDBVersion returns the current db version
@ -737,3 +741,39 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
return nil
}
// modifyColumn will modify column's type or other propertity. SQLITE is not supported
func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error {
var indexes map[string]*schemas.Index
var err error
// MSSQL have to remove index at first, otherwise alter column will fail
// 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/
if x.Dialect().URI().DBType == schemas.MSSQL {
indexes, err = x.Dialect().GetIndexes(x.DB(), context.Background(), tableName)
if err != nil {
return err
}
for _, index := range indexes {
_, err = x.Exec(x.Dialect().DropIndexSQL(tableName, index))
if err != nil {
return err
}
}
}
defer func() {
for _, index := range indexes {
_, err = x.Exec(x.Dialect().CreateIndexSQL(tableName, index))
if err != nil {
log.Error("Create index %s on table %s failed: %v", index.Name, tableName, err)
}
}
}()
alterSQL := x.Dialect().ModifyColumnSQL(tableName, col)
if _, err := x.Exec(alterSQL); err != nil {
return err
}
return nil
}