mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-03 07:45:30 +00:00
Edit migrator to make it possible to create static instances
This commit is contained in:
parent
92646e00b8
commit
435445fbd0
@ -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()
|
||||||
|
@ -85,9 +85,11 @@ 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),*
|
migrations: std::borrow::Cow::Borrowed(&[
|
||||||
])
|
#(#migrations),*
|
||||||
|
])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user