paymentsdb: implement FetchInFlightPayments for sql backend

This commit is contained in:
ziggie committed 2026-02-25 08:02:16 +01:00
1 parent 82ee78c5ef
commit 969b00e4a5
5 files changed
+288 -40

No files matched your search

+7 -9
View File
@@ -222,18 +222,16 @@ func (r FetchPaymentRow) GetPaymentIntent() PaymentIntent {
}
}
// GetPayment returns the Payment associated with this interface.
//
// NOTE: This method is part of the PaymentAndIntent interface.
func (r FetchPaymentsByIDsRow) GetPayment() Payment {
return r.Payment
return Payment{
ID: r.ID,
AmountMsat: r.AmountMsat,
CreatedAt: r.CreatedAt,
PaymentIdentifier: r.PaymentIdentifier,
FailReason: r.FailReason,
}
}
// GetPaymentIntent returns the PaymentIntent associated with this payment.
// If the payment has no intent (IntentType is NULL), this returns a zero-value
// PaymentIntent.
//
// NOTE: This method is part of the PaymentAndIntent interface.
func (r FetchPaymentsByIDsRow) GetPaymentIntent() PaymentIntent {
if !r.IntentType.Valid {
return PaymentIntent{}
+35 -15
View File
@@ -115,12 +115,20 @@ WHERE NOT EXISTS (
SELECT 1 FROM payment_htlc_attempt_resolutions hr
WHERE hr.attempt_index = ha.attempt_index
)
AND ha.attempt_index > $1
ORDER BY ha.attempt_index ASC
LIMIT $2
`
// Fetch all inflight attempts across all payments
func (q *Queries) FetchAllInflightAttempts(ctx context.Context) ([]PaymentHtlcAttempt, error) {
rows, err := q.db.QueryContext(ctx, fetchAllInflightAttempts)
type FetchAllInflightAttemptsParams struct {
AttemptIndex int64
Limit int32
}
// Fetch all inflight attempts with their payment data using pagination.
// Returns attempt data joined with payment and intent data to avoid separate queries.
func (q *Queries) FetchAllInflightAttempts(ctx context.Context, arg FetchAllInflightAttemptsParams) ([]PaymentHtlcAttempt, error) {
rows, err := q.db.QueryContext(ctx, fetchAllInflightAttempts, arg.AttemptIndex, arg.Limit)
if err != nil {
return nil, err
}
@@ -522,20 +530,32 @@ func (q *Queries) FetchPaymentLevelFirstHopCustomRecords(ctx context.Context, pa
const fetchPaymentsByIDs = `-- name: FetchPaymentsByIDs :many
SELECT
p.id, p.amount_msat, p.created_at, p.payment_identifier, p.fail_reason,
i.intent_type AS "intent_type",
i.intent_payload AS "intent_payload"
p.id,
p.amount_msat,
p.created_at,
p.payment_identifier,
p.fail_reason,
pi.intent_type,
pi.intent_payload
FROM payments p
LEFT JOIN payment_intents i ON i.payment_id = p.id
LEFT JOIN payment_intents pi ON pi.payment_id = p.id
WHERE p.id IN (/*SLICE:payment_ids*/?)
ORDER BY p.id ASC
`
type FetchPaymentsByIDsRow struct {
Payment Payment
IntentType sql.NullInt16
IntentPayload []byte
ID int64
AmountMsat int64
CreatedAt time.Time
PaymentIdentifier []byte
FailReason sql.NullInt32
IntentType sql.NullInt16
IntentPayload []byte
}
// Batch fetch payment and intent data for a set of payment IDs.
// Used to avoid fetching redundant payment data when processing multiple
// attempts for the same payment.
func (q *Queries) FetchPaymentsByIDs(ctx context.Context, paymentIds []int64) ([]FetchPaymentsByIDsRow, error) {
query := fetchPaymentsByIDs
var queryParams []interface{}
@@ -556,11 +576,11 @@ func (q *Queries) FetchPaymentsByIDs(ctx context.Context, paymentIds []int64) ([
for rows.Next() {
var i FetchPaymentsByIDsRow
if err := rows.Scan(
&i.Payment.ID,
&i.Payment.AmountMsat,
&i.Payment.CreatedAt,
&i.Payment.PaymentIdentifier,
&i.Payment.FailReason,
&i.ID,
&i.AmountMsat,
&i.CreatedAt,
&i.PaymentIdentifier,
&i.FailReason,
&i.IntentType,
&i.IntentPayload,
); err != nil {
+6 -2
View File
@@ -38,8 +38,9 @@ type Querier interface {
FailPayment(ctx context.Context, arg FailPaymentParams) (sql.Result, error)
FetchAMPSubInvoiceHTLCs(ctx context.Context, arg FetchAMPSubInvoiceHTLCsParams) ([]FetchAMPSubInvoiceHTLCsRow, error)
FetchAMPSubInvoices(ctx context.Context, arg FetchAMPSubInvoicesParams) ([]AmpSubInvoice, error)
// Fetch all inflight attempts across all payments
FetchAllInflightAttempts(ctx context.Context) ([]PaymentHtlcAttempt, error)
// Fetch all inflight attempts with their payment data using pagination.
// Returns attempt data joined with payment and intent data to avoid separate queries.
FetchAllInflightAttempts(ctx context.Context, arg FetchAllInflightAttemptsParams) ([]PaymentHtlcAttempt, error)
FetchHopLevelCustomRecords(ctx context.Context, hopIds []int64) ([]PaymentHopCustomRecord, error)
FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices []int64) ([]FetchHopsForAttemptsRow, error)
// Batch query to fetch only HTLC resolution status for multiple payments.
@@ -49,6 +50,9 @@ type Querier interface {
FetchHtlcAttemptsForPayments(ctx context.Context, paymentIds []int64) ([]FetchHtlcAttemptsForPaymentsRow, error)
FetchPayment(ctx context.Context, paymentIdentifier []byte) (FetchPaymentRow, error)
FetchPaymentLevelFirstHopCustomRecords(ctx context.Context, paymentIds []int64) ([]PaymentFirstHopCustomRecord, error)
// Batch fetch payment and intent data for a set of payment IDs.
// Used to avoid fetching redundant payment data when processing multiple
// attempts for the same payment.
FetchPaymentsByIDs(ctx context.Context, paymentIds []int64) ([]FetchPaymentsByIDsRow, error)
// FetchPendingInvoices returns all invoices in a pending state (open or
// accepted). The invoices_state_idx index on the state column makes this a
+22 -11
View File
@@ -40,15 +40,6 @@ FROM payments p
LEFT JOIN payment_intents i ON i.payment_id = p.id
WHERE p.payment_identifier = $1;
-- name: FetchPaymentsByIDs :many
SELECT
sqlc.embed(p),
i.intent_type AS "intent_type",
i.intent_payload AS "intent_payload"
FROM payments p
LEFT JOIN payment_intents i ON i.payment_id = p.id
WHERE p.id IN (sqlc.slice('payment_ids')/*SLICE:payment_ids*/);
-- name: CountPayments :one
SELECT COUNT(*) FROM payments;
@@ -86,8 +77,26 @@ FROM payment_htlc_attempts ha
LEFT JOIN payment_htlc_attempt_resolutions hr ON hr.attempt_index = ha.attempt_index
WHERE ha.payment_id IN (sqlc.slice('payment_ids')/*SLICE:payment_ids*/);
-- name: FetchPaymentsByIDs :many
-- Batch fetch payment and intent data for a set of payment IDs.
-- Used to avoid fetching redundant payment data when processing multiple
-- attempts for the same payment.
SELECT
p.id,
p.amount_msat,
p.created_at,
p.payment_identifier,
p.fail_reason,
pi.intent_type,
pi.intent_payload
FROM payments p
LEFT JOIN payment_intents pi ON pi.payment_id = p.id
WHERE p.id IN (sqlc.slice('payment_ids')/*SLICE:payment_ids*/)
ORDER BY p.id ASC;
-- name: FetchAllInflightAttempts :many
-- Fetch all inflight attempts across all payments
-- Fetch all inflight attempts with their payment data using pagination.
-- Returns attempt data joined with payment and intent data to avoid separate queries.
SELECT
ha.id,
ha.attempt_index,
@@ -104,7 +113,9 @@ WHERE NOT EXISTS (
SELECT 1 FROM payment_htlc_attempt_resolutions hr
WHERE hr.attempt_index = ha.attempt_index
)
ORDER BY ha.attempt_index ASC;
AND ha.attempt_index > $1
ORDER BY ha.attempt_index ASC
LIMIT $2;
-- name: FetchHopsForAttempts :many
SELECT