Skip to content

Commit

Permalink
fix: Drop foreign key constraint on order id
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
holzeis committed Jun 7, 2024
1 parent 37a7a73 commit 51e49ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "matches" DROP CONSTRAINT matches_order_id_fkey;
ALTER TABLE "matches" DROP CONSTRAINT matches_match_order_id_fkey;
34 changes: 17 additions & 17 deletions coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text>,
created_at -> Timestamptz,
updated_at -> Nullable<Timestamptz>,
invoice_state -> InvoiceStateType,
order_id -> Nullable<Uuid>,
}
}

diesel::table! {
funding_fee_events (id) {
id -> Int4,
Expand All @@ -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<Text>,
created_at -> Timestamptz,
updated_at -> Nullable<Timestamptz>,
invoice_state -> InvoiceStateType,
order_id -> Nullable<Uuid>,
}
}

diesel::table! {
last_outbound_dlc_messages (peer_id) {
peer_id -> Text,
Expand Down

0 comments on commit 51e49ec

Please sign in to comment.