1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: not creating language mappings table due to broken type definition in users model (resolve #69)

chore: introduce foreign key constraints
This commit is contained in:
Ferdinand Mütsch 2020-11-03 10:02:39 +01:00
parent 1224024913
commit b4d2ee7d16
6 changed files with 20 additions and 8 deletions

View File

@ -81,13 +81,12 @@ func (c *Config) GetMigrationFunc(dbDialect string) models.MigrationFunc {
switch dbDialect { switch dbDialect {
default: default:
return func(db *gorm.DB) error { return func(db *gorm.DB) error {
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.KeyStringValue{})
db.AutoMigrate(&models.Alias{}) db.AutoMigrate(&models.Alias{})
db.AutoMigrate(&models.Heartbeat{})
db.AutoMigrate(&models.Summary{}) db.AutoMigrate(&models.Summary{})
db.AutoMigrate(&models.SummaryItem{}) db.AutoMigrate(&models.SummaryItem{})
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Heartbeat{})
db.AutoMigrate(&models.SummaryItem{})
db.AutoMigrate(&models.KeyStringValue{})
db.AutoMigrate(&models.LanguageMapping{}) db.AutoMigrate(&models.LanguageMapping{})
return nil return nil
} }

View File

@ -13,8 +13,18 @@ func init() {
customPreMigrations = []migrationFunc{ customPreMigrations = []migrationFunc{
{ {
f: func(db *gorm.DB, cfg *config.Config) error { f: func(db *gorm.DB, cfg *config.Config) error {
if db.Migrator().HasTable("custom_rules") { migrator := db.Migrator()
return db.Migrator().RenameTable("custom_rules", &models.LanguageMapping{}) 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 return nil
}, },

View File

@ -3,6 +3,7 @@ package models
type Alias struct { type Alias struct {
ID uint `gorm:"primary_key"` ID uint `gorm:"primary_key"`
Type uint8 `gorm:"not null; index:idx_alias_type_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"` UserID string `gorm:"not null; index:idx_alias_user"`
Key string `gorm:"not null; index:idx_alias_type_key"` Key string `gorm:"not null; index:idx_alias_type_key"`
Value string `gorm:"not null"` Value string `gorm:"not null"`

View File

@ -35,6 +35,7 @@ const UnknownSummaryKey = "unknown"
type Summary struct { type Summary struct {
ID uint `json:"-" gorm:"primary_key"` 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"` 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"` 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"` 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 { type SummaryItem struct {
ID uint `json:"-" gorm:"primary_key"` ID uint `json:"-" gorm:"primary_key"`
Summary *Summary `json:"-" gorm:"not null"`
SummaryID uint `json:"-"` SummaryID uint `json:"-"`
Type uint8 `json:"-"` Type uint8 `json:"-"`
Key string `json:"key"` Key string `json:"key"`

View File

@ -6,7 +6,7 @@ type User struct {
Password string `json:"-"` Password string `json:"-"`
CreatedAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"` CreatedAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP"`
LastLoggedInAt 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 { type Login struct {

View File

@ -1 +1 @@
1.14.0 1.14.1