From 8c65da90316e7e7c0667078a0ea12aa496990bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 13 Mar 2022 08:56:35 +0100 Subject: [PATCH] chore: remove entity index again chore: add migration note --- migrations/20220313_index_generation_hint.go | 24 ++++++++++++++++++++ models/heartbeat.go | 2 +- repositories/heartbeat.go | 9 +------- version.txt | 2 +- 4 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 migrations/20220313_index_generation_hint.go diff --git a/migrations/20220313_index_generation_hint.go b/migrations/20220313_index_generation_hint.go new file mode 100644 index 00000000..60ae3334 --- /dev/null +++ b/migrations/20220313_index_generation_hint.go @@ -0,0 +1,24 @@ +package migrations + +import ( + "github.com/emvi/logbuch" + "github.com/muety/wakapi/config" + "gorm.io/gorm" +) + +func init() { + const name = "20220313-index_generation_hint" + f := migrationFunc{ + name: name, + f: func(db *gorm.DB, cfg *config.Config) error { + if hasRun(name, db) { + return nil + } + logbuch.Info("please note: the following migrations might take a few minutes, as column types are changed and new indexes are created, have some patience") + setHasRun(name, db) + return nil + }, + } + + registerPreMigration(f) +} diff --git a/models/heartbeat.go b/models/heartbeat.go index 6eee4c05..f1bdd6a9 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -12,7 +12,7 @@ type Heartbeat struct { ID uint64 `gorm:"primary_key" hash:"ignore"` User *User `json:"-" gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" hash:"ignore"` UserID string `json:"-" gorm:"not null; index:idx_time_user"` - Entity string `json:"entity" gorm:"not null; index:idx_entity"` + Entity string `json:"entity" gorm:"not null"` Type string `json:"type"` Category string `json:"category"` Project string `json:"project" gorm:"index:idx_project"` diff --git a/repositories/heartbeat.go b/repositories/heartbeat.go index 92382a6d..10b73cf8 100644 --- a/repositories/heartbeat.go +++ b/repositories/heartbeat.go @@ -1,7 +1,6 @@ package repositories import ( - "errors" "github.com/muety/wakapi/models" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -158,16 +157,10 @@ func (r *HeartbeatRepository) CountByUsers(users []*models.User) ([]*models.Coun } func (r HeartbeatRepository) GetEntitySetByUser(entityType uint8, user *models.User) ([]string, error) { - columns := []string{"project", "language", "editor", "operating_system", "machine"} - if int(entityType) >= len(columns) { - // invalid entity type - return nil, errors.New("invalid entity type") - } - var results []string if err := r.db. Model(&models.Heartbeat{}). - Distinct(columns[entityType]). + Distinct(models.GetEntityColumn(entityType)). Where(&models.Heartbeat{UserID: user.ID}). Find(&results).Error; err != nil { return nil, err diff --git a/version.txt b/version.txt index 04761555..ecf00d90 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2.2.4 \ No newline at end of file +2.2.5 \ No newline at end of file