DB Migrations #67
|
@ -25,6 +25,7 @@ func init() {
|
||||||
rootCmd.AddCommand(migrateCmd)
|
rootCmd.AddCommand(migrateCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: add args to run migrations up or down, until a certain point etc
|
||||||
var migrateCmd = &cobra.Command{
|
var migrateCmd = &cobra.Command{
|
||||||
Use: "migrate",
|
Use: "migrate",
|
||||||
Short: "Run all database migrations which didn't already run.",
|
Short: "Run all database migrations which didn't already run.",
|
||||||
|
|
|
@ -24,10 +24,9 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
migrations = append(migrations, &xormigrate.Migration{
|
migrations = append(migrations, &xormigrate.Migration{
|
||||||
ID: "20190324205606",
|
ID: "20190324205606",
|
||||||
Description: "Lorem Ipsum",
|
Description: "Remove reminder_unix from tasks",
|
||||||
Migrate: func(tx *xorm.Engine) error {
|
Migrate: func(tx *xorm.Engine) error {
|
||||||
|
return dropTableColum(tx, "tasks", "reminder_unix")
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
Rollback: func(tx *xorm.Engine) error {
|
Rollback: func(tx *xorm.Engine) error {
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
|
"github.com/spf13/viper"
|
||||||
"sort"
|
"sort"
|
||||||
"src.techknowlogick.com/xormigrate"
|
"src.techknowlogick.com/xormigrate"
|
||||||
)
|
)
|
||||||
|
@ -53,10 +54,6 @@ func Migrate(x *xorm.Engine) {
|
||||||
log.Log.Criticalf("Migration failed: %v", err)
|
log.Log.Criticalf("Migration failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//m.InitSchema()
|
|
||||||
|
|
||||||
// TODO: Init schema
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
XORM logs from try.vikunja.io:
|
XORM logs from try.vikunja.io:
|
||||||
|
|
||||||
|
@ -119,6 +116,24 @@ func Migrate(x *xorm.Engine) {
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
func initSchema(tx *xorm.Engine) error {
|
||||||
return tx.Sync2(
|
return tx.Sync2(
|
||||||
db.GetTables(),
|
db.GetTables(),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user