2020-11-01 22:14:10 +03:00
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
2021-01-30 13:17:37 +03:00
|
|
|
|
"github.com/emvi/logbuch"
|
2020-11-01 22:14:10 +03:00
|
|
|
|
"github.com/muety/wakapi/config"
|
|
|
|
|
"github.com/muety/wakapi/models"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var customPreMigrations []migrationFunc
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
customPreMigrations = []migrationFunc{
|
|
|
|
|
{
|
|
|
|
|
f: func(db *gorm.DB, cfg *config.Config) error {
|
2020-11-03 12:02:39 +03:00
|
|
|
|
migrator := db.Migrator()
|
|
|
|
|
oldTableName, newTableName := "custom_rules", "language_mappings"
|
|
|
|
|
oldIndexName, newIndexName := "idx_customrule_user", "idx_language_mapping_user"
|
|
|
|
|
|
|
|
|
|
if migrator.HasTable(oldTableName) {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Info("renaming '%s' table to '%s'", oldTableName, newTableName)
|
2020-11-03 12:02:39 +03:00
|
|
|
|
if err := migrator.RenameTable(oldTableName, &models.LanguageMapping{}); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Info("renaming '%s' index to '%s'", oldIndexName, newIndexName)
|
2020-11-03 12:02:39 +03:00
|
|
|
|
return migrator.RenameIndex(&models.LanguageMapping{}, oldIndexName, newIndexName)
|
2020-11-01 22:14:10 +03:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
name: "rename language mappings table",
|
|
|
|
|
},
|
2020-11-06 19:09:41 +03:00
|
|
|
|
{
|
|
|
|
|
f: func(db *gorm.DB, cfg *config.Config) error {
|
|
|
|
|
// drop all already existing foreign key constraints
|
|
|
|
|
// afterwards let them be re-created by auto migrate with the newly introduced cascade settings,
|
|
|
|
|
|
|
|
|
|
migrator := db.Migrator()
|
|
|
|
|
const lookupKey = "20201106-migration_cascade_constraints"
|
|
|
|
|
|
2020-11-06 19:20:26 +03:00
|
|
|
|
if cfg.Db.Dialect == config.SQLDialectSqlite {
|
2020-11-06 19:09:41 +03:00
|
|
|
|
// https://stackoverflow.com/a/1884893/3112139
|
|
|
|
|
// unfortunately, we can't migrate existing sqlite databases to the newly introduced cascade settings
|
|
|
|
|
// things like deleting all summaries won't work in those cases unless an entirely new db is created
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Info("not attempting to drop and regenerate constraints on sqlite")
|
2020-11-06 19:09:41 +03:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !migrator.HasTable(&models.KeyStringValue{}) {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Info("key-value table not yet existing")
|
2020-11-06 19:09:41 +03:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
condition := "key = ?"
|
2020-11-06 19:20:26 +03:00
|
|
|
|
if cfg.Db.Dialect == config.SQLDialectMysql {
|
2020-11-06 19:09:41 +03:00
|
|
|
|
condition = "`key` = ?"
|
|
|
|
|
}
|
|
|
|
|
lookupResult := db.Where(condition, lookupKey).First(&models.KeyStringValue{})
|
|
|
|
|
if lookupResult.Error == nil && lookupResult.RowsAffected > 0 {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Info("no need to migrate cascade constraints")
|
2020-11-06 19:09:41 +03:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SELECT * FROM INFORMATION_SCHEMA.table_constraints;
|
|
|
|
|
constraints := map[string]interface{}{
|
|
|
|
|
"fk_summaries_editors": &models.SummaryItem{},
|
|
|
|
|
"fk_summaries_languages": &models.SummaryItem{},
|
|
|
|
|
"fk_summaries_machines": &models.SummaryItem{},
|
|
|
|
|
"fk_summaries_operating_systems": &models.SummaryItem{},
|
|
|
|
|
"fk_summaries_projects": &models.SummaryItem{},
|
|
|
|
|
"fk_summary_items_summary": &models.SummaryItem{},
|
|
|
|
|
"fk_summaries_user": &models.Summary{},
|
|
|
|
|
"fk_language_mappings_user": &models.LanguageMapping{},
|
|
|
|
|
"fk_heartbeats_user": &models.Heartbeat{},
|
|
|
|
|
"fk_aliases_user": &models.Alias{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, table := range constraints {
|
|
|
|
|
if migrator.HasConstraint(table, name) {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Info("dropping constraint '%s'", name)
|
2020-11-06 19:09:41 +03:00
|
|
|
|
if err := migrator.DropConstraint(table, name); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := db.Create(&models.KeyStringValue{
|
|
|
|
|
Key: lookupKey,
|
|
|
|
|
Value: "done",
|
|
|
|
|
}).Error; err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
name: "add cascade constraints",
|
|
|
|
|
},
|
2020-11-01 22:14:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RunCustomPreMigrations(db *gorm.DB, cfg *config.Config) {
|
|
|
|
|
for _, m := range customPreMigrations {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Info("potentially running migration '%s'", m.name)
|
2020-11-01 22:14:10 +03:00
|
|
|
|
if err := m.f(db, cfg); err != nil {
|
2021-01-30 13:17:37 +03:00
|
|
|
|
logbuch.Fatal("migration '%s' failed – %v", m.name, err)
|
2020-11-01 22:14:10 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|