Document migrate! (and small fixes)

This commit is contained in:
Raphaël Thériault 2020-07-23 15:25:13 -04:00
parent e5e9665bd9
commit 8381e87d4a
5 changed files with 46 additions and 12 deletions

View File

@ -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),
}

View File

@ -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<LitStr>);
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::<syn::Error>() {

View File

@ -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 {

View File

@ -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;

View File

@ -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!()
}
};
}