From 8381e87d4a368433dbf2e78532c6bbbee250811a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Th=C3=A9riault?= Date: Thu, 23 Jul 2020 15:25:13 -0400 Subject: [PATCH] Document migrate! (and small fixes) --- sqlx-core/src/migrate/migrator.rs | 2 +- sqlx-macros/src/lib.rs | 8 ++---- sqlx-macros/src/migrate.rs | 1 - src/lib.rs | 4 --- src/macros.rs | 43 +++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/sqlx-core/src/migrate/migrator.rs b/sqlx-core/src/migrate/migrator.rs index 916a8df0..0ee4773d 100644 --- a/sqlx-core/src/migrate/migrator.rs +++ b/sqlx-core/src/migrate/migrator.rs @@ -37,7 +37,7 @@ impl Migrator { } /// Creates a new instance from a static slice of migrations. - pub async fn from_static(migrations: &'static [Migration]) -> Self { + pub fn from_static(migrations: &'static [Migration]) -> Self { Self { migrations: Cow::Borrowed(migrations), } diff --git a/sqlx-macros/src/lib.rs b/sqlx-macros/src/lib.rs index d1d64cec..7dc5c53e 100644 --- a/sqlx-macros/src/lib.rs +++ b/sqlx-macros/src/lib.rs @@ -87,12 +87,8 @@ pub fn derive_from_row(input: TokenStream) -> TokenStream { pub fn migrate(input: TokenStream) -> TokenStream { use syn::LitStr; - let input = syn::parse_macro_input!(input as Option); - let dir = input - .as_ref() - .map_or("migrations".to_owned(), LitStr::value); - - match migrate::expand_migrator_from_dir(dir) { + let input = syn::parse_macro_input!(input as LitStr); + match migrate::expand_migrator_from_dir(input.value()) { Ok(ts) => ts.into(), Err(e) => { if let Some(parse_err) = e.downcast_ref::() { diff --git a/sqlx-macros/src/migrate.rs b/sqlx-macros/src/migrate.rs index 1b1ac224..b0ebd0eb 100644 --- a/sqlx-macros/src/migrate.rs +++ b/sqlx-macros/src/migrate.rs @@ -2,7 +2,6 @@ use proc_macro2::TokenStream; use quote::{quote, ToTokens, TokenStreamExt}; use sha2::{Digest, Sha384}; use std::fs; -use std::fs::DirEntry; use std::path::Path; struct QuotedMigration { diff --git a/src/lib.rs b/src/lib.rs index 53b01bd9..125342ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,10 +61,6 @@ pub extern crate sqlx_macros; #[doc(hidden)] pub use sqlx_macros::{FromRow, Type}; -// embedded migrations -#[cfg(all(feature = "migrate", features = "macros"))] -pub use sqlx_macros::migrate; - #[cfg(feature = "macros")] mod macros; diff --git a/src/macros.rs b/src/macros.rs index 6cb4e635..574eee79 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -573,3 +573,46 @@ macro_rules! query_file_as_unchecked ( macro_result!($($args),*) }) ); + +/// Creates a static [Migrator][crate::migrate::Migrator] by embedding the migrations in the binary. +/// +/// The macro takes an optional migrations directory and defaults to `"migrations"` if it's not specified. +/// +/// ```rust,ignore +/// sqlx::migrate!("migrations") // same as sqlx::migrate!() +/// .run(&pool) +/// .await?; +/// ``` +/// +/// It can also be used as a static constructor. +/// +/// ```rust,ignore +/// use sqlx::migrate::Migrator; +/// +/// static MIGRATOR: Migrator = sqlx::migrate!(); +/// ``` +#[cfg(feature = "migrate")] +#[macro_export] +macro_rules! migrate { + ($dir:literal) => { + #[allow(dead_code)] + { + #[macro_use] + mod _macro_result { + $crate::sqlx_macros::migrate!($dir); + } + macro_result!() + } + }; + + () => { + #[allow(dead_code)] + { + #[macro_use] + mod _macro_result { + $crate::sqlx_macros::migrate!("migrations"); + } + macro_result!() + } + }; +}