Skip to content

Commit

Permalink
fix: restore Migrator to the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander committed Mar 7, 2024
1 parent bbfd0d7 commit 24be262
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
24 changes: 20 additions & 4 deletions sqlx-core/src/migrate/migrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::slice;

/// A resolved set of migrations, ready to be run.
///
/// Can be constructed statically using `migrate!()` or at runtime using [`Migrator::new()`].
#[derive(Debug)]
#[doc(hidden)]
// Forbids `migrate!()` from constructing this:
// #[non_exhaustive]
pub struct Migrator {
// NOTE: these fields are semver-exempt and may be changed or removed in any future version.
// These have to be public for `migrate!()` to be able to initialize them in an implicitly
// const-promotable context. A `const fn` constructor isn't implicitly const-promotable.
#[doc(hidden)]
pub migrations: Cow<'static, [Migration]>,
#[doc(hidden)]
pub ignore_missing: bool,
#[doc(hidden)]
pub locking: bool,
}

Expand All @@ -33,6 +43,13 @@ fn validate_applied_migrations(
}

impl Migrator {
#[doc(hidden)]
pub const DEFAULT: Migrator = Migrator {
migrations: Cow::Borrowed(&[]),
ignore_missing: false,
locking: true,
};

/// Creates a new instance with the given source.
///
/// # Examples
Expand All @@ -57,8 +74,7 @@ impl Migrator {
{
Ok(Self {
migrations: Cow::Owned(source.resolve().await.map_err(MigrateError::Source)?),
ignore_missing: false,
locking: true,
..Self::DEFAULT
})
}

Expand All @@ -68,7 +84,7 @@ impl Migrator {
self
}

/// Specify whether or not to lock database during migration. Defaults to `true`.
/// Specify whether or not to lock the database during migration. Defaults to `true`.
///
/// ### Warning
/// Disabling locking can lead to errors or data loss if multiple clients attempt to apply migrations simultaneously
Expand Down
5 changes: 2 additions & 3 deletions sqlx-macros-core/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ pub(crate) fn expand_migrator(path: &Path) -> crate::Result<TokenStream> {
Ok(quote! {
::sqlx::migrate::Migrator {
migrations: ::std::borrow::Cow::Borrowed(&[
#(#migrations),*
#(#migrations),*
]),
ignore_missing: false,
locking: true,
..::sqlx::migrate::Migrator::DEFAULT
}
})
}

0 comments on commit 24be262

Please sign in to comment.