Skip to content

Commit

Permalink
fix: transactions queries on v1.11 (#488)
Browse files Browse the repository at this point in the history
* chore: fix transactions query

* fix: tx queries
  • Loading branch information
gfyrag authored Apr 4, 2024
1 parent 25e147c commit acbd8bd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/storage/sqlstorage/store_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestStore(t *testing.T) {
{name: "GetBalancesAggregated", fn: testGetBalancesAggregated},
{name: "GetBalancesAggregatedByAccount", fn: testGetBalancesAggregatedByAccount},
{name: "CreateIK", fn: testIKS},
{name: "GetTransactionsByAccount", fn: testGetTransactionsByAccount},
} {
t.Run(fmt.Sprintf("%s/%s-singleInstance", ledgertesting.StorageDriverName(), tf.name), runTest((tf)))
}
Expand Down
22 changes: 14 additions & 8 deletions pkg/storage/sqlstorage/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,22 @@ func (s *Store) buildTransactionsQuery(flavor Flavor, p ledger.TransactionsQuery
sb.Where(s.schema.Table("use_account") + "(postings, " + arg + ")")
} else {
// new wildcard handling
dst := strings.Split(account, ":")
sb.Where(fmt.Sprintf("(jsonb_array_length(postings.destination) = %d OR jsonb_array_length(postings.source) = %d)", len(dst), len(dst)))
for i, segment := range dst {
if segment == ".*" || segment == "*" || segment == "" {
continue
ands := make([]string, 0)
for _, column := range []string{"source", "destination"} {
forColumn := make([]string, 0)
forColumn = append(forColumn, fmt.Sprintf("(jsonb_array_length(postings.%s) = %d)", column, len(strings.Split(account, ":"))))
for i, segment := range strings.Split(account, ":") {
if segment == ".*" || segment == "*" || segment == "" {
continue
}

arg := sb.Args.Add(segment)
forColumn = append(forColumn, fmt.Sprintf("postings.%s @@ ('$[%d] == \"' || %s::text || '\"')::jsonpath", column, i, arg))
}

arg := sb.Args.Add(segment)
sb.Where(fmt.Sprintf("(postings.source @@ ('$[%d] == \"' || %s::text || '\"')::jsonpath OR postings.destination @@ ('$[%d] == \"' || %s::text || '\"')::jsonpath)", i, arg, i, arg))
ands = append(ands, sb.And(forColumn...))
}

sb.Where(sb.Or(ands...))
}
t.AccountFilter = account
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/storage/sqlstorage/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,28 @@ func testTransactionsQueryAddress(t *testing.T, store *sqlstorage.Store) {
assert.Equal(t, cursor.Data[0].ID, tx5.ID)
})
}

func testGetTransactionsByAccount(t *testing.T, store *sqlstorage.Store) {
now := time.Now()
err := store.Commit(context.Background(), core.ExpandedTransaction{
Transaction: core.Transaction{
TransactionData: core.TransactionData{
Postings: core.Postings{
{
Source: "a:b:c",
Destination: "d:e:f",
Amount: core.NewMonetaryInt(10),
Asset: "USD",
},
},
Timestamp: now,
},
ID: 0,
},
})
require.NoError(t, err)

txs, err := store.GetTransactions(context.Background(), *ledger.NewTransactionsQuery().WithAccountFilter("a:e:c"))
require.NoError(t, err)
require.Empty(t, txs.Data)
}

0 comments on commit acbd8bd

Please sign in to comment.