143 lines
6.3 KiB
Go
143 lines
6.3 KiB
Go
// Vikunja is a todo-list application to facilitate your life.
|
|
// Copyright 2019 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/db"
|
|
"code.vikunja.io/api/pkg/log"
|
|
"code.vikunja.io/api/pkg/models"
|
|
"github.com/go-xorm/xorm"
|
|
"github.com/spf13/viper"
|
|
"sort"
|
|
"src.techknowlogick.com/xormigrate"
|
|
)
|
|
|
|
// You can get the id string for new migrations by running `date +%Y%m%d%H%M%S` on a unix system.
|
|
|
|
var migrations []*xormigrate.Migration
|
|
|
|
func Migrate(x *xorm.Engine) {
|
|
// Get our own xorm engine if we don't have one
|
|
if x == nil {
|
|
var err error
|
|
x, err = db.CreateDBEngine()
|
|
if err != nil {
|
|
log.Log.Criticalf("Could not connect to db: %v", err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
// Because init() does not guarantee the order in which these are added to the slice,
|
|
// we need to sort them to ensure that they are in order
|
|
sort.Slice(migrations, func(i, j int) bool {
|
|
return migrations[i].ID < migrations[j].ID
|
|
})
|
|
|
|
m := xormigrate.New(x, migrations)
|
|
m.NewLogger(log.GetLogWriter("database"))
|
|
m.InitSchema(initSchema)
|
|
err := m.Migrate()
|
|
if err != nil {
|
|
log.Log.Criticalf("Migration failed: %v", err)
|
|
}
|
|
|
|
/*
|
|
XORM logs from try.vikunja.io:
|
|
|
|
Table users Column email db default is 'NULL', struct default is
|
|
Table users Column is_active db default is NULL, struct default is
|
|
Table users Column password_reset_token db default is 'NULL', struct default is
|
|
Table users Column email_confirm_token db default is 'NULL', struct default is
|
|
Table users Column created db default is NULL, struct default is
|
|
Table users Column updated db default is NULL, struct default is
|
|
Table list Column title db default is 'NULL', struct default is
|
|
Table list Column description db default is 'NULL', struct default is
|
|
Table list Column owner_id db default is NULL, struct default is
|
|
Table list Column namespace_id db default is NULL, struct default is
|
|
Table list Column created db default is NULL, struct default is
|
|
Table list Column updated db default is NULL, struct default is
|
|
Table tasks Column text db default is 'NULL', struct default is
|
|
Table tasks Column description db default is 'NULL', struct default is
|
|
Table tasks Column done db default is NULL, struct default is
|
|
Table tasks Column due_date_unix db default is NULL, struct default is
|
|
Table tasks Column reminders_unix db default is 'NULL', struct default is
|
|
Table tasks Column created_by_id db default is NULL, struct default is
|
|
Table tasks Column list_id db default is NULL, struct default is
|
|
Table tasks Column repeat_after db default is NULL, struct default is
|
|
Table tasks Column parent_task_id db default is NULL, struct default is
|
|
Table tasks Column priority db default is NULL, struct default is
|
|
Table tasks Column start_date_unix db default is NULL, struct default is
|
|
Table tasks Column end_date_unix db default is NULL, struct default is
|
|
Table tasks Column created db default is NULL, struct default is
|
|
Table tasks Column updated db default is NULL, struct default is
|
|
Table teams Column description db default is 'NULL', struct default is
|
|
Table teams Column created db default is NULL, struct default is
|
|
Table teams Column updated db default is NULL, struct default is
|
|
Table team_members Column admin db default is NULL, struct default is
|
|
Table team_members Column created db default is NULL, struct default is
|
|
Table team_list Column right db default is NULL, struct default is
|
|
Table team_list Column created db default is NULL, struct default is
|
|
Table team_list Column updated db default is NULL, struct default is
|
|
Table team_namespaces Column right db default is NULL, struct default is
|
|
Table team_namespaces Column created db default is NULL, struct default is
|
|
Table team_namespaces Column updated db default is NULL, struct default is
|
|
Table namespaces Column name db default is 'NULL', struct default is
|
|
Table namespaces Column description db default is 'NULL', struct default is
|
|
Table namespaces Column created db default is NULL, struct default is
|
|
Table namespaces Column updated db default is NULL, struct default is
|
|
Table users_list Column right db default is NULL, struct default is
|
|
Table users_list Column created db default is NULL, struct default is
|
|
Table users_list Column updated db default is NULL, struct default is
|
|
Table users_namespace Column right db default is NULL, struct default is
|
|
Table users_namespace Column created db default is NULL, struct default is
|
|
Table users_namespace Column updated db default is NULL, struct default is
|
|
Table task_assignees Column created db default is NULL, struct default is
|
|
Table labels Column description db default is 'NULL', struct default isdfdfsd
|
|
Table labels Column hex_color db default is 'NULL', struct default is
|
|
Table labels Column created db default is NULL, struct default is
|
|
Table labels Column updated db default is NULL, struct default is
|
|
Table label_task Column created db default is NULL, struct default is
|
|
|
|
Table tasks has column reminder_unix but struct has not related field
|
|
Table team_members has column updated but struct has not related field
|
|
*/
|
|
}
|
|
|
|
// Deletes a column from a table. All arguments are strings, to let them be standalone and not depending on any struct.
|
|
func dropTableColum(x *xorm.Engine, tableName, col string) error {
|
|
|
|
switch viper.GetString("database.type") {
|
|
case "sqlite":
|
|
log.Log.Warning("Unable to drop columns in SQLite")
|
|
case "mysql":
|
|
_, err := x.Exec("ALTER TABLE ? DROP COLUMN ?", tableName, col)
|
|
if err != nil {
|
|
log.Log.Errorf("Error dropping column: %v", err)
|
|
return err
|
|
}
|
|
default:
|
|
log.Log.Fatal("Unknown db.")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func initSchema(tx *xorm.Engine) error {
|
|
return tx.Sync2(
|
|
models.GetTables()...,
|
|
)
|
|
}
|