fix: out of order when database is not sqlite3 (#6560)

pull/6907/head
j2rong4cn 2024-08-03 13:11:09 +08:00 committed by GitHub
parent af9c6afd25
commit 87caaf2459
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 14 additions and 9 deletions

View File

@ -34,6 +34,7 @@ func initSettings() {
// create or save setting
for i := range initialSettingItems {
item := &initialSettingItems[i]
item.Index = uint(i)
if item.PreDefault == "" {
item.PreDefault = item.Value
}

View File

@ -34,7 +34,7 @@ func GetMetas(pageIndex, pageSize int) (metas []model.Meta, count int64, err err
if err = metaDB.Count(&count).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get metas count")
}
if err = metaDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&metas).Error; err != nil {
if err = metaDB.Order(columnName("id")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&metas).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get find metas")
}
return metas, count, nil

View File

@ -49,7 +49,8 @@ func GetSettingItemsByGroup(group int) ([]model.SettingItem, error) {
func GetSettingItemsInGroups(groups []int) ([]model.SettingItem, error) {
var settingItems []model.SettingItem
if err := db.Where(fmt.Sprintf("%s in ?", columnName("group")), groups).Find(&settingItems).Error; err != nil {
err := db.Order(columnName("index")).Where(fmt.Sprintf("%s in ?", columnName("group")), groups).Find(&settingItems).Error
if err != nil {
return nil, errors.WithStack(err)
}
return settingItems, nil

View File

@ -2,7 +2,6 @@ package db
import (
"fmt"
"sort"
"github.com/alist-org/alist/v3/internal/model"
"github.com/pkg/errors"
@ -36,7 +35,7 @@ func GetStorages(pageIndex, pageSize int) ([]model.Storage, int64, error) {
return nil, 0, errors.Wrapf(err, "failed get storages count")
}
var storages []model.Storage
if err := storageDB.Order(columnName("order")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&storages).Error; err != nil {
if err := addStorageOrder(storageDB).Order(columnName("order")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&storages).Error; err != nil {
return nil, 0, errors.WithStack(err)
}
return storages, count, nil
@ -63,11 +62,9 @@ func GetStorageByMountPath(mountPath string) (*model.Storage, error) {
func GetEnabledStorages() ([]model.Storage, error) {
var storages []model.Storage
if err := db.Where(fmt.Sprintf("%s = ?", columnName("disabled")), false).Find(&storages).Error; err != nil {
err := addStorageOrder(db).Where(fmt.Sprintf("%s = ?", columnName("disabled")), false).Find(&storages).Error
if err != nil {
return nil, errors.WithStack(err)
}
sort.Slice(storages, func(i, j int) bool {
return storages[i].Order < storages[j].Order
})
return storages, nil
}

View File

@ -54,7 +54,7 @@ func GetUsers(pageIndex, pageSize int) (users []model.User, count int64, err err
if err := userDB.Count(&count).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get users count")
}
if err := userDB.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
if err := userDB.Order(columnName("id")).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&users).Error; err != nil {
return nil, 0, errors.Wrapf(err, "failed get find users")
}
return users, count, nil

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/alist-org/alist/v3/internal/conf"
"gorm.io/gorm"
)
func columnName(name string) string {
@ -12,3 +13,7 @@ func columnName(name string) string {
}
return fmt.Sprintf("`%s`", name)
}
func addStorageOrder(db *gorm.DB) *gorm.DB {
return db.Order(fmt.Sprintf("%s, %s", columnName("order"), columnName("id")))
}

View File

@ -29,6 +29,7 @@ type SettingItem struct {
Options string `json:"options"` // values for select
Group int `json:"group"` // use to group setting in frontend
Flag int `json:"flag"` // 0 = public, 1 = private, 2 = readonly, 3 = deprecated, etc.
Index uint `json:"index"`
}
func (s SettingItem) IsDeprecated() bool {