From c07875403c4429d85bd5851af3aa7e7b07a1b01a Mon Sep 17 00:00:00 2001 From: sunce86 Date: Tue, 20 Aug 2024 12:09:29 +0200 Subject: [PATCH 1/2] bump auction id on staging --- crates/autopilot/src/database/auction.rs | 8 +++++++- crates/database/src/auction.rs | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/autopilot/src/database/auction.rs b/crates/autopilot/src/database/auction.rs index 9230ce72a3..5f73658600 100644 --- a/crates/autopilot/src/database/auction.rs +++ b/crates/autopilot/src/database/auction.rs @@ -107,7 +107,13 @@ impl Postgres { let data = serde_json::to_value(auction)?; let mut ex = self.pool.begin().await?; database::auction::delete_all_auctions(&mut ex).await?; - let id = database::auction::save(&mut ex, &data).await?; + let mut id = database::auction::save(&mut ex, &data).await?; + if id < i64::MAX / 2 { + // update the auction id in the `auctions` table + database::auction::update_current_auction_id(&mut ex, i64::MAX / 2).await?; + // return the updated value + id = i64::MAX / 2; + } ex.commit().await?; Ok(id) } diff --git a/crates/database/src/auction.rs b/crates/database/src/auction.rs index 0e5de8ea37..6a7a2ef07f 100644 --- a/crates/database/src/auction.rs +++ b/crates/database/src/auction.rs @@ -12,6 +12,17 @@ RETURNING id Ok(id) } +pub async fn update_current_auction_id( + ex: &mut PgConnection, + id: AuctionId, +) -> Result<(), sqlx::Error> { + const QUERY: &str = r#" +UPDATE auctions +SET id = $1 + ;"#; + sqlx::query(QUERY).bind(id).execute(ex).await.map(|_| ()) +} + pub async fn load_most_recent( ex: &mut PgConnection, ) -> Result, sqlx::Error> { @@ -64,5 +75,10 @@ mod tests { let (id, value_) = load_most_recent(&mut db).await.unwrap().unwrap(); assert_eq!(value, value_); assert_eq!(id_, id); + + // update works + update_current_auction_id(&mut db, 100).await.unwrap(); + let (id, _) = load_most_recent(&mut db).await.unwrap().unwrap(); + assert_eq!(100, id); } } From cd7d0cceec377e3e114c42759ae12284a257072a Mon Sep 17 00:00:00 2001 From: sunce86 Date: Wed, 21 Aug 2024 12:44:57 +0200 Subject: [PATCH 2/2] update sequence --- crates/database/src/auction.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/database/src/auction.rs b/crates/database/src/auction.rs index 6a7a2ef07f..238785bc3d 100644 --- a/crates/database/src/auction.rs +++ b/crates/database/src/auction.rs @@ -16,11 +16,8 @@ pub async fn update_current_auction_id( ex: &mut PgConnection, id: AuctionId, ) -> Result<(), sqlx::Error> { - const QUERY: &str = r#" -UPDATE auctions -SET id = $1 - ;"#; - sqlx::query(QUERY).bind(id).execute(ex).await.map(|_| ()) + let query = format!("ALTER SEQUENCE auctions_id_seq RESTART WITH {};", id); + sqlx::query(&query).execute(ex).await.map(|_| ()) } pub async fn load_most_recent( @@ -78,7 +75,11 @@ mod tests { // update works update_current_auction_id(&mut db, 100).await.unwrap(); - let (id, _) = load_most_recent(&mut db).await.unwrap().unwrap(); - assert_eq!(100, id); + + let id_ = save(&mut db, &value).await.unwrap(); + assert_eq!(100, id_); + + let id_ = save(&mut db, &value).await.unwrap(); + assert_eq!(101, id_); } }