diff --git a/migrations/20210411_drop_migrations_table.go b/migrations/20210411_drop_migrations_table.go index 2133dec..711f872 100644 --- a/migrations/20210411_drop_migrations_table.go +++ b/migrations/20210411_drop_migrations_table.go @@ -11,7 +11,7 @@ func init() { f := migrationFunc{ name: name, f: func(db *gorm.DB, cfg *config.Config) error { - if err := db.Migrator().DropTable("gorp_migrations"); err != nil { + if err := db.Migrator().DropTable("gorp_migrations"); err == nil { logbuch.Info("dropped table 'gorp_migrations'") } return nil diff --git a/migrations/20210806_remove_persisted_project_labels.go b/migrations/20210806_remove_persisted_project_labels.go new file mode 100644 index 0000000..5af6ced --- /dev/null +++ b/migrations/20210806_remove_persisted_project_labels.go @@ -0,0 +1,49 @@ +package migrations + +import ( + "github.com/emvi/logbuch" + "github.com/muety/wakapi/config" + "github.com/muety/wakapi/models" + "gorm.io/gorm" +) + +func init() { + const name = "20210806-remove_persisted_project_labels" + 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 + } + + rawDb, err := db.DB() + if err != nil { + logbuch.Error("failed to retrieve raw sql db instance") + return err + } + if _, err := rawDb.Exec("delete from summary_items where type = ?", models.SummaryLabel); err != nil { + logbuch.Error("failed to delete project label summary items") + return err + } + logbuch.Info("successfully deleted project label summary items") + + if err := db.Create(&models.KeyStringValue{ + Key: name, + Value: "done", + }).Error; err != nil { + return err + } + + return nil + }, + } + + registerPostMigration(f) +} diff --git a/models/summary.go b/models/summary.go index 67c83d8..8db9f68 100644 --- a/models/summary.go +++ b/models/summary.go @@ -30,7 +30,7 @@ type Summary struct { Editors SummaryItems `json:"editors" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` OperatingSystems SummaryItems `json:"operating_systems" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` Machines SummaryItems `json:"machines" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` - Labels SummaryItems `json:"labels" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"` + Labels SummaryItems `json:"labels" gorm:"-"` // labels are not persisted, but calculated at runtime, i.e. when summary is retrieved } type SummaryItems []*SummaryItem diff --git a/repositories/summary.go b/repositories/summary.go index 3b09426..01dc61a 100644 --- a/repositories/summary.go +++ b/repositories/summary.go @@ -23,7 +23,6 @@ func (r *SummaryRepository) GetAll() ([]*models.Summary, error) { Preload("Editors", "type = ?", models.SummaryEditor). Preload("OperatingSystems", "type = ?", models.SummaryOS). Preload("Machines", "type = ?", models.SummaryMachine). - Preload("Labels", "type = ?", models.SummaryLabel). Find(&summaries).Error; err != nil { return nil, err } @@ -49,7 +48,6 @@ func (r *SummaryRepository) GetByUserWithin(user *models.User, from, to time.Tim Preload("Editors", "type = ?", models.SummaryEditor). Preload("OperatingSystems", "type = ?", models.SummaryOS). Preload("Machines", "type = ?", models.SummaryMachine). - Preload("Labels", "type = ?", models.SummaryLabel). Find(&summaries).Error; err != nil { return nil, err } diff --git a/services/summary.go b/services/summary.go index a0005e2..524f16d 100644 --- a/services/summary.go +++ b/services/summary.go @@ -94,6 +94,7 @@ func (srv *SummaryService) Retrieve(from, to time.Time, user *models.User) (*mod if err != nil { return nil, err } + summary = srv.withProjectLabels(summary) return summary.Sorted(), nil } diff --git a/views/settings.tpl.html b/views/settings.tpl.html index 1e0fab0..3be74b0 100644 --- a/views/settings.tpl.html +++ b/views/settings.tpl.html @@ -212,7 +212,7 @@