Skip to content

Commit

Permalink
fix: sqlx::macro db cleanup race condition by adding a margin to curr…
Browse files Browse the repository at this point in the history
…ent timestamp (#2640)

* fix: sqlx::macro db cleanup race condition by adding a margin to current timestamp

* feat: increase margin to 2 seconds
  • Loading branch information
fhsgoncalves authored Oct 11, 2023
1 parent 5ebe296 commit 3c2471e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion sqlx-mysql/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<MySql>, Error> {
}

async fn do_cleanup(conn: &mut MySqlConnection, created_before: Duration) -> Result<usize, Error> {
// since SystemTime is not monotonic we added a little margin here to avoid race conditions with other threads
let created_before_as_secs = created_before.as_secs() - 2;
let delete_db_ids: Vec<u64> = query_scalar(
"select db_id from _sqlx_test_databases \
where created_at < from_unixtime(?)",
)
.bind(&created_before.as_secs())
.bind(&created_before_as_secs)
.fetch_all(&mut *conn)
.await?;

Expand Down
3 changes: 2 additions & 1 deletion sqlx-postgres/src/testing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ async fn test_context(args: &TestArgs) -> Result<TestContext<Postgres>, Error> {
}

async fn do_cleanup(conn: &mut PgConnection, created_before: Duration) -> Result<usize, Error> {
let created_before = i64::try_from(created_before.as_secs()).unwrap();
// since SystemTime is not monotonic we added a little margin here to avoid race conditions with other threads
let created_before = i64::try_from(created_before.as_secs()).unwrap() - 2;

let delete_db_names: Vec<String> = query_scalar(
"select db_name from _sqlx_test.databases \
Expand Down

0 comments on commit 3c2471e

Please sign in to comment.