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(()) }