Skip to content

Commit

Permalink
fix operations query
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Dec 11, 2023
1 parent 84787e8 commit ad3b941
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion services/horizon/internal/db2/history/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/guregu/null"

"github.com/stellar/go/support/db"
"github.com/stellar/go/xdr"
)
Expand Down Expand Up @@ -49,17 +50,20 @@ func (i *operationBatchInsertBuilder) Add(
sourceAccountMuxed null.String,
isPayment bool,
) error {
return i.builder.Row(map[string]interface{}{
row := map[string]interface{}{
"id": id,
"transaction_id": transactionID,
"application_order": applicationOrder,
"type": operationType,
"details": string(details),
"source_account": sourceAccount,
"source_account_muxed": sourceAccountMuxed,
"is_payment": isPayment,
})

"is_payment": nil,
}
if isPayment {
row["is_payment"] = true
}
return i.builder.Row(row)
}

func (i *operationBatchInsertBuilder) Exec(ctx context.Context, session db.SessionInterface) error {
Expand Down
8 changes: 4 additions & 4 deletions services/horizon/internal/db2/history/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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})
Expand All @@ -177,7 +177,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)
}

Expand Down Expand Up @@ -239,7 +239,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
Expand Down Expand Up @@ -302,7 +302,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) {
Expand Down

0 comments on commit ad3b941

Please sign in to comment.