Billy Chan 1054d6451c
Use file!() macro to avoid manually defining the name of migration (#736)
* Add `DeriveMigrationName`

* CLI migrate generate without replacing migration name

* Update migrations

* Fixup
2022-06-29 00:42:59 +08:00

33 lines
783 B
Rust

use proc_macro2::TokenStream;
use quote::quote;
struct DeriveMigrationName {
ident: syn::Ident,
}
impl DeriveMigrationName {
fn new(input: syn::DeriveInput) -> Self {
let ident = input.ident;
DeriveMigrationName { ident }
}
fn expand(&self) -> TokenStream {
let ident = &self.ident;
quote!(
#[automatically_derived]
impl sea_orm_migration::MigrationName for #ident {
fn name(&self) -> &str {
sea_orm_migration::util::get_file_stem(file!())
}
}
)
}
}
/// Method to derive a MigrationName
pub fn expand_derive_migration_name(input: syn::DeriveInput) -> syn::Result<TokenStream> {
Ok(DeriveMigrationName::new(input).expand())
}