Skip to content

feat(postgres): add locking attribute to #[sqlx::test]#4180

Open
mpecan wants to merge 1 commit intolaunchbadge:mainfrom
mpecan:feat/sqlx-test-locking
Open

feat(postgres): add locking attribute to #[sqlx::test]#4180
mpecan wants to merge 1 commit intolaunchbadge:mainfrom
mpecan:feat/sqlx-test-locking

Conversation

@mpecan
Copy link

@mpecan mpecan commented Feb 25, 2026

Adds locking = false support to #[sqlx::test] to skip advisory locks during test database setup and migrations. This is the #[sqlx::test] analog of Migrator::set_locking(false) (#2063).

Since #3753 switched test setup to pg_advisory_xact_lock, databases that speak the PostgreSQL wire protocol but don't implement advisory locks (e.g. CockroachDB) cannot use #[sqlx::test] at all.

Usage:

#[sqlx::test(locking = false)]
async fn my_test(pool: PgPool) -> sqlx::Result<()> {
    // ...
    Ok(())
}

Locking remains enabled by default. Disabling it means concurrent test runs may race during schema setup, which is acceptable for databases that handle schema DDL differently.

Does your PR solve an issue?

Partially addresses #198 (CockroachDB support).

Is this a breaking change?

No. Default behavior is unchanged (locking = true).

@mpecan mpecan force-pushed the feat/sqlx-test-locking branch from 014bb58 to 3cf761a Compare February 25, 2026 18:28
@mpecan mpecan marked this pull request as draft February 25, 2026 19:40
@mpecan mpecan force-pushed the feat/sqlx-test-locking branch from 3cf761a to 501cd1a Compare February 25, 2026 19:41
Add a `locking` parameter to `#[sqlx::test]` that controls whether an
advisory lock is acquired before creating the test database schema.
Defaults to `true` to preserve existing PostgreSQL behavior.

Setting `locking = false` allows `#[sqlx::test]` to work with databases
that speak the PostgreSQL wire protocol but do not implement advisory
locks, such as CockroachDB. When disabled, migrator locking is also
skipped so the entire test setup is advisory-lock-free.

Follows the same pattern as `Migrator::set_locking()` (PR launchbadge#2063).
@mpecan mpecan force-pushed the feat/sqlx-test-locking branch from 501cd1a to 82f2933 Compare February 25, 2026 19:45
@mpecan mpecan marked this pull request as ready for review February 25, 2026 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant