sqldb: make migration 1 and 3 idempotent

This commit is contained in:
Andras Banki-Horvath
2025-03-26 10:53:49 +01:00
parent b6cf1bcaa0
commit 65b2bac81c
2 changed files with 20 additions and 13 deletions

View File

@ -1,12 +1,14 @@
-- sequences contains all sequences used for invoices.
CREATE TABLE invoice_sequences (
CREATE TABLE IF NOT EXISTS 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);
INSERT INTO invoice_sequences (name, current_value)
VALUES ('settle_index', 0)
ON CONFLICT (name) DO NOTHING;
-- invoices table contains all the information shared by all the invoice types.
CREATE TABLE IF NOT EXISTS invoices (

View File

@ -5,26 +5,31 @@ CREATE TABLE IF NOT EXISTS invoice_event_types(
description TEXT NOT NULL
);
-- invoice_event_types defines the different types of invoice events.
INSERT INTO invoice_event_types (id, description)
VALUES
-- Insert events explicitly, checking their existence first.
INSERT INTO invoice_event_types (id, description)
SELECT id, description FROM (
-- invoice_created is the event type used when an invoice is created.
(0, 'invoice_created'),
SELECT 0 AS id, 'invoice_created' AS description UNION ALL
-- invoice_canceled is the event type used when an invoice is canceled.
(1, 'invoice_canceled'),
SELECT 1, 'invoice_canceled' UNION ALL
-- invoice_settled is the event type used when an invoice is settled.
(2, 'invoice_settled'),
SELECT 2, 'invoice_settled' UNION ALL
-- setid_created is the event type used when an AMP sub invoice
-- corresponding to the set_id is created.
(3, 'setid_created'),
SELECT 3, 'setid_created' UNION ALL
-- 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
SELECT 4, 'setid_canceled' UNION ALL
-- setid_settled is the event type used when an AMP sub invoice
-- corresponding to the set_id is settled.
(5, 'setid_settled');
SELECT 5, 'setid_settled'
) AS new_values
WHERE NOT EXISTS (
SELECT 1 FROM invoice_event_types
WHERE invoice_event_types.id = new_values.id
);
-- 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.