From d0e6b43b2926297385b20401c5595ce1efc1ad55 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Thu, 6 Jun 2024 14:52:21 +1000 Subject: [PATCH] fix(coordinator): Add unique_end_date constraint to funding_rates We only need one funding rate entry per `end_date`. Without this change we were getting a new duplicate entry per maker restart. We `DO NOTHING` on conflict to ensure that the actually new rows in the batch will be inserted if we encounter any conflicts. --- .../down.sql | 2 ++ .../up.sql | 2 ++ coordinator/src/funding_fee/db/funding_rates.rs | 9 +++------ 3 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/down.sql create mode 100644 coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/up.sql diff --git a/coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/down.sql b/coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/down.sql new file mode 100644 index 000000000..99c5711c9 --- /dev/null +++ b/coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/down.sql @@ -0,0 +1,2 @@ +ALTER TABLE funding_rates +DROP CONSTRAINT IF EXISTS unique_end_date; diff --git a/coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/up.sql b/coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/up.sql new file mode 100644 index 000000000..1ca851b7c --- /dev/null +++ b/coordinator/migrations/2024-06-06-045129_add_unique_end_date_constraint/up.sql @@ -0,0 +1,2 @@ +ALTER TABLE funding_rates +ADD CONSTRAINT unique_end_date UNIQUE (end_date); diff --git a/coordinator/src/funding_fee/db/funding_rates.rs b/coordinator/src/funding_fee/db/funding_rates.rs index e732feb18..018cea28d 100644 --- a/coordinator/src/funding_fee/db/funding_rates.rs +++ b/coordinator/src/funding_fee/db/funding_rates.rs @@ -1,5 +1,4 @@ use crate::schema::funding_rates; -use anyhow::bail; use anyhow::Result; use diesel::prelude::*; use rust_decimal::prelude::FromPrimitive; @@ -37,14 +36,12 @@ pub fn insert_funding_rates( .map(NewFundingRate::from) .collect::>(); - let affected_rows = diesel::insert_into(funding_rates::table) + diesel::insert_into(funding_rates::table) .values(funding_rates) + .on_conflict(funding_rates::end_date) + .do_nothing() .execute(conn)?; - if affected_rows == 0 { - bail!("Failed to insert funding rates"); - } - Ok(()) }