Fix migration statements

This commit is contained in:
kolaente 2020-12-18 16:45:51 +01:00
parent c11e2df9ca
commit 1b8b1c0539
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 8 additions and 7 deletions

View File

@ -24,7 +24,7 @@ import (
"xorm.io/xorm"
)
func changeColumnToBigint(x *xorm.Engine, table, column string, nullable, defaultValue bool) (err error) {
func changeColumnToBigint(x *xorm.Session, table, column string, nullable, defaultValue bool) (err error) {
switch config.DatabaseType.GetString() {
case "sqlite":
// Sqlite only has one "INTEGER" type which is at the same time int and int64
@ -39,17 +39,17 @@ func changeColumnToBigint(x *xorm.Engine, table, column string, nullable, defaul
def = " DEFAULT 0"
}
_, err := x.Exec("ALTER TABLE " + table + " MODIFY " + column + " BIGINT" + notnull + def)
_, err := x.Exec("ALTER TABLE " + table + " MODIFY `" + column + "` BIGINT" + def + notnull)
if err != nil {
return err
}
case "postgres":
var notnull = " ALTER COLUMN " + column + " SET "
var notnull = " ALTER COLUMN `" + column + "` SET "
if !nullable {
notnull = "NOT"
}
notnull += " NULL"
_, err := x.Exec("ALTER TABLE " + table + " ALTER COLUMN " + column + " TYPE BIGINT" + notnull)
_, err := x.Exec("ALTER TABLE " + table + " ALTER COLUMN `" + column + "` TYPE BIGINT" + notnull)
if err != nil {
return err
}
@ -180,6 +180,7 @@ func init() {
},
}
s := tx.NewSession()
for table, cols := range columns {
for _, col := range cols {
var nullable = false
@ -195,14 +196,14 @@ func init() {
}
log.Debugf("Migrating %s.%s to bigint", table, col)
err := changeColumnToBigint(tx, table, col, nullable, defaultValue)
err := changeColumnToBigint(s, table, col, nullable, defaultValue)
if err != nil {
_ = s.Rollback()
return err
}
}
}
return nil
return s.Commit()
},
Rollback: func(tx *xorm.Engine) error {
return nil