chore: minor fix in latest migration

This commit is contained in:
Ferdinand Mütsch
2023-10-23 12:11:02 +02:00
parent a49fe28fe8
commit bc8e7f1234
2 changed files with 684 additions and 674 deletions

View File

@@ -1,8 +1,10 @@
package migrations
import (
"github.com/alitto/pond"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/utils"
"gorm.io/gorm"
)
@@ -20,26 +22,30 @@ func init() {
return err
}
wp := pond.New(utils.AllCPUs(), 0)
// this is the most inefficient way to perform the update, but i couldn't find a way to do this is a single query
for _, h := range heartbeats {
var latest models.Heartbeat
if err := db.
Where(&models.Heartbeat{UserID: h.UserID, Project: h.Project}).
Not("branch", "<<LAST_BRANCH>>").
Where("time < ?", h.Time).
Order("time desc").
First(&latest).Error; err != nil {
return err
}
if err := db.
Model(&models.Heartbeat{}).
Where("id", h.ID).
Update("branch", latest.Branch).
Error; err != nil {
return err
}
h := h
wp.Submit(func() {
var latest models.Heartbeat
if err := db.
Where(&models.Heartbeat{UserID: h.UserID, Project: h.Project}).
Not("branch", "<<LAST_BRANCH>>").
Where("time < ?", h.Time).
Order("time desc").
First(&latest).Error; err != nil {
return
}
db.
Model(&models.Heartbeat{}).
Where("id", h.ID).
Update("branch", latest.Branch)
})
}
wp.StopAndWait()
setHasRun(name, db)
return nil
},