Edit migrator to make it possible to create static instances

This commit is contained in:
Raphaël Thériault 2020-07-23 17:54:05 -04:00
parent 92646e00b8
commit 435445fbd0
3 changed files with 9 additions and 13 deletions

View File

@ -6,7 +6,7 @@ use std::slice;
#[derive(Debug)] #[derive(Debug)]
pub struct Migrator { pub struct Migrator {
migrations: Cow<'static, [Migration]>, pub migrations: Cow<'static, [Migration]>,
} }
impl Migrator { impl Migrator {
@ -36,13 +36,6 @@ impl Migrator {
}) })
} }
/// Creates a new instance from a static slice of migrations.
pub fn from_static(migrations: &'static [Migration]) -> Self {
Self {
migrations: Cow::Borrowed(migrations),
}
}
/// Get an iterator over all known migrations. /// Get an iterator over all known migrations.
pub fn iter(&self) -> slice::Iter<'_, Migration> { pub fn iter(&self) -> slice::Iter<'_, Migration> {
self.migrations.iter() self.migrations.iter()

View File

@ -85,10 +85,12 @@ pub(crate) fn expand_migrator_from_dir(dir: LitStr) -> crate::Result<proc_macro2
Ok(quote! { Ok(quote! {
macro_rules! macro_result { macro_rules! macro_result {
() => { () => {
sqlx::migrate::Migrator::from_static(&[ sqlx::migrate::Migrator {
migrations: std::borrow::Cow::Borrowed(&[
#(#migrations),* #(#migrations),*
]) ])
} }
} }
}
}) })
} }

View File

@ -1,12 +1,13 @@
use sqlx::migrate::Migrator; use sqlx::migrate::Migrator;
use std::path::Path; use std::path::Path;
static EMBEDDED: Migrator = sqlx::migrate!("tests/migrate/migrations");
#[sqlx_macros::test] #[sqlx_macros::test]
async fn same_output() -> anyhow::Result<()> { async fn same_output() -> anyhow::Result<()> {
let embedded = sqlx::migrate!("tests/migrate/migrations");
let runtime = Migrator::new(Path::new("tests/migrate/migrations")).await?; let runtime = Migrator::new(Path::new("tests/migrate/migrations")).await?;
for (e, r) in embedded.iter().zip(runtime.iter()) { for (e, r) in EMBEDDED.iter().zip(runtime.iter()) {
assert_eq!(e.version, r.version); assert_eq!(e.version, r.version);
assert_eq!(e.description, r.description); assert_eq!(e.description, r.description);
assert_eq!(e.sql, r.sql); assert_eq!(e.sql, r.sql);