diff --git a/payments/db/sql_store.go b/payments/db/sql_store.go index 4e494c802..d2500d62a 100644 --- a/payments/db/sql_store.go +++ b/payments/db/sql_store.go @@ -49,6 +49,7 @@ type SQLQueries interface { CountPayments(ctx context.Context) (int64, error) FetchHtlcAttemptsForPayments(ctx context.Context, paymentIDs []int64) ([]sqlc.FetchHtlcAttemptsForPaymentsRow, error) + FetchHtlcAttemptResolutionsForPayment(ctx context.Context, paymentID int64) ([]sql.NullInt32, error) FetchAllInflightAttempts(ctx context.Context) ([]sqlc.PaymentHtlcAttempt, error) FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices []int64) ([]sqlc.FetchHopsForAttemptsRow, error) diff --git a/sqldb/sqlc/payments.sql.go b/sqldb/sqlc/payments.sql.go index ae92aa146..dd135e3db 100644 --- a/sqldb/sqlc/payments.sql.go +++ b/sqldb/sqlc/payments.sql.go @@ -242,6 +242,39 @@ func (q *Queries) FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices [ return items, nil } +const fetchHtlcAttemptResolutionsForPayment = `-- name: FetchHtlcAttemptResolutionsForPayment :many +SELECT + hr.resolution_type +FROM payment_htlc_attempts ha +LEFT JOIN payment_htlc_attempt_resolutions hr ON hr.attempt_index = ha.attempt_index +WHERE ha.payment_id = $1 +ORDER BY ha.attempt_time ASC +` + +// Lightweight query to fetch only HTLC resolution status. +func (q *Queries) FetchHtlcAttemptResolutionsForPayment(ctx context.Context, paymentID int64) ([]sql.NullInt32, error) { + rows, err := q.db.QueryContext(ctx, fetchHtlcAttemptResolutionsForPayment, paymentID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []sql.NullInt32 + for rows.Next() { + var resolution_type sql.NullInt32 + if err := rows.Scan(&resolution_type); err != nil { + return nil, err + } + items = append(items, resolution_type) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const fetchHtlcAttemptsForPayments = `-- name: FetchHtlcAttemptsForPayments :many SELECT ha.id, diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index d1605a096..008624af4 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -38,6 +38,8 @@ type Querier interface { FetchAllInflightAttempts(ctx context.Context) ([]PaymentHtlcAttempt, error) FetchHopLevelCustomRecords(ctx context.Context, hopIds []int64) ([]PaymentHopCustomRecord, error) FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices []int64) ([]FetchHopsForAttemptsRow, error) + // Lightweight query to fetch only HTLC resolution status. + FetchHtlcAttemptResolutionsForPayment(ctx context.Context, paymentID int64) ([]sql.NullInt32, error) FetchHtlcAttemptsForPayments(ctx context.Context, paymentIds []int64) ([]FetchHtlcAttemptsForPaymentsRow, error) FetchPayment(ctx context.Context, paymentIdentifier []byte) (FetchPaymentRow, error) FetchPaymentLevelFirstHopCustomRecords(ctx context.Context, paymentIds []int64) ([]PaymentFirstHopCustomRecord, error) diff --git a/sqldb/sqlc/queries/payments.sql b/sqldb/sqlc/queries/payments.sql index a70631adb..b35919c0e 100644 --- a/sqldb/sqlc/queries/payments.sql +++ b/sqldb/sqlc/queries/payments.sql @@ -75,6 +75,15 @@ LEFT JOIN payment_htlc_attempt_resolutions hr ON hr.attempt_index = ha.attempt_i WHERE ha.payment_id IN (sqlc.slice('payment_ids')/*SLICE:payment_ids*/) ORDER BY ha.payment_id ASC, ha.attempt_time ASC; +-- name: FetchHtlcAttemptResolutionsForPayment :many +-- Lightweight query to fetch only HTLC resolution status. +SELECT + hr.resolution_type +FROM payment_htlc_attempts ha +LEFT JOIN payment_htlc_attempt_resolutions hr ON hr.attempt_index = ha.attempt_index +WHERE ha.payment_id = $1 +ORDER BY ha.attempt_time ASC; + -- name: FetchAllInflightAttempts :many -- Fetch all inflight attempts across all payments SELECT