From b870861298d91883169f705da39687102d2407fb Mon Sep 17 00:00:00 2001 From: Ragot Geoffrey Date: Thu, 4 Apr 2024 22:18:22 +0200 Subject: [PATCH] Revert "fix: transaction queries on ledger v1.10 (#485)" This reverts commit 930d7b3728085f40867d5c79a789b69436980322. --- pkg/storage/sqlstorage/store_ledger_test.go | 1 - pkg/storage/sqlstorage/transactions.go | 22 +++++++----------- pkg/storage/sqlstorage/transactions_test.go | 25 --------------------- 3 files changed, 8 insertions(+), 40 deletions(-) diff --git a/pkg/storage/sqlstorage/store_ledger_test.go b/pkg/storage/sqlstorage/store_ledger_test.go index 01ea008ec..67c2cf5d1 100644 --- a/pkg/storage/sqlstorage/store_ledger_test.go +++ b/pkg/storage/sqlstorage/store_ledger_test.go @@ -93,7 +93,6 @@ 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))) } diff --git a/pkg/storage/sqlstorage/transactions.go b/pkg/storage/sqlstorage/transactions.go index a16d2611b..4f12f85a3 100644 --- a/pkg/storage/sqlstorage/transactions.go +++ b/pkg/storage/sqlstorage/transactions.go @@ -99,22 +99,16 @@ func (s *Store) buildTransactionsQuery(flavor Flavor, p ledger.TransactionsQuery sb.Where(s.schema.Table("use_account") + "(postings, " + arg + ")") } else { // new wildcard handling - 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) - sb.Where(fmt.Sprintf("postings.%s @@ ('$[%d] == \"' || %s::text || '\"')::jsonpath", column, i, arg)) + 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 = append(ands, sb.And(forColumn...)) - } - sb.Where(sb.Or(ands...)) + 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)) + } } t.AccountFilter = account } diff --git a/pkg/storage/sqlstorage/transactions_test.go b/pkg/storage/sqlstorage/transactions_test.go index 98dc31b58..3fa52f019 100644 --- a/pkg/storage/sqlstorage/transactions_test.go +++ b/pkg/storage/sqlstorage/transactions_test.go @@ -523,28 +523,3 @@ 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) -}