Skip to content

Commit

Permalink
Fix: sorting transfer in rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Apr 5, 2024
1 parent 0916de9 commit 4fb2116
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
9 changes: 9 additions & 0 deletions internal/storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type FilterOptions struct {
SortField string
SortOrder storage.SortOrder

SortFields []string

MaxHeight uint64
HeightColumnName string
Cursor uint64
Expand Down Expand Up @@ -88,6 +90,13 @@ func WithDescSortByIdFilter() FilterOption {
}
}

// WithMultiSort -
func WithMultiSort(fields ...string) FilterOption {
return func(opt *FilterOptions) {
opt.SortFields = fields
}
}

// WithMaxHeight -
func WithMaxHeight(height uint64, columnName string) FilterOption {
return func(opt *FilterOptions) {
Expand Down
11 changes: 8 additions & 3 deletions internal/storage/postgres/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ func addSort(q *bun.SelectQuery, field string, order sdk.SortOrder) *bun.SelectQ
return q
}
if order == sdk.SortOrderAsc {
return q.OrderExpr("? asc", bun.Ident(field))
return q.OrderExpr("(?) asc", bun.Ident(field))
}
return q.OrderExpr("? desc", bun.Ident(field))
return q.OrderExpr("(?) desc", bun.Ident(field))
}

func optionsFilter(q *bun.SelectQuery, tableName string, opts ...storage.FilterOption) *bun.SelectQuery {
Expand All @@ -198,7 +198,12 @@ func optionsFilter(q *bun.SelectQuery, tableName string, opts ...storage.FilterO
}
q = addLimit(q, opt.Limit)
q = addOffset(q, opt.Offset)
q = addSort(q, opt.SortField, opt.SortOrder)

if len(opt.SortFields) > 0 {
q.Order(opt.SortFields...)
} else {
q = addSort(q, opt.SortField, opt.SortOrder)
}

if opt.MaxHeight > 0 {
q = q.Where("?.? <= ?", bun.Ident(tableName), bun.Safe(opt.HeightColumnName), opt.MaxHeight)
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/postgres/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ func (rm RollbackManager) rollbackTokenBalances(ctx context.Context, height uint
},
},
},
models.WithDescSortByIdFilter(),
models.WithLimitFilter(limit),
models.WithOffsetFilter(offset),
models.WithMultiSort("time desc", "id desc"),
)
if err != nil {
return err
Expand Down

0 comments on commit 4fb2116

Please sign in to comment.