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

@@ -1,59 +1,51 @@
-- invoice_event_types stores the different types of events that can be emitted
-- for invoices.
-- invoice_event_types stores the different types of invoice events.
CREATE TABLE IF NOT EXISTS invoice_event_types(
id INTEGER PRIMARY KEY,
description TEXT NOT NULL
);
-- invoice_events stores all events related to the node invoices.
CREATE TABLE IF NOT EXISTS invoice_events (
id INTEGER PRIMARY KEY,
-- created_at is the creation time of this event.
created_at TIMESTAMP NOT NULL,
-- invoice_id is the reference to the invoice this event was emitted for.
invoice_id INTEGER NOT NULL REFERENCES invoices(id),
-- htlc_id is the reference to the htlc this event was emitted for, may be
-- null.
htlc_id BIGINT REFERENCES invoice_htlcs(htlc_id),
-- set_id is the reference to the set_id this event was emitted for, may be
-- null.
set_id BLOB NOT NULL REFERENCES amp_invoice_payments(set_id),
-- event_type is the type of this event.
event_type INTEGER NOT NULL REFERENCES invoice_event_types(id),
-- event_metadata is a TLV encoding any relevant information for this kind
-- of events.
event_metadata BLOB
);
CREATE INDEX IF NOT EXISTS invoice_events_created_at_idx ON invoice_events(created_at);
CREATE INDEX IF NOT EXISTS invoice_events_invoice_id_idx ON invoice_events(invoice_id);
CREATE INDEX IF NOT EXISTS invoice_events_htlc_id_idx ON invoice_events(htlc_id);
CREATE INDEX IF NOT EXISTS invoice_events_set_id_idx ON invoice_events(set_id);
CREATE INDEX IF NOT EXISTS invoice_events_event_type_idx ON invoice_events(event_type);
-- invoice_event_types defines the different types of events that can be emitted
-- for an invoice.
-- invoice_event_types defines the different types of invoice events.
INSERT INTO invoice_event_types (id, description)
VALUES
-- invoice_created is the event emitted when an invoice is created.
-- invoice_created is the event type used when an invoice is created.
(0, 'invoice_created'),
-- invoice_canceled is the event emitted when an invoice is canceled.
(1, "invoice_canceled"),
-- invoice_settled is the event emitted when an invoice is settled.
(2, "invoice_settled"),
-- setid_created is the event emitted when the first htlc for the set_id is
-- received.
(3, "setid_created"),
-- setid_canceled is the event emitted when the set_id is canceled.
(4, "setid_canceled"),
-- setid_settled is the event emitted when the set_id is settled.
(5, "setid_settled");
-- invoice_canceled is the event type used when an invoice is canceled.
(1, 'invoice_canceled'),
-- invoice_settled is the event type used when an invoice is settled.
(2, 'invoice_settled'),
-- setid_created is the event type used when an AMP sub invoice
-- corresponding to the set_id is created.
(3, 'setid_created'),
-- setid_canceled is the event type used when an AMP sub invoice
-- corresponding to the set_id is canceled.
(4, 'setid_canceled'),
-- setid_settled is the event type used when an AMP sub invoice
-- corresponding to the set_id is settled.
(5, 'setid_settled');
-- invoice_events stores all major events related to the node's invoices and
-- AMP sub invoices. This table can be used to create a historical view of what
-- happened to the node's invoices.
CREATE TABLE IF NOT EXISTS invoice_events (
id BIGINT PRIMARY KEY,
-- added_at is the timestamp when this event was added.
added_at TIMESTAMP NOT NULL,
-- event_type is the type of this event.
event_type INTEGER NOT NULL REFERENCES invoice_event_types(id) ON DELETE CASCADE,
-- invoice_id is the reference to the invoice this event was added for.
invoice_id BIGINT NOT NULL REFERENCES invoices(id) ON DELETE CASCADE,
-- set_id is the reference to the AMP sub invoice this event was added for.
-- May be NULL if the event is not related to an AMP sub invoice.
set_id BLOB REFERENCES amp_sub_invoices(set_id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS invoice_events_added_at_idx ON invoice_events(added_at);
CREATE INDEX IF NOT EXISTS invoice_events_event_type_idx ON invoice_events(event_type);
CREATE INDEX IF NOT EXISTS invoice_events_invoice_id_idx ON invoice_events(invoice_id);
CREATE INDEX IF NOT EXISTS invoice_events_set_id_idx ON invoice_events(set_id);