Add migration to make all int(11) fields bigint by default
This commit is contained in:
parent
1435e80aa4
commit
c11e2df9ca
|
@ -0,0 +1,211 @@
|
|||
// Vikunja is a to-do list application to facilitate your life.
|
||||
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package migration
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"src.techknowlogick.com/xormigrate"
|
||||
"strings"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func changeColumnToBigint(x *xorm.Engine, 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
|
||||
case "mysql":
|
||||
var notnull = " NOT NULL"
|
||||
if nullable {
|
||||
notnull = ""
|
||||
}
|
||||
|
||||
var def = ""
|
||||
if defaultValue {
|
||||
def = " DEFAULT 0"
|
||||
}
|
||||
|
||||
_, err := x.Exec("ALTER TABLE " + table + " MODIFY " + column + " BIGINT" + notnull + def)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case "postgres":
|
||||
var notnull = " ALTER COLUMN " + column + " SET "
|
||||
if !nullable {
|
||||
notnull = "NOT"
|
||||
}
|
||||
notnull += " NULL"
|
||||
_, err := x.Exec("ALTER TABLE " + table + " ALTER COLUMN " + column + " TYPE BIGINT" + notnull)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
log.Fatal("Unknown db.")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
migrations = append(migrations, &xormigrate.Migration{
|
||||
ID: "20201218152741",
|
||||
Description: "Make sure all int64 fields are bigint in the db",
|
||||
Migrate: func(tx *xorm.Engine) error {
|
||||
columns := make(map[string][]string) // table is the key, columns are the contents
|
||||
columns = map[string][]string{
|
||||
"totp": {
|
||||
"id",
|
||||
"user_id",
|
||||
},
|
||||
"users": {
|
||||
"id",
|
||||
},
|
||||
"files": {
|
||||
"id",
|
||||
"size", // TODO: This should be a uint64
|
||||
"created_by_id",
|
||||
},
|
||||
"namespaces": {
|
||||
"id",
|
||||
"owner_id",
|
||||
},
|
||||
"team_list": {
|
||||
"id",
|
||||
"team_id",
|
||||
"list_id",
|
||||
"default:right",
|
||||
},
|
||||
"saved_filters": {
|
||||
"owner_id",
|
||||
},
|
||||
"label_task": {
|
||||
"id",
|
||||
"task_id",
|
||||
"label_id",
|
||||
},
|
||||
"task_relations": {
|
||||
"id",
|
||||
"task_id",
|
||||
"other_task_id",
|
||||
"created_by_id",
|
||||
},
|
||||
"team_namespaces": {
|
||||
"id",
|
||||
"team_id",
|
||||
"namespace_id",
|
||||
"default:right",
|
||||
},
|
||||
"users_namespace": {
|
||||
"id",
|
||||
"user_id",
|
||||
"namespace_id",
|
||||
"default:right",
|
||||
},
|
||||
"teams": {
|
||||
"id",
|
||||
"created_by_id",
|
||||
},
|
||||
"team_members": {
|
||||
"id",
|
||||
"team_id",
|
||||
"user_id",
|
||||
},
|
||||
"task_assignees": {
|
||||
"id",
|
||||
"task_id",
|
||||
"user_id",
|
||||
},
|
||||
"users_list": {
|
||||
"id",
|
||||
"user_id",
|
||||
"list_id",
|
||||
"default:right",
|
||||
},
|
||||
"tasks": {
|
||||
"id",
|
||||
"created_by_id",
|
||||
"list_id",
|
||||
"nullable:repeat_after",
|
||||
"nullable:priority",
|
||||
"default:index",
|
||||
"nullable:bucket_id",
|
||||
},
|
||||
"task_reminders": {
|
||||
"id",
|
||||
"task_id",
|
||||
},
|
||||
"task_attachments": {
|
||||
"id",
|
||||
"task_id",
|
||||
"file_id",
|
||||
"created_by_id",
|
||||
},
|
||||
"list": {
|
||||
"id",
|
||||
"owner_id",
|
||||
"namespace_id",
|
||||
},
|
||||
"labels": {
|
||||
"id",
|
||||
"created_by_id",
|
||||
},
|
||||
"buckets": {
|
||||
"id",
|
||||
"list_id",
|
||||
"created_by_id",
|
||||
},
|
||||
"link_sharing": {
|
||||
"id",
|
||||
"list_id",
|
||||
"default:right",
|
||||
"sharing_type",
|
||||
"shared_by_id",
|
||||
},
|
||||
"migration_status": {
|
||||
"id",
|
||||
"user_id",
|
||||
},
|
||||
}
|
||||
|
||||
for table, cols := range columns {
|
||||
for _, col := range cols {
|
||||
var nullable = false
|
||||
if strings.HasPrefix(col, "nullable:") {
|
||||
col = strings.ReplaceAll(col, "nullable:", "")
|
||||
nullable = true
|
||||
}
|
||||
|
||||
var defaultValue = false
|
||||
if strings.HasPrefix(col, "default:") {
|
||||
col = strings.ReplaceAll(col, "default:", "")
|
||||
defaultValue = true
|
||||
}
|
||||
|
||||
log.Debugf("Migrating %s.%s to bigint", table, col)
|
||||
err := changeColumnToBigint(tx, table, col, nullable, defaultValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Rollback: func(tx *xorm.Engine) error {
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue