Skip to content

Commit

Permalink
Updated removing of expired quote interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
mstrug committed Nov 29, 2024
1 parent 5239901 commit 5aac1dc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
3 changes: 2 additions & 1 deletion crates/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ pub fn all_tables() -> impl Iterator<Item = &'static str> {
#[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(())
}
Expand Down
27 changes: 4 additions & 23 deletions crates/database/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use {
sqlx::{
types::chrono::{DateTime, Utc},
PgConnection,
Row,
},
};

Expand Down Expand Up @@ -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::<Vec<_>>();

delete_quote_interactions(ex, &deleted_ids).await
.execute(ex)
.await
.map(|_| ())
}

/// One row in the `quote_interactions` table.
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5aac1dc

Please sign in to comment.