diff --git a/pkg/cmd/migrate.go b/pkg/cmd/migrate.go index fcbb4c16a..bf177d7c1 100644 --- a/pkg/cmd/migrate.go +++ b/pkg/cmd/migrate.go @@ -25,6 +25,7 @@ func init() { rootCmd.AddCommand(migrateCmd) } +// TODO: add args to run migrations up or down, until a certain point etc var migrateCmd = &cobra.Command{ Use: "migrate", Short: "Run all database migrations which didn't already run.", diff --git a/pkg/migration/20190324205606.go b/pkg/migration/20190324205606.go index 18956f890..a6272a45d 100644 --- a/pkg/migration/20190324205606.go +++ b/pkg/migration/20190324205606.go @@ -24,10 +24,9 @@ import ( func init() { migrations = append(migrations, &xormigrate.Migration{ ID: "20190324205606", - Description: "Lorem Ipsum", + Description: "Remove reminder_unix from tasks", Migrate: func(tx *xorm.Engine) error { - - return nil + return dropTableColum(tx, "tasks", "reminder_unix") }, Rollback: func(tx *xorm.Engine) error { diff --git a/pkg/migration/migration.go b/pkg/migration/migration.go index 3494ca8ac..0cf549aca 100644 --- a/pkg/migration/migration.go +++ b/pkg/migration/migration.go @@ -20,6 +20,7 @@ import ( "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/log" "github.com/go-xorm/xorm" + "github.com/spf13/viper" "sort" "src.techknowlogick.com/xormigrate" ) @@ -53,10 +54,6 @@ func Migrate(x *xorm.Engine) { log.Log.Criticalf("Migration failed: %v", err) } - //m.InitSchema() - - // TODO: Init schema - /* 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 { return tx.Sync2( db.GetTables(),