From 4fb21164fa763cc7eb8864a1ca3ff1853ac551d2 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 5 Apr 2024 13:28:43 +0200 Subject: [PATCH] Fix: sorting transfer in rollback --- internal/storage/interface.go | 9 +++++++++ internal/storage/postgres/filters.go | 11 ++++++++--- internal/storage/postgres/rollback.go | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/storage/interface.go b/internal/storage/interface.go index cf19219..39c127a 100644 --- a/internal/storage/interface.go +++ b/internal/storage/interface.go @@ -38,6 +38,8 @@ type FilterOptions struct { SortField string SortOrder storage.SortOrder + SortFields []string + MaxHeight uint64 HeightColumnName string Cursor uint64 @@ -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) { diff --git a/internal/storage/postgres/filters.go b/internal/storage/postgres/filters.go index 2f47efa..3befa7b 100644 --- a/internal/storage/postgres/filters.go +++ b/internal/storage/postgres/filters.go @@ -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 { @@ -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) diff --git a/internal/storage/postgres/rollback.go b/internal/storage/postgres/rollback.go index cafa06b..7438788 100644 --- a/internal/storage/postgres/rollback.go +++ b/internal/storage/postgres/rollback.go @@ -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