Skip to content

Commit 664bc61

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 to QueryTransactionsParams, consistent with the existing field in QueryMovementsParams. The zero value (FromLast) produces DESC ordering, matching the convention. Signed-off-by: Evan <evanyan@sign.global>
1 parent a75e3ad commit 664bc61

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func TTransaction(t *testing.T, db driver3.TokenTransactionStore) {
308308

309309
// get all except last year's
310310
t1 := time.Now().Add(time.Second * 3)
311-
it, err := db.QueryTransactions(ctx, driver3.QueryTransactionsParams{From: &t0, To: &t1}, nil)
311+
it, err := db.QueryTransactions(ctx, driver3.QueryTransactionsParams{From: &t0, To: &t1, SearchDirection: driver3.FromBeginning}, nil)
312312
require.NoError(t, err)
313313
for _, exp := range txs {
314314
act, err := it.Items.Next()
@@ -547,7 +547,8 @@ func TAllowsSameTxID(t *testing.T, db driver3.TokenTransactionStore) {
547547
require.NoError(t, w.AddTransaction(ctx, tr2))
548548
require.NoError(t, w.Commit())
549549

550-
txs := getTransactions(t, db, driver3.QueryTransactionsParams{})
550+
// Explicit ASC: tr1 first, tr2 second
551+
txs := getTransactions(t, db, driver3.QueryTransactionsParams{SearchDirection: driver3.FromBeginning})
551552
assert.Len(t, txs, 2)
552553
assertTxEqual(t, &tr1, txs[0])
553554
assertTxEqual(t, &tr2, txs[1])
@@ -822,6 +823,28 @@ 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+
t.Run("SearchDirection ASC", func(t *testing.T) {
829+
res := getTransactions(t, db, driver3.QueryTransactionsParams{
830+
ActionTypes: []driver3.ActionType{driver3.Redeem},
831+
Statuses: []driver3.TxStatus{driver3.Confirmed},
832+
SearchDirection: driver3.FromBeginning,
833+
})
834+
require.Len(t, res, 2)
835+
assert.Equal(t, "8", res[0].TxID, "ASC: older redemption first")
836+
assert.Equal(t, "10", res[1].TxID, "ASC: newer redemption second")
837+
})
838+
t.Run("SearchDirection DESC", func(t *testing.T) {
839+
res := getTransactions(t, db, driver3.QueryTransactionsParams{
840+
ActionTypes: []driver3.ActionType{driver3.Redeem},
841+
Statuses: []driver3.TxStatus{driver3.Confirmed},
842+
SearchDirection: driver3.FromLast,
843+
})
844+
require.Len(t, res, 2)
845+
assert.Equal(t, "10", res[0].TxID, "DESC: newer redemption first")
846+
assert.Equal(t, "8", res[1].TxID, "DESC: older redemption second")
847+
})
825848
}
826849

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ 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+
// Default (zero value) is FromLast (descending by stored_at).
242+
SearchDirection SearchDirection
240243
}
241244

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (db *TransactionStore) QueryTransactions(ctx context.Context, params driver
146146
cond.Cmp(transactionsTable.Field("tx_id"), "=", requestsTable.Field("tx_id"))),
147147
).
148148
Where(HasTransactionParams(params, transactionsTable)).
149-
OrderBy(q.Asc(common3.FieldName("stored_at"))).
149+
OrderBy(orderBy("stored_at", params.SearchDirection)).
150150
Paginated(pagination).
151151
FormatPaginated(db.ci, db.pi)
152152

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestQueryTransactions(t *testing.T, store transactionsStoreConstructor) {
116116
mockDB.
117117
ExpectQuery("SELECT TRANSACTIONS.tx_id, action_type, sender_eid, recipient_eid, token_type, amount, " +
118118
"REQUESTS.status, REQUESTS.application_metadata, REQUESTS.public_metadata, stored_at " +
119-
"FROM TRANSACTIONS LEFT JOIN REQUESTS ON TRANSACTIONS.tx_id = REQUESTS.tx_id ORDER BY stored_at ASC").
119+
"FROM TRANSACTIONS LEFT JOIN REQUESTS ON TRANSACTIONS.tx_id = REQUESTS.tx_id ORDER BY stored_at DESC").
120120
WillReturnRows(mockDB.NewRows([]string{"tx_id", "action_type", "sender_eid", "recipient_eid", "token_type", "amount", "status", "application_metadata", "public_metadata", "stored_at"}).AddRow(output...))
121121

122122
info, err := store(db).QueryTransactions(t.Context(),

0 commit comments

Comments
 (0)