mirror of
https://github.com/gotify/server.git
synced 2026-05-06 21:42:07 +08:00
feat: add sort_key to backend
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/gotify/server/v2/auth/password"
|
||||
"github.com/gotify/server/v2/mode"
|
||||
"github.com/gotify/server/v2/fracdex"
|
||||
"github.com/gotify/server/v2/model"
|
||||
"github.com/mattn/go-isatty"
|
||||
"gorm.io/driver/mysql"
|
||||
@@ -91,9 +95,46 @@ func New(dialect, connection, defaultUser, defaultPass string, strength int, cre
|
||||
db.Create(&model.User{Name: defaultUser, Pass: password.CreatePassword(defaultPass, strength), Admin: true})
|
||||
}
|
||||
|
||||
if err := db.Transaction(fillMissingSortKeys, &sql.TxOptions{Isolation: sql.LevelSerializable}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &GormDatabase{DB: db}, nil
|
||||
}
|
||||
|
||||
func fillMissingSortKeys(db *gorm.DB) error {
|
||||
missingSort := int64(0)
|
||||
if err := db.Model(new(model.Application)).Where("sort_key IS NULL OR sort_key = ''").Count(&missingSort).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if missingSort == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var apps []*model.Application
|
||||
if err := db.Order("user_id, sort_key, id ASC").Find(&apps).Error; err != nil && err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Migrating", len(apps), "application sort keys for")
|
||||
|
||||
sortKey := ""
|
||||
currentUser := uint(math.MaxUint)
|
||||
var err error
|
||||
for _, app := range apps {
|
||||
if currentUser != app.UserID {
|
||||
sortKey = ""
|
||||
currentUser = app.UserID
|
||||
}
|
||||
sortKey, err = fracdex.KeyBetween(sortKey, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
app.SortKey = sortKey
|
||||
}
|
||||
return db.Save(apps).Error
|
||||
}
|
||||
|
||||
func createDirectoryIfSqlite(dialect, connection string) {
|
||||
if dialect == "sqlite3" {
|
||||
if _, err := os.Stat(filepath.Dir(connection)); os.IsNotExist(err) {
|
||||
|
||||
Reference in New Issue
Block a user