From 65b2bac81c9a9bab53076ac792ec4947e67d32ce Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Wed, 26 Mar 2025 10:53:49 +0100 Subject: [PATCH] sqldb: make migration 1 and 3 idempotent --- sqldb/sqlc/migrations/000001_invoices.up.sql | 6 +++-- .../migrations/000003_invoice_events.up.sql | 27 +++++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/sqldb/sqlc/migrations/000001_invoices.up.sql b/sqldb/sqlc/migrations/000001_invoices.up.sql index 7f8c653d5..5dca73b34 100644 --- a/sqldb/sqlc/migrations/000001_invoices.up.sql +++ b/sqldb/sqlc/migrations/000001_invoices.up.sql @@ -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 ( diff --git a/sqldb/sqlc/migrations/000003_invoice_events.up.sql b/sqldb/sqlc/migrations/000003_invoice_events.up.sql index b9280a9d2..7409f149f 100644 --- a/sqldb/sqlc/migrations/000003_invoice_events.up.sql +++ b/sqldb/sqlc/migrations/000003_invoice_events.up.sql @@ -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.