mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
Document migrate! (and small fixes)
This commit is contained in:
parent
e5e9665bd9
commit
8381e87d4a
@ -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),
|
||||
}
|
||||
|
||||
@ -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>() {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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!()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user