Skip to content

Commit

Permalink
chore: remove entity index again
Browse files Browse the repository at this point in the history
chore: add migration note
  • Loading branch information
muety committed Mar 13, 2022
1 parent 647bf17 commit 8c65da9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
24 changes: 24 additions & 0 deletions migrations/20220313_index_generation_hint.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion models/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
9 changes: 1 addition & 8 deletions repositories/heartbeat.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package repositories

import (
"errors"
"github.com/muety/wakapi/models"
"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.4
2.2.5

0 comments on commit 8c65da9

Please sign in to comment.