1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: broken migration on postgres (resolve #127)

This commit is contained in:
Ferdinand Mütsch 2021-02-14 16:01:31 +01:00
parent e2f046a83d
commit 708863fd33
3 changed files with 25 additions and 4 deletions

View File

@ -14,7 +14,7 @@ func init() {
migrator := db.Migrator() migrator := db.Migrator()
if !migrator.HasColumn(&models.User{}, "badges_enabled") { if !migrator.HasColumn(&models.User{}, "badges_enabled") {
// empty database, nothing to migrate // empty database or already migrated, nothing to migrate
return nil return nil
} }

View File

@ -1,17 +1,38 @@
package migrations package migrations
import ( import (
"github.com/emvi/logbuch"
"github.com/muety/wakapi/config" "github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"gorm.io/gorm" "gorm.io/gorm"
) )
func init() { func init() {
const name = "20210213-add_has_data_field"
f := migrationFunc{ f := migrationFunc{
name: "20210213_add_has_data_field", name: name,
f: func(db *gorm.DB, cfg *config.Config) error { f: func(db *gorm.DB, cfg *config.Config) error {
if err := db.Exec("UPDATE users SET has_data = TRUE WHERE 1").Error; err != nil { condition := "key = ?"
if cfg.Db.Dialect == config.SQLDialectMysql {
condition = "`key` = ?"
}
lookupResult := db.Where(condition, name).First(&models.KeyStringValue{})
if lookupResult.Error == nil && lookupResult.RowsAffected > 0 {
logbuch.Info("no need to migrate '%s'", name)
return nil
}
if err := db.Exec("UPDATE users SET has_data = TRUE WHERE TRUE").Error; err != nil {
return err return err
} }
if err := db.Create(&models.KeyStringValue{
Key: name,
Value: "done",
}).Error; err != nil {
return err
}
return nil return nil
}, },
} }

View File

@ -1 +1 @@
1.24.0 1.24.1