Skip to content

Commit

Permalink
fix: out of order when database is not sqlite3 (#6560)
Browse files Browse the repository at this point in the history
  • Loading branch information
j2rong4cn authored Aug 3, 2024
1 parent af9c6af commit 87caaf2
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/db/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/db/settingitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions internal/db/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package db

import (
"fmt"
"sort"

"github.com/alist-org/alist/v3/internal/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion internal/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/db/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/alist-org/alist/v3/internal/conf"
"gorm.io/gorm"
)

func columnName(name string) string {
Expand All @@ -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")))
}
1 change: 1 addition & 0 deletions internal/model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 87caaf2

Please sign in to comment.