Skip to content

Commit 6e1d50a

Browse files
Evanadecaro
authored andcommitted
feat(db): support SearchDirection in QueryTransactionsParams
QueryTransactions previously hard-coded ORDER BY stored_at ASC, making it impossible for callers to request newest-first ordering. Add a *SearchDirection field (pointer) to QueryTransactionsParams. When nil (zero value), the query defaults to ASC for backward compatibility with all existing callers. Set explicitly to &FromLast for descending or &FromBeginning for ascending. This is consistent with the existing SearchDirection field in QueryMovementsParams, while preserving backward compatibility. Signed-off-by: Evan <evanyan@sign.global>
1 parent 2b0ba32 commit 6e1d50a

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

token/services/storage/db/dbtest/transactions.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ func TAllowsSameTxID(t *testing.T, db driver3.TokenTransactionStore) {
547547
require.NoError(t, w.AddTransaction(ctx, tr2))
548548
require.NoError(t, w.Commit())
549549

550+
// Default (nil SearchDirection): ASC — tr1 first, tr2 second
550551
txs := getTransactions(t, db, driver3.QueryTransactionsParams{})
551552
assert.Len(t, txs, 2)
552553
assertTxEqual(t, &tr1, txs[0])
@@ -822,6 +823,30 @@ func TTransactionQueries(t *testing.T, db driver3.TokenTransactionStore) {
822823
assert.Len(t, res, tc.expectedLen, fmt.Sprintf("params: %v", tc.params))
823824
})
824825
}
826+
827+
// SearchDirection: redemptions have distinct stored_at (now-1day vs now+1day)
828+
fromBeginning := driver3.FromBeginning
829+
fromLast := driver3.FromLast
830+
t.Run("SearchDirection ASC", func(t *testing.T) {
831+
res := getTransactions(t, db, driver3.QueryTransactionsParams{
832+
ActionTypes: []driver3.ActionType{driver3.Redeem},
833+
Statuses: []driver3.TxStatus{driver3.Confirmed},
834+
SearchDirection: &fromBeginning,
835+
})
836+
require.Len(t, res, 2)
837+
assert.Equal(t, "8", res[0].TxID, "ASC: older redemption first")
838+
assert.Equal(t, "10", res[1].TxID, "ASC: newer redemption second")
839+
})
840+
t.Run("SearchDirection DESC", func(t *testing.T) {
841+
res := getTransactions(t, db, driver3.QueryTransactionsParams{
842+
ActionTypes: []driver3.ActionType{driver3.Redeem},
843+
Statuses: []driver3.TxStatus{driver3.Confirmed},
844+
SearchDirection: &fromLast,
845+
})
846+
require.Len(t, res, 2)
847+
assert.Equal(t, "10", res[0].TxID, "DESC: newer redemption first")
848+
assert.Equal(t, "8", res[1].TxID, "DESC: older redemption second")
849+
})
825850
}
826851

827852
func getTransactions(t *testing.T, db driver3.TokenTransactionStore, params driver3.QueryTransactionsParams) []*driver3.TransactionRecord {

token/services/storage/db/driver/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ type QueryTransactionsParams struct {
237237
// TokenTypes is the list of token types to accept
238238
// If empty, any token type is accepted
239239
TokenTypes []token2.Type
240+
// SearchDirection is the direction of the search.
241+
// If nil, defaults to FromBeginning (ascending by stored_at) for backward compatibility.
242+
// Set explicitly to override: &FromLast for descending, &FromBeginning for ascending.
243+
SearchDirection *SearchDirection
240244
}
241245

242246
// QueryValidationRecordsParams defines the parameters for querying validation records.

token/services/storage/db/sql/common/transactions.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ func orderBy(f common3.FieldName, direction driver4.SearchDirection) _select.Ord
9898
return q.Desc(f)
9999
}
100100

101+
// transactionOrderBy returns the ORDER BY clause for transaction queries.
102+
// If direction is nil, it defaults to ASC (FromBeginning) for backward compatibility.
103+
func transactionOrderBy(f common3.FieldName, direction *driver4.SearchDirection) _select.OrderBy {
104+
if direction != nil {
105+
return orderBy(f, *direction)
106+
}
107+
108+
return q.Asc(f)
109+
}
110+
101111
func (db *TransactionStore) QueryMovements(ctx context.Context, params driver4.QueryMovementsParams) (res []*driver4.MovementRecord, err error) {
102112
movementsTable, requestsTable := q.Table(db.table.Movements), q.Table(db.table.Requests)
103113
query, args := q.Select().
@@ -146,7 +156,7 @@ func (db *TransactionStore) QueryTransactions(ctx context.Context, params driver
146156
cond.Cmp(transactionsTable.Field("tx_id"), "=", requestsTable.Field("tx_id"))),
147157
).
148158
Where(HasTransactionParams(params, transactionsTable)).
149-
OrderBy(q.Asc(common3.FieldName("stored_at"))).
159+
OrderBy(transactionOrderBy("stored_at", params.SearchDirection)).
150160
Paginated(pagination).
151161
FormatPaginated(db.ci, db.pi)
152162

0 commit comments

Comments
 (0)