From b7974543d853694af88db6a1f83d1fdfa945a06b Mon Sep 17 00:00:00 2001 From: Molly Karcher Date: Tue, 19 Sep 2023 17:35:59 -0400 Subject: [PATCH] Merge pull request #5060 from tamirms/fix-payments-query services/horizon/internal/db2/history: fix payments query (cherry picked from commit 4391e0b1989a428cd90b34abd729ee57728504db) --- services/horizon/internal/db2/history/operation.go | 3 ++- .../db2/history/operation_batch_insert_builder.go | 11 ++++++++--- .../horizon/internal/db2/history/operation_test.go | 9 +++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/services/horizon/internal/db2/history/operation.go b/services/horizon/internal/db2/history/operation.go index 94064e8f28..d0dbfffc26 100644 --- a/services/horizon/internal/db2/history/operation.go +++ b/services/horizon/internal/db2/history/operation.go @@ -8,6 +8,7 @@ import ( "text/template" sq "github.com/Masterminds/squirrel" + "github.com/stellar/go/services/horizon/internal/db2" "github.com/stellar/go/support/errors" "github.com/stellar/go/toid" @@ -394,7 +395,7 @@ var selectOperation = sq.Select( "hop.details, " + "hop.source_account, " + "hop.source_account_muxed, " + - "hop.is_payment, " + + "COALESCE(hop.is_payment, false) as is_payment, " + "ht.transaction_hash, " + "ht.tx_result, " + "COALESCE(ht.successful, true) as transaction_successful"). diff --git a/services/horizon/internal/db2/history/operation_batch_insert_builder.go b/services/horizon/internal/db2/history/operation_batch_insert_builder.go index b2a9db6c88..d44aeec193 100644 --- a/services/horizon/internal/db2/history/operation_batch_insert_builder.go +++ b/services/horizon/internal/db2/history/operation_batch_insert_builder.go @@ -4,6 +4,7 @@ import ( "context" "github.com/guregu/null" + "github.com/stellar/go/support/db" "github.com/stellar/go/xdr" ) @@ -52,7 +53,7 @@ func (i *operationBatchInsertBuilder) Add( sourceAccountMuxed null.String, isPayment bool, ) error { - return i.builder.Row(ctx, map[string]interface{}{ + row := map[string]interface{}{ "id": id, "transaction_id": transactionID, "application_order": applicationOrder, @@ -60,8 +61,12 @@ func (i *operationBatchInsertBuilder) Add( "details": details, "source_account": sourceAccount, "source_account_muxed": sourceAccountMuxed, - "is_payment": isPayment, - }) + "is_payment": nil, + } + if isPayment { + row["is_payment"] = true + } + return i.builder.Row(ctx, row) } diff --git a/services/horizon/internal/db2/history/operation_test.go b/services/horizon/internal/db2/history/operation_test.go index 480ec9c837..5ba24d3e31 100644 --- a/services/horizon/internal/db2/history/operation_test.go +++ b/services/horizon/internal/db2/history/operation_test.go @@ -5,6 +5,7 @@ import ( sq "github.com/Masterminds/squirrel" "github.com/guregu/null" + "github.com/stellar/go/services/horizon/internal/db2" "github.com/stellar/go/services/horizon/internal/test" "github.com/stellar/go/toid" @@ -172,7 +173,7 @@ func TestOperationQueryBuilder(t *testing.T) { tt.Assert.NoError(err) // Operations for account queries will use hopp.history_operation_id in their predicates. - want := "SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, hop.is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id JOIN history_operation_participants hopp ON hopp.history_operation_id = hop.id WHERE hopp.history_account_id = ? AND hopp.history_operation_id > ? ORDER BY hopp.history_operation_id asc LIMIT 10" + want := "SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, COALESCE(hop.is_payment, false) as is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id JOIN history_operation_participants hopp ON hopp.history_operation_id = hop.id WHERE hopp.history_account_id = ? AND hopp.history_operation_id > ? ORDER BY hopp.history_operation_id asc LIMIT 10" tt.Assert.EqualValues(want, got) opsQ = q.Operations().ForLedger(tt.Ctx, 2).Page(db2.PageQuery{Cursor: "8589938689", Order: "asc", Limit: 10}) @@ -181,7 +182,7 @@ func TestOperationQueryBuilder(t *testing.T) { tt.Assert.NoError(err) // Other operation queries will use hop.id in their predicates. - want = "SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, hop.is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id WHERE hop.id >= ? AND hop.id < ? AND hop.id > ? ORDER BY hop.id asc LIMIT 10" + want = "SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, COALESCE(hop.is_payment, false) as is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id WHERE hop.id >= ? AND hop.id < ? AND hop.id > ? ORDER BY hop.id asc LIMIT 10" tt.Assert.EqualValues(want, got) } @@ -243,7 +244,7 @@ func TestOperationIncludeFailed(t *testing.T) { sql, _, err := query.sql.ToSql() tt.Assert.NoError(err) - tt.Assert.Equal("SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, hop.is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id JOIN history_operation_participants hopp ON hopp.history_operation_id = hop.id WHERE hopp.history_account_id = ?", sql) + tt.Assert.Equal("SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, COALESCE(hop.is_payment, false) as is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id JOIN history_operation_participants hopp ON hopp.history_operation_id = hop.id WHERE hopp.history_account_id = ?", sql) } // TestPaymentsSuccessfulOnly tests if default query returns payments in @@ -306,7 +307,7 @@ func TestPaymentsIncludeFailed(t *testing.T) { sql, _, err := query.sql.ToSql() tt.Assert.NoError(err) - tt.Assert.Equal("SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, hop.is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id JOIN history_operation_participants hopp ON hopp.history_operation_id = hop.id WHERE (hop.type IN (?,?,?,?,?) OR hop.is_payment = ?) AND hopp.history_account_id = ?", sql) + tt.Assert.Equal("SELECT hop.id, hop.transaction_id, hop.application_order, hop.type, hop.details, hop.source_account, hop.source_account_muxed, COALESCE(hop.is_payment, false) as is_payment, ht.transaction_hash, ht.tx_result, COALESCE(ht.successful, true) as transaction_successful FROM history_operations hop LEFT JOIN history_transactions ht ON ht.id = hop.transaction_id JOIN history_operation_participants hopp ON hopp.history_operation_id = hop.id WHERE (hop.type IN (?,?,?,?,?) OR hop.is_payment = ?) AND hopp.history_account_id = ?", sql) } func TestExtraChecksOperationsTransactionSuccessfulTrueResultFalse(t *testing.T) {