2021-02-13 15:04:47 +03:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2021-02-14 18:01:31 +03:00
|
|
|
"github.com/emvi/logbuch"
|
2021-02-13 15:04:47 +03:00
|
|
|
"github.com/muety/wakapi/config"
|
2021-02-14 18:01:31 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2021-02-13 15:04:47 +03:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-02-14 18:01:31 +03:00
|
|
|
const name = "20210213-add_has_data_field"
|
2021-02-13 15:04:47 +03:00
|
|
|
f := migrationFunc{
|
2021-02-14 18:01:31 +03:00
|
|
|
name: name,
|
2021-02-13 15:04:47 +03:00
|
|
|
f: func(db *gorm.DB, cfg *config.Config) error {
|
2021-02-14 18:01:31 +03:00
|
|
|
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 {
|
2021-02-13 15:04:47 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-02-14 18:01:31 +03:00
|
|
|
|
|
|
|
if err := db.Create(&models.KeyStringValue{
|
|
|
|
Key: name,
|
|
|
|
Value: "done",
|
|
|
|
}).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-13 15:04:47 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
registerPostMigration(f)
|
|
|
|
}
|