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 committed 2024-03-01 10:08:07 +01:00
1 parent 3e0f98a75a
commit 6a360fb2e2
12 files changed
+781 -937

No files matched your search

+34 -28
View File
@@ -1,7 +1,17 @@
-- sequences contains all sequences used for invoices.
CREATE TABLE invoice_sequences (
name TEXT PRIMARY KEY,
current_value BIGINT NOT NULL
);
-- Initialize a sequence for the settled index used to track invoice settlement
-- to remain compatible with the legacy channeldb implementation.
INSERT INTO invoice_sequences(name, current_value) VALUES ('settle_index', 0);
-- invoices table contains all the information shared by all the invoice types.
CREATE TABLE IF NOT EXISTS invoices (
-- The id of the invoice. Translates to the AddIndex.
id INTEGER PRIMARY KEY,
id BIGINT PRIMARY KEY,
-- The hash for this invoice. The invoice hash will always identify that
-- invoice.
@@ -10,6 +20,14 @@ CREATE TABLE IF NOT EXISTS invoices (
-- The preimage for the hash in this invoice. Some invoices may have this
-- field empty, like unsettled hodl invoices or AMP invoices.
preimage BLOB,
-- If settled, the index is set to the current_value+1 of the settle_index
-- seuqence in the invoice_sequences table. If not settled, then it is set
-- to NULL.
settle_index BIGINT,
-- When the invoice was settled.
settled_at TIMESTAMP,
-- An optional memo to attach along with the invoice.
memo TEXT,
@@ -31,7 +49,12 @@ CREATE TABLE IF NOT EXISTS invoices (
-- The encoded payment request for this invoice. Some invoice types may
-- not have leave this empty, like Keysends.
payment_request TEXT UNIQUE,
payment_request TEXT,
-- Holds the hash of the payment request. This field is used to ensure that
-- there are no duplicates in the database. This trick is needed because
-- PostgreSQL has a limitiation of 2712 bytes for unique index rows.
payment_request_hash BLOB UNIQUE,
-- The invoice state.
state SMALLINT NOT NULL,
@@ -58,6 +81,7 @@ CREATE INDEX IF NOT EXISTS invoices_preimage_idx ON invoices(preimage);
CREATE INDEX IF NOT EXISTS invoices_payment_addr_idx ON invoices(payment_addr);
CREATE INDEX IF NOT EXISTS invoices_state_idx ON invoices(state);
CREATE INDEX IF NOT EXISTS invoices_created_at_idx ON invoices(created_at);
CREATE INDEX IF NOT EXISTS invoices_settled_at_idx ON invoices(settled_at);
-- invoice_features contains the feature bits of an invoice.
CREATE TABLE IF NOT EXISTS invoice_features (
@@ -65,7 +89,7 @@ CREATE TABLE IF NOT EXISTS invoice_features (
feature INTEGER NOT NULL,
-- The invoice id this feature belongs to.
invoice_id INTEGER NOT NULL REFERENCES invoices(id),
invoice_id BIGINT NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
-- The feature bit is unique per invoice.
UNIQUE (feature, invoice_id)
@@ -78,15 +102,15 @@ CREATE INDEX IF NOT EXISTS invoice_feature_invoice_id_idx ON invoice_features(in
CREATE TABLE IF NOT EXISTS invoice_htlcs (
-- The id for this htlc. Used in foreign keys instead of the
-- htlc_id/chan_id combination.
id INTEGER PRIMARY KEY,
id BIGINT PRIMARY KEY,
-- Short chan id indicating the htlc's origin. uint64 stored as text.
chan_id TEXT NOT NULL,
-- The uint64 htlc id. This field is a counter so it is safe to store it as
-- int64 in the database. The application layer must check that there is no
-- overflow when storing/loading this column.
htlc_id BIGINT NOT NULL,
-- Short chan id indicating the htlc's origin. uint64 stored as text.
chan_id TEXT NOT NULL,
-- The htlc's amount in millisatoshis.
amount_msat BIGINT NOT NULL,
@@ -110,10 +134,10 @@ CREATE TABLE IF NOT EXISTS invoice_htlcs (
resolve_time TIMESTAMP,
-- The id of the invoice this htlc belongs to.
invoice_id INTEGER NOT NULL REFERENCES invoices(id),
invoice_id BIGINT NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
-- The htlc_id and chan_id identify the htlc.
UNIQUE (htlc_id, chan_id)
UNIQUE (chan_id, htlc_id)
);
CREATE INDEX IF NOT EXISTS invoice_htlc_invoice_id_idx ON invoice_htlcs(invoice_id);
@@ -129,26 +153,8 @@ CREATE TABLE IF NOT EXISTS invoice_htlc_custom_records (
value BLOB NOT NULL,
-- The htlc id this record belongs to.
htlc_id BIGINT NOT NULL REFERENCES invoice_htlcs(id)
htlc_id BIGINT NOT NULL REFERENCES invoice_htlcs(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS invoice_htlc_custom_records_htlc_id_idx ON invoice_htlc_custom_records(htlc_id);
-- invoice_payments contains the information of a settled invoice payment.
CREATE TABLE IF NOT EXISTS invoice_payments (
-- The id for this invoice payment. Translates to SettleIndex.
id INTEGER PRIMARY KEY,
-- When the payment was settled.
settled_at TIMESTAMP NOT NULL,
-- The amount of the payment in millisatoshis. This is the sum of all the
-- the htlcs settled for this payment.
amount_paid_msat BIGINT NOT NULL,
-- The invoice id this payment is for.
invoice_id INTEGER NOT NULL REFERENCES invoices(id)
);
CREATE INDEX IF NOT EXISTS invoice_payments_settled_at_idx ON invoice_payments(settled_at);
CREATE INDEX IF NOT EXISTS invoice_payments_invoice_id_idx ON invoice_payments(invoice_id);