From 51e49ec178faba71f66de6e22e0c1655f1cf4ed1 Mon Sep 17 00:00:00 2001 From: Richard Holzeis Date: Fri, 7 Jun 2024 11:08:06 +0200 Subject: [PATCH] fix: Drop foreign key constraint on order id The order and the matches are persisted by async actors. It can happen that the market order has not yet been persisted when the match is processed. Thus it could happen that the matches table is persisted before the order has been persisted, which resulted in a foreign key violation. By dropping the foreign key we don't care if the order is already persisted and can continue with eventual consistency. I think this is the better option than to synchronize the two inserts as we are anyways aiming for decoupling the orderbook from the coordinator. --- .../down.sql | 9 +++++ .../up.sql | 2 ++ coordinator/src/schema.rs | 34 +++++++++---------- 3 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/down.sql create mode 100644 coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/up.sql diff --git a/coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/down.sql b/coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/down.sql new file mode 100644 index 000000000..beb16f583 --- /dev/null +++ b/coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/down.sql @@ -0,0 +1,9 @@ +ALTER TABLE "matches" + ADD CONSTRAINT matches_order_id_fkey + FOREIGN KEY (order_id) + REFERENCES orders (order_id); + +ALTER TABLE "matches" + ADD CONSTRAINT matches_match_order_id_fkey + FOREIGN KEY (match_order_id) + REFERENCES orders (order_id); diff --git a/coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/up.sql b/coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/up.sql new file mode 100644 index 000000000..dc5ad5e1d --- /dev/null +++ b/coordinator/migrations/2024-06-07-105918_drop_foreign_key_constraint_to_order_id_on_matches/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE "matches" DROP CONSTRAINT matches_order_id_fkey; +ALTER TABLE "matches" DROP CONSTRAINT matches_match_order_id_fkey; diff --git a/coordinator/src/schema.rs b/coordinator/src/schema.rs index a0d1ebb95..ac0424001 100644 --- a/coordinator/src/schema.rs +++ b/coordinator/src/schema.rs @@ -216,23 +216,6 @@ diesel::table! { } } -diesel::table! { - use diesel::sql_types::*; - use super::sql_types::InvoiceStateType; - - hodl_invoices (id) { - id -> Int4, - trader_pubkey -> Text, - r_hash -> Text, - amount_sats -> Int8, - pre_image -> Nullable, - created_at -> Timestamptz, - updated_at -> Nullable, - invoice_state -> InvoiceStateType, - order_id -> Nullable, - } -} - diesel::table! { funding_fee_events (id) { id -> Int4, @@ -257,6 +240,23 @@ diesel::table! { } } +diesel::table! { + use diesel::sql_types::*; + use super::sql_types::InvoiceStateType; + + hodl_invoices (id) { + id -> Int4, + trader_pubkey -> Text, + r_hash -> Text, + amount_sats -> Int8, + pre_image -> Nullable, + created_at -> Timestamptz, + updated_at -> Nullable, + invoice_state -> InvoiceStateType, + order_id -> Nullable, + } +} + diesel::table! { last_outbound_dlc_messages (peer_id) { peer_id -> Text,