From b4d2ee7d16ecc61cd8ba95c4f6a06c5bce4a5217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Tue, 3 Nov 2020 10:02:39 +0100 Subject: [PATCH] fix: not creating language mappings table due to broken type definition in users model (resolve #69) chore: introduce foreign key constraints --- config/config.go | 7 +++---- migrations/common/custom_pre.go | 14 ++++++++++++-- models/alias.go | 1 + models/summary.go | 2 ++ models/user.go | 2 +- version.txt | 2 +- 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index d0e6470..140aee1 100644 --- a/config/config.go +++ b/config/config.go @@ -81,13 +81,12 @@ func (c *Config) GetMigrationFunc(dbDialect string) models.MigrationFunc { switch dbDialect { default: return func(db *gorm.DB) error { + db.AutoMigrate(&models.User{}) + db.AutoMigrate(&models.KeyStringValue{}) db.AutoMigrate(&models.Alias{}) + db.AutoMigrate(&models.Heartbeat{}) db.AutoMigrate(&models.Summary{}) db.AutoMigrate(&models.SummaryItem{}) - db.AutoMigrate(&models.User{}) - db.AutoMigrate(&models.Heartbeat{}) - db.AutoMigrate(&models.SummaryItem{}) - db.AutoMigrate(&models.KeyStringValue{}) db.AutoMigrate(&models.LanguageMapping{}) return nil } diff --git a/migrations/common/custom_pre.go b/migrations/common/custom_pre.go index 4b41586..4e53aca 100644 --- a/migrations/common/custom_pre.go +++ b/migrations/common/custom_pre.go @@ -13,8 +13,18 @@ func init() { customPreMigrations = []migrationFunc{ { f: func(db *gorm.DB, cfg *config.Config) error { - if db.Migrator().HasTable("custom_rules") { - return db.Migrator().RenameTable("custom_rules", &models.LanguageMapping{}) + migrator := db.Migrator() + oldTableName, newTableName := "custom_rules", "language_mappings" + oldIndexName, newIndexName := "idx_customrule_user", "idx_language_mapping_user" + + if migrator.HasTable(oldTableName) { + log.Printf("renaming '%s' table to '%s'\n", oldTableName, newTableName) + if err := migrator.RenameTable(oldTableName, &models.LanguageMapping{}); err != nil { + return err + } + + log.Printf("renaming '%s' index to '%s'\n", oldIndexName, newIndexName) + return migrator.RenameIndex(&models.LanguageMapping{}, oldIndexName, newIndexName) } return nil }, diff --git a/models/alias.go b/models/alias.go index 691ebdd..4f121b2 100644 --- a/models/alias.go +++ b/models/alias.go @@ -3,6 +3,7 @@ package models type Alias struct { ID uint `gorm:"primary_key"` Type uint8 `gorm:"not null; index:idx_alias_type_key"` + User *User `json:"-" gorm:"not null"` UserID string `gorm:"not null; index:idx_alias_user"` Key string `gorm:"not null; index:idx_alias_type_key"` Value string `gorm:"not null"` diff --git a/models/summary.go b/models/summary.go index 4af3a60..b8ba8d5 100644 --- a/models/summary.go +++ b/models/summary.go @@ -35,6 +35,7 @@ const UnknownSummaryKey = "unknown" type Summary struct { ID uint `json:"-" gorm:"primary_key"` + User *User `json:"-" gorm:"not null"` UserID string `json:"user_id" gorm:"not null; index:idx_time_summary_user"` FromTime CustomTime `json:"from" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user"` ToTime CustomTime `json:"to" gorm:"not null; type:timestamp; default:CURRENT_TIMESTAMP; index:idx_time_summary_user"` @@ -47,6 +48,7 @@ type Summary struct { type SummaryItem struct { ID uint `json:"-" gorm:"primary_key"` + Summary *Summary `json:"-" gorm:"not null"` SummaryID uint `json:"-"` Type uint8 `json:"-"` Key string `json:"key"` diff --git a/models/user.go b/models/user.go index 6016085..e99182a 100644 --- a/models/user.go +++ b/models/user.go @@ -6,7 +6,7 @@ type User struct { Password string `json:"-"` CreatedAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"` LastLoggedInAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"` - BadgesEnabled bool `json:"-" gorm:"not null; default:false; type: bool"` + BadgesEnabled bool `json:"-" gorm:"default:false; type:bool"` } type Login struct { diff --git a/version.txt b/version.txt index 850e742..63e799c 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.14.0 +1.14.1