From 32e2e81741a3f54aafd58f7eeb3cb6b90030d545 Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 28 Mar 2019 19:30:40 +0100 Subject: [PATCH] Started adding extra commands for rollback and list --- pkg/cmd/migrate.go | 21 +++++++++++ pkg/migration/migration.go | 75 +++++++------------------------------- 2 files changed, 35 insertions(+), 61 deletions(-) diff --git a/pkg/cmd/migrate.go b/pkg/cmd/migrate.go index b7d78e6aa..f825ad0e2 100644 --- a/pkg/cmd/migrate.go +++ b/pkg/cmd/migrate.go @@ -22,6 +22,9 @@ import ( ) func init() { + migrateCmd.AddCommand(migrateListCmd) + migrationRollbackCmd.Flags().StringVarP(&rollbackUntilFlag, "name", "n", "", "The id of the migration you want to roll back until.") + migrateCmd.AddCommand(migrationRollbackCmd) rootCmd.AddCommand(migrateCmd) } @@ -35,3 +38,21 @@ var migrateCmd = &cobra.Command{ migration.Migrate(nil) }, } + +var migrateListCmd = &cobra.Command{ + Use: "list", + Short: "Show a list with all database migrations.", + Run: func(cmd *cobra.Command, args []string) { + migration.ListMigrations() + }, +} + +var rollbackUntilFlag string + +var migrationRollbackCmd = &cobra.Command{ + Use: "rollback", + Short: "Roll migrations back until a certain point.", + Run: func(cmd *cobra.Command, args []string) { + migration.Rollback(rollbackUntilFlag) + }, +} diff --git a/pkg/migration/migration.go b/pkg/migration/migration.go index 31fac0b26..2cb7de436 100644 --- a/pkg/migration/migration.go +++ b/pkg/migration/migration.go @@ -30,14 +30,15 @@ import ( var migrations []*xormigrate.Migration -func Migrate(x *xorm.Engine) { +// A helper function because we need a migration in various places which we can't really solve with an init() function. +func initMigration(x *xorm.Engine) *xormigrate.Xormigrate { // 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 + return nil } } @@ -50,71 +51,23 @@ func Migrate(x *xorm.Engine) { m := xormigrate.New(x, migrations) m.NewLogger(log.GetLogWriter("database")) m.InitSchema(initSchema) + return m +} + +func Migrate(x *xorm.Engine) { + m := initMigration(x) err := m.Migrate() if err != nil { log.Log.Fatalf("Migration failed: %v", err) } +} - /* - XORM logs from try.vikunja.io: +func ListMigrations() { - 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 +} + +func Rollback(until string) { - 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. @@ -136,6 +89,6 @@ func dropTableColum(x *xorm.Engine, tableName, col string) error { func initSchema(tx *xorm.Engine) error { return tx.Sync2( - models.GetTables()..., + models.GetTables()..., //TODO: Why does it fail here with new dbs? ) }