sqldb: simplify and fixup the existing invoice store schema and queries

This commit attempts to fix some issues with the invoice store's schema that we
couldn't foresee before the implementation was finished. This is safe as the
schema has not been instantiated yet outside of unit tests. Furthermore the
commit updates invoice store SQL queries according to fixes in the schema as
well as to prepare the higher level implementation in the upcoming commits.
This commit is contained in:
Andras Banki-Horvath
2023-11-22 17:23:23 +01:00
parent 3e0f98a75a
commit 6a360fb2e2
12 changed files with 781 additions and 937 deletions

View File

@@ -1,35 +1,41 @@
-- name: InsertInvoiceEvent :exec
-- name: OnInvoiceCreated :exec
INSERT INTO invoice_events (
created_at, invoice_id, htlc_id, set_id, event_type, event_metadata
added_at, event_type, invoice_id
) VALUES (
$1, $2, $3, $4, $5, $6
$1, 0, $2
);
-- name: SelectInvoiceEvents :many
SELECT *
FROM invoice_events
WHERE (
invoice_id = sqlc.narg('invoice_id') OR
sqlc.narg('invoice_id') IS NULL
) AND (
htlc_id = sqlc.narg('htlc_id') OR
sqlc.narg('htlc_id') IS NULL
) AND (
set_id = sqlc.narg('set_id') OR
sqlc.narg('set_id') IS NULL
) AND (
event_type = sqlc.narg('event_type') OR
sqlc.narg('event_type') IS NULL
) AND (
created_at >= sqlc.narg('created_after') OR
sqlc.narg('created_after') IS NULL
) AND (
created_at <= sqlc.narg('created_before') OR
sqlc.narg('created_before') IS NULL
)
LIMIT @num_limit OFFSET @num_offset;
-- name: OnInvoiceCanceled :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id
) VALUES (
$1, 1, $2
);
-- name: DeleteInvoiceEvents :exec
DELETE
FROM invoice_events
WHERE invoice_id = $1;
-- name: OnInvoiceSettled :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id
) VALUES (
$1, 2, $2
);
-- name: OnAMPSubInvoiceCreated :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id, set_id
) VALUES (
$1, 3, $2, $3
);
-- name: OnAMPSubInvoiceCanceled :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id, set_id
) VALUES (
$1, 4, $2, $3
);
-- name: OnAMPSubInvoiceSettled :exec
INSERT INTO invoice_events (
added_at, event_type, invoice_id, set_id
) VALUES (
$1, 5, $2, $3
);