Skip to content

Commit

Permalink
Move modifyColumn to migrations.go so that other migrate function cou…
Browse files Browse the repository at this point in the history
…ld use it
  • Loading branch information
lunny committed Jan 5, 2021
1 parent 1343067 commit 7fd394d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
38 changes: 38 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package migrations

import (
"context"
"fmt"
"os"
"reflect"
Expand All @@ -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
Expand Down Expand Up @@ -739,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
}
38 changes: 0 additions & 38 deletions models/migrations/v165.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,10 @@
package migrations

import (
"context"

"code.gitea.io/gitea/modules/log"
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)

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
}

func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
dbType := x.Dialect().URI().DBType
if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT
Expand Down

0 comments on commit 7fd394d

Please sign in to comment.