Skip to content

customizable db locking during migration#2063

Merged
abonander merged 4 commits intolaunchbadge:mainfrom
fuzzbuck:patch-1
Sep 6, 2022
Merged

customizable db locking during migration#2063
abonander merged 4 commits intolaunchbadge:mainfrom
fuzzbuck:patch-1

Conversation

@fuzzbuck
Copy link
Contributor

@fuzzbuck fuzzbuck commented Aug 20, 2022

? - Why
Various pgwire databases such as CockroachDB do not support such a feature.

[iodb] running migrations failed: Execute(Database(PgDatabaseError { severity: Error, code: "42883", message: "unknown function: pg_advisory_lock()", detail: None, hint: None, position: None, where: None, schema: None, table: None, column: None, data_type: None, constraint: None, file: Some("name_resolution.go"), line: Some(205), routine: Some("ResolveFunction") }))

In such scenarios the only way to perform migrations is without locking

This PR allows the end user to choose whether to lock the DB when running migrations or not.
Default behaviour is maintained and DBs are still locked on default.

Example:
sqlx::migrate!().set_locking(false).run(&pool).await

(Changes have been tested & are actively used in development of our app)

Co-authored-by: Austin Bonander <austin.bonander@gmail.com>
@fuzzbuck
Copy link
Contributor Author

Please review again

@abonander
Copy link
Collaborator

Can you run cargo fmt real quick?

@fuzzbuck
Copy link
Contributor Author

there

@abonander
Copy link
Collaborator

The instantiation of Migrator in this doctest is missing locking: true: https://github.com/fuzzbuck/sqlx/blob/patch-1/src/macros/test.md?plain=1#L134

@fuzzbuck
Copy link
Contributor Author

fuzzbuck commented Sep 2, 2022

Is that what was causing the CI to fail?

Anyway, that should be good now. Perhaps I shall make a suggestion to add information about doctests here?
EDIT: as well as cargo formatting, common new contributor mistakes, etc.
https://github.com/launchbadge/sqlx/blob/main/CONTRIBUTING.md

@fuzzbuck fuzzbuck requested a review from abonander September 3, 2022 06:35
@abonander abonander merged commit 18a76fb into launchbadge:main Sep 6, 2022
mpecan added a commit to mpecan/sqlx that referenced this pull request Feb 25, 2026
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 added a commit to mpecan/sqlx that referenced this pull request Feb 25, 2026
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 added a commit to mpecan/sqlx that referenced this pull request Feb 25, 2026
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 added a commit to mpecan/sqlx that referenced this pull request Feb 25, 2026
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).
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.

2 participants