1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/migrations/20210411_add_imprint_content.go
Steven Tang 6256c8e10a
ref: embed files, bump to go 1.16 (#167)
* ref: embed portion of files
* fix: readd pkger
* ref: embed version.txt
* fix: wrong mail template import path
* refactor: get rid of sql-migrate
refactor: get rid of pkger in favor of go embed (resolve #164)
* chore: remove unused var [ci-skip]

Co-authored-by: Ferdinand Mütsch <ferdinand@muetsch.io>
2021-04-11 10:42:43 +00:00

48 lines
1.1 KiB
Go

package migrations
import (
"github.com/emvi/logbuch"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func init() {
const name = "20210411-add_imprint_content"
f := migrationFunc{
name: name,
f: func(db *gorm.DB, cfg *config.Config) error {
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
}
imprintKv := &models.KeyStringValue{Key: "imprint", Value: "no content here"}
if err := db.
Clauses(clause.OnConflict{UpdateAll: false, DoNothing: true}).
Where(condition, imprintKv.Key).
Assign(imprintKv).
Create(imprintKv).Error; err != nil {
return err
}
if err := db.Create(&models.KeyStringValue{
Key: name,
Value: "done",
}).Error; err != nil {
return err
}
return nil
},
}
registerPostMigration(f)
}