diff --git a/main.go b/main.go index 6b00d69..119ee6e 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "time" "github.com/lpar/gzipped/v2" - "github.com/muety/wakapi/models" "github.com/muety/wakapi/routes/relay" "github.com/emvi/logbuch" @@ -269,18 +268,6 @@ func main() { middlewares.NewFileTypeFilterMiddleware([]string{".go"})(staticFileServer), ) - // Miscellaneous - // Pre-warm projects cache - if !config.IsDev() { - allUsers, err := userService.GetAll() - if err == nil { - logbuch.Info("pre-warming user project cache") - for _, u := range allUsers { - go heartbeatService.GetEntitySetByUser(models.SummaryProject, u) - } - } - } - // Listen HTTP listen(router) } diff --git a/migrations/20220319_add_user_project_idx.go b/migrations/20220319_add_user_project_idx.go new file mode 100644 index 0000000..ab38d0e --- /dev/null +++ b/migrations/20220319_add_user_project_idx.go @@ -0,0 +1,35 @@ +package migrations + +import ( + "fmt" + "github.com/emvi/logbuch" + "github.com/muety/wakapi/config" + "github.com/muety/wakapi/models" + "gorm.io/gorm" +) + +func init() { + const name = "20220319-add_user_project_idx" + f := migrationFunc{ + name: name, + f: func(db *gorm.DB, cfg *config.Config) error { + if hasRun(name, db) { + return nil + } + + idxName := "idx_user_project" + + if !db.Migrator().HasIndex(&models.Heartbeat{}, idxName) { + logbuch.Info("running migration '%s'", name) + if err := db.Exec(fmt.Sprintf("create index %s on heartbeats (user_id, project)", idxName)).Error; err != nil { + logbuch.Warn("failed to create %s", idxName) + } + } + + setHasRun(name, db) + return nil + }, + } + + registerPostMigration(f) +} diff --git a/models/heartbeat.go b/models/heartbeat.go index 71f8dc8..9922cfb 100644 --- a/models/heartbeat.go +++ b/models/heartbeat.go @@ -11,11 +11,11 @@ import ( 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"` + UserID string `json:"-" gorm:"not null; index:idx_time_user,idx_user_project"` // idx_user_project is for quickly fetching a user's project list (settings page) Entity string `json:"entity" gorm:"not null"` Type string `json:"type"` Category string `json:"category"` - Project string `json:"project" gorm:"index:idx_project"` + Project string `json:"project" gorm:"index:idx_project,idx_user_project"` Branch string `json:"branch" gorm:"index:idx_branch"` Language string `json:"language" gorm:"index:idx_language"` IsWrite bool `json:"is_write"`