mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Auto-migrate custom languages.
This commit is contained in:
parent
cdf158e2ac
commit
df7b2d4a33
16
main.go
16
main.go
@ -87,6 +87,9 @@ func main() {
|
|||||||
db.AutoMigrate(&models.User{})
|
db.AutoMigrate(&models.User{})
|
||||||
db.AutoMigrate(&models.Heartbeat{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")
|
db.AutoMigrate(&models.Heartbeat{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")
|
||||||
|
|
||||||
|
// Migrate custom languages
|
||||||
|
migrateLanguages(db, config)
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
heartbeatSrvc := &services.HeartbeatService{config, db}
|
heartbeatSrvc := &services.HeartbeatService{config, db}
|
||||||
userSrvc := &services.UserService{config, db}
|
userSrvc := &services.UserService{config, db}
|
||||||
@ -135,3 +138,16 @@ func main() {
|
|||||||
log.Printf("Listening on %+s\n", portString)
|
log.Printf("Listening on %+s\n", portString)
|
||||||
s.ListenAndServe()
|
s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func migrateLanguages(db *gorm.DB, cfg *models.Config) {
|
||||||
|
for k, v := range cfg.CustomLanguages {
|
||||||
|
result := db.Model(models.Heartbeat{}).
|
||||||
|
Where("language = ?", "").
|
||||||
|
Where("entity LIKE ?", "%."+k).
|
||||||
|
Updates(models.Heartbeat{Language: v})
|
||||||
|
if result.Error != nil {
|
||||||
|
log.Fatal(result.Error)
|
||||||
|
}
|
||||||
|
log.Printf("Migrated %+v rows for custom language %+s.\n", result.RowsAffected, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -14,14 +14,14 @@ type HeartbeatReqTime time.Time
|
|||||||
|
|
||||||
type Heartbeat struct {
|
type Heartbeat struct {
|
||||||
ID uint `gorm:"primary_key"`
|
ID uint `gorm:"primary_key"`
|
||||||
User *User `json:"-" gorm:"not null; index:idx_time_user"`
|
User *User `json:"-" gorm:"not null"`
|
||||||
UserID string `json:"-" gorm:"not null; index:idx_time_user"`
|
UserID string `json:"-" gorm:"not null; index:idx_time_user"`
|
||||||
Entity string `json:"entity" gorm:"not null"`
|
Entity string `json:"entity" gorm:"not null: index:idx_entity"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Category string `json:"category"`
|
Category string `json:"category"`
|
||||||
Project string `json:"project"`
|
Project string `json:"project"`
|
||||||
Branch string `json:"branch"`
|
Branch string `json:"branch"`
|
||||||
Language string `json:"language"`
|
Language string `json:"language" gorm:"index:idx_language"`
|
||||||
IsWrite bool `json:"is_write"`
|
IsWrite bool `json:"is_write"`
|
||||||
Editor string `json:"editor"`
|
Editor string `json:"editor"`
|
||||||
OperatingSystem string `json:"operating_system"`
|
OperatingSystem string `json:"operating_system"`
|
||||||
|
@ -2,7 +2,6 @@ package routes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@ -75,7 +74,6 @@ func (h *SummaryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|||||||
cachedSummary, ok := h.Cache.Get(cacheKey)
|
cachedSummary, ok := h.Cache.Get(cacheKey)
|
||||||
if !ok {
|
if !ok {
|
||||||
// Cache Miss
|
// Cache Miss
|
||||||
fmt.Println("Cache miss")
|
|
||||||
summary, err = h.SummarySrvc.GetSummary(from, to, user) // 'to' is always constant
|
summary, err = h.SummarySrvc.GetSummary(from, to, user) // 'to' is always constant
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user