Skip to content

Commit fd24e8f

Browse files
committed
refactor: simplify migration testing
1 parent 9b0c27f commit fd24e8f

File tree

4 files changed

+15
-65
lines changed

4 files changed

+15
-65
lines changed

sqlx-test/src/lib.rs

-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use sqlx::pool::PoolOptions;
22
use sqlx::{Connection, Database, Pool};
33
use std::env;
4-
use std::sync::atomic::{AtomicBool, Ordering};
54

65
pub fn setup_if_needed() {
76
let _ = dotenvy::dotenv();
@@ -224,31 +223,3 @@ macro_rules! Postgres_query_for_test_prepared_type {
224223
"SELECT ({0} is not distinct from $1)::int4, {0}, $2"
225224
};
226225
}
227-
228-
/// Global lock that prevents multiple tests in this module to be executed at the same time.
229-
static GLOBAL_LOCK: AtomicBool = AtomicBool::new(false);
230-
231-
/// Simple lock guard that should not be used in production but that is `Send` (i.e. can easily be used with tokio).
232-
///
233-
/// This may be helpful for tests that modify the database state, e.g. migrations.
234-
pub struct SimpleLockGuard;
235-
236-
impl SimpleLockGuard {
237-
/// Acquire global lock.
238-
pub fn acquire() -> Self {
239-
loop {
240-
let was_locked = GLOBAL_LOCK.fetch_or(true, Ordering::SeqCst);
241-
if !was_locked {
242-
break;
243-
}
244-
std::thread::sleep(std::time::Duration::from_millis(10));
245-
}
246-
Self
247-
}
248-
}
249-
250-
impl Drop for SimpleLockGuard {
251-
fn drop(&mut self) {
252-
GLOBAL_LOCK.store(false, Ordering::SeqCst);
253-
}
254-
}

tests/mysql/migrate.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
use sqlx::migrate::Migrator;
22
use sqlx::mysql::{MySql, MySqlConnection};
3+
use sqlx::pool::PoolConnection;
34
use sqlx::Executor;
45
use sqlx::Row;
56
use std::path::Path;
67

7-
use sqlx_test::{new, SimpleLockGuard};
8-
9-
#[sqlx_macros::test]
10-
async fn simple() -> anyhow::Result<()> {
11-
let _guard = SimpleLockGuard::acquire();
12-
13-
let mut conn = new::<MySql>().await?;
8+
#[sqlx::test(migrations = false)]
9+
async fn simple(mut conn: PoolConnection<MySql>) -> anyhow::Result<()> {
1410
clean_up(&mut conn).await?;
1511

1612
let migrator = Migrator::new(Path::new("tests/mysql/migrations_simple")).await?;
@@ -31,11 +27,8 @@ async fn simple() -> anyhow::Result<()> {
3127
Ok(())
3228
}
3329

34-
#[sqlx_macros::test]
35-
async fn reversible() -> anyhow::Result<()> {
36-
let _guard = SimpleLockGuard::acquire();
37-
38-
let mut conn = new::<MySql>().await?;
30+
#[sqlx::test(migrations = false)]
31+
async fn reversible(mut conn: PoolConnection<MySql>) -> anyhow::Result<()> {
3932
clean_up(&mut conn).await?;
4033

4134
let migrator = Migrator::new(Path::new("tests/mysql/migrations_reversible")).await?;

tests/postgres/migrate.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
use sqlx::migrate::Migrator;
2+
use sqlx::pool::PoolConnection;
23
use sqlx::postgres::{PgConnection, Postgres};
34
use sqlx::Executor;
45
use sqlx::Row;
56
use std::path::Path;
67

7-
use sqlx_test::{new, SimpleLockGuard};
8-
9-
#[sqlx_macros::test]
10-
async fn simple() -> anyhow::Result<()> {
11-
let _guard = SimpleLockGuard::acquire();
12-
13-
let mut conn = new::<Postgres>().await?;
8+
#[sqlx::test(migrations = false)]
9+
async fn simple(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
1410
clean_up(&mut conn).await?;
1511

1612
let migrator = Migrator::new(Path::new("tests/postgres/migrations_simple")).await?;
@@ -31,11 +27,8 @@ async fn simple() -> anyhow::Result<()> {
3127
Ok(())
3228
}
3329

34-
#[sqlx_macros::test]
35-
async fn reversible() -> anyhow::Result<()> {
36-
let _guard = SimpleLockGuard::acquire();
37-
38-
let mut conn = new::<Postgres>().await?;
30+
#[sqlx::test(migrations = false)]
31+
async fn reversible(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
3932
clean_up(&mut conn).await?;
4033

4134
let migrator = Migrator::new(Path::new("tests/postgres/migrations_reversible")).await?;

tests/sqlite/migrate.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
use sqlx::migrate::Migrator;
2+
use sqlx::pool::PoolConnection;
23
use sqlx::sqlite::{Sqlite, SqliteConnection};
34
use sqlx::Executor;
45
use sqlx::Row;
56
use std::path::Path;
67

7-
use sqlx_test::{new, SimpleLockGuard};
8-
9-
#[sqlx_macros::test]
10-
async fn simple() -> anyhow::Result<()> {
11-
let _guard = SimpleLockGuard::acquire();
12-
13-
let mut conn = new::<Sqlite>().await?;
8+
#[sqlx::test(migrations = false)]
9+
async fn simple(mut conn: PoolConnection<Sqlite>) -> anyhow::Result<()> {
1410
clean_up(&mut conn).await?;
1511

1612
let migrator = Migrator::new(Path::new("tests/sqlite/migrations_simple")).await?;
@@ -31,11 +27,8 @@ async fn simple() -> anyhow::Result<()> {
3127
Ok(())
3228
}
3329

34-
#[sqlx_macros::test]
35-
async fn reversible() -> anyhow::Result<()> {
36-
let _guard = SimpleLockGuard::acquire();
37-
38-
let mut conn = new::<Sqlite>().await?;
30+
#[sqlx::test(migrations = false)]
31+
async fn reversible(mut conn: PoolConnection<Sqlite>) -> anyhow::Result<()> {
3932
clean_up(&mut conn).await?;
4033

4134
let migrator = Migrator::new(Path::new("tests/sqlite/migrations_reversible")).await?;

0 commit comments

Comments
 (0)