Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customizable db locking during migration #2063

Merged
merged 4 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions sqlx-core/src/migrate/migrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::slice;
pub struct Migrator {
pub migrations: Cow<'static, [Migration]>,
pub ignore_missing: bool,
pub locking: bool,
}

fn validate_applied_migrations(
Expand Down Expand Up @@ -56,6 +57,7 @@ impl Migrator {
Ok(Self {
migrations: Cow::Owned(source.resolve().await.map_err(MigrateError::Source)?),
ignore_missing: false,
locking: true,
})
}

Expand All @@ -65,6 +67,19 @@ impl Migrator {
self
}

/// Specify whether or not to lock database during migration. Defaults to `true`.
///
/// ### Warning
/// Disabling locking can lead to errors or data loss if multiple clients attempt to apply migrations simultaneously
/// without some sort of mutual exclusion.
///
/// This should only be used if the database does not support locking, e.g. CockroachDB which talks the Postgres
/// protocol but does not support advisory locks used by SQLx's migrations support for Postgres.
pub fn set_locking(&mut self, locking: bool) -> &Self {
self.locking = locking;
self
}

/// Get an iterator over all known migrations.
pub fn iter(&self) -> slice::Iter<'_, Migration> {
self.migrations.iter()
Expand Down Expand Up @@ -103,7 +118,9 @@ impl Migrator {
C: Migrate,
{
// lock the database for exclusive access by the migrator
conn.lock().await?;
if self.locking {
conn.lock().await?;
}

// creates [_migrations] table only if needed
// eventually this will likely migrate previous versions of the table
Expand Down Expand Up @@ -141,7 +158,9 @@ impl Migrator {

// unlock the migrator to allow other migrators to run
// but do nothing as we already migrated
conn.unlock().await?;
if self.locking {
conn.unlock().await?;
}

Ok(())
}
Expand Down Expand Up @@ -170,7 +189,9 @@ impl Migrator {
let mut conn = migrator.acquire().await?;

// lock the database for exclusive access by the migrator
conn.lock().await?;
if self.locking {
conn.lock().await?;
}

// creates [_migrations] table only if needed
// eventually this will likely migrate previous versions of the table
Expand Down Expand Up @@ -201,7 +222,9 @@ impl Migrator {

// unlock the migrator to allow other migrators to run
// but do nothing as we already migrated
conn.unlock().await?;
if self.locking {
conn.unlock().await?;
}

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions sqlx-macros/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub(crate) fn expand_migrator(path: &Path) -> crate::Result<TokenStream> {
#(#migrations),*
]),
ignore_missing: false,
locking: true,
}
})
}
1 change: 1 addition & 0 deletions src/macros/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ use sqlx::PgPool;
# static MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate::Migrator {
# migrations: Cow::Borrowed(&[]),
# ignore_missing: false,
# locking: true,
# };
# }

Expand Down