Added function to drop a column from a table
continuous-integration/drone/push Build is failing Details

This commit is contained in:
kolaente 2019-03-26 08:20:45 +01:00
parent 6b48bcf9c2
commit 2495986872
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 22 additions and 7 deletions

View File

@ -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.",

View File

@ -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 {

View File

@ -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(),