2021-02-02 23:50:43 +03:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/emvi/logbuch"
|
|
|
|
"github.com/muety/wakapi/config"
|
2022-12-29 13:54:14 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2021-02-02 23:50:43 +03:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-12-29 13:54:14 +03:00
|
|
|
type gormMigrationFunc func(db *gorm.DB) error
|
|
|
|
|
2021-02-02 23:50:43 +03:00
|
|
|
type migrationFunc struct {
|
|
|
|
f func(db *gorm.DB, cfg *config.Config) error
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type migrationFuncs []migrationFunc
|
|
|
|
|
|
|
|
var (
|
|
|
|
preMigrations migrationFuncs
|
|
|
|
postMigrations migrationFuncs
|
|
|
|
)
|
|
|
|
|
2022-12-29 13:54:14 +03:00
|
|
|
func GetMigrationFunc(cfg *config.Config) gormMigrationFunc {
|
|
|
|
switch cfg.Db.Dialect {
|
|
|
|
default:
|
|
|
|
return func(db *gorm.DB) error {
|
|
|
|
if err := db.AutoMigrate(&models.User{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.KeyStringValue{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.Alias{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.Heartbeat{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.Summary{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.SummaryItem{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.LanguageMapping{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.ProjectLabel{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.Diagnostics{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := db.AutoMigrate(&models.LeaderboardItem{}); err != nil && !cfg.Db.AutoMigrateFailSilently {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 23:50:43 +03:00
|
|
|
func registerPreMigration(f migrationFunc) {
|
|
|
|
preMigrations = append(preMigrations, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerPostMigration(f migrationFunc) {
|
|
|
|
postMigrations = append(postMigrations, f)
|
|
|
|
}
|
|
|
|
|
2021-02-21 14:02:19 +03:00
|
|
|
func Run(db *gorm.DB, cfg *config.Config) {
|
|
|
|
RunPreMigrations(db, cfg)
|
|
|
|
RunSchemaMigrations(db, cfg)
|
|
|
|
RunPostMigrations(db, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunSchemaMigrations(db *gorm.DB, cfg *config.Config) {
|
2022-12-29 13:54:14 +03:00
|
|
|
if err := GetMigrationFunc(cfg)(db); err != nil {
|
2021-02-21 14:02:19 +03:00
|
|
|
logbuch.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 23:50:43 +03:00
|
|
|
|
|
|
|
func RunPreMigrations(db *gorm.DB, cfg *config.Config) {
|
|
|
|
sort.Sort(preMigrations)
|
|
|
|
|
|
|
|
for _, m := range preMigrations {
|
|
|
|
logbuch.Info("potentially running migration '%s'", m.name)
|
|
|
|
if err := m.f(db, cfg); err != nil {
|
2022-02-17 14:20:22 +03:00
|
|
|
logbuch.Fatal("migration '%s' failed - %v", m.name, err)
|
2021-02-02 23:50:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 14:02:19 +03:00
|
|
|
func RunPostMigrations(db *gorm.DB, cfg *config.Config) {
|
2021-02-02 23:50:43 +03:00
|
|
|
sort.Sort(postMigrations)
|
|
|
|
|
|
|
|
for _, m := range postMigrations {
|
|
|
|
logbuch.Info("potentially running migration '%s'", m.name)
|
|
|
|
if err := m.f(db, cfg); err != nil {
|
2022-02-17 14:20:22 +03:00
|
|
|
logbuch.Fatal("migration '%s' failed - %v", m.name, err)
|
2021-02-02 23:50:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m migrationFuncs) Len() int {
|
|
|
|
return len(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m migrationFuncs) Less(i, j int) bool {
|
|
|
|
return strings.Compare(m[i].name, m[j].name) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m migrationFuncs) Swap(i, j int) {
|
|
|
|
m[i], m[j] = m[j], m[i]
|
|
|
|
}
|