diff --git a/crates/database/src/lib.rs b/crates/database/src/lib.rs index 8b59d60243..48e6496ef6 100644 --- a/crates/database/src/lib.rs +++ b/crates/database/src/lib.rs @@ -87,7 +87,8 @@ pub fn all_tables() -> impl Iterator { #[allow(non_snake_case)] pub async fn clear_DANGER_(ex: &mut PgTransaction<'_>) -> sqlx::Result<()> { for table in all_tables() { - ex.execute(format!("TRUNCATE {table};").as_str()).await?; + ex.execute(format!("TRUNCATE {table} CASCADE;").as_str()) + .await?; } Ok(()) } diff --git a/crates/database/src/quotes.rs b/crates/database/src/quotes.rs index 6d6cd1a47d..24a62b04d2 100644 --- a/crates/database/src/quotes.rs +++ b/crates/database/src/quotes.rs @@ -4,7 +4,6 @@ use { sqlx::{ types::chrono::{DateTime, Utc}, PgConnection, - Row, }, }; @@ -140,17 +139,12 @@ pub async fn remove_expired_quotes( const QUERY: &str = r#" DELETE FROM quotes WHERE expiration_timestamp < $1 -RETURNING id "#; - let deleted_ids = sqlx::query(QUERY) + sqlx::query(QUERY) .bind(max_expiry) - .fetch_all(&mut *ex) - .await? - .iter() - .map(|row| row.get(0)) - .collect::>(); - - delete_quote_interactions(ex, &deleted_ids).await + .execute(ex) + .await + .map(|_| ()) } /// One row in the `quote_interactions` table. @@ -211,19 +205,6 @@ WHERE quote_id = $1 sqlx::query_as(QUERY).bind(quote_id).fetch_all(ex).await } -pub async fn delete_quote_interactions( - ex: &mut PgConnection, - quote_ids: &[QuoteId], -) -> Result<(), sqlx::Error> { - const QUERY: &str = r#" -DELETE FROM quote_interactions -WHERE quote_id = ANY($1) - "#; - - sqlx::query(QUERY).bind(quote_ids).execute(ex).await?; - Ok(()) -} - #[cfg(test)] mod tests { use { diff --git a/database/sql/V075__add_call_data_to_quotes_and_order_quotes.sql b/database/sql/V075__add_call_data_to_quotes_and_order_quotes.sql index 0d0f50576b..bbb19aed3b 100644 --- a/database/sql/V075__add_call_data_to_quotes_and_order_quotes.sql +++ b/database/sql/V075__add_call_data_to_quotes_and_order_quotes.sql @@ -16,7 +16,8 @@ CREATE TABLE quote_interactions ( value numeric(78,0) NOT NULL, call_data bytea NOT NULL, - PRIMARY KEY (quote_id, index) + PRIMARY KEY (quote_id, index), + FOREIGN KEY (quote_id) REFERENCES quotes(id) ON DELETE CASCADE ); -- Get a specific quote's interactions.