Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file!() macro to avoid manually defining the name of migration #736

Merged
merged 5 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use entity::post::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use entity::post::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use entity::post::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use entity::note;
use sea_orm::{DbBackend, EntityTrait, Schema};
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

fn get_seaorm_create_stmt<E: EntityTrait>(e: E) -> TableCreateStatement {
Expand All @@ -17,12 +18,6 @@ fn get_seaorm_drop_stmt<E: EntityTrait>(e: E) -> TableDropStatement {
Table::drop().table(e).if_exists().to_owned()
}

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220101_000001_create_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use entity::post::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use entity::post::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use entity::post::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use entity::post::*;
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
6 changes: 1 addition & 5 deletions sea-orm-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,8 @@ fn create_new_migration(migration_name: &str, migration_dir: &str) -> Result<(),
// TODO: make OS agnostic
let migration_template =
include_str!("../template/migration/src/m20220101_000001_create_table.rs");
let migration_content =
migration_template.replace("m20220101_000001_create_table", &migration_name);
let mut migration_file = fs::File::create(migration_filepath)?;
migration_file.write_all(migration_content.as_bytes())?;
migration_file.write_all(migration_template.as_bytes())?;
Ok(())
}

Expand Down Expand Up @@ -466,8 +464,6 @@ mod tests {
.join(format!("{}.rs", migration_name));
assert!(migration_filepath.exists());
let migration_content = fs::read_to_string(migration_filepath).unwrap();
let migration_content =
migration_content.replace(&migration_name, "m20220101_000001_create_table");
assert_eq!(
&migration_content,
include_str!("../template/migration/src/m20220101_000001_create_table.rs")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220101_000001_create_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
32 changes: 32 additions & 0 deletions sea-orm-macros/src/derives/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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())
}
2 changes: 2 additions & 0 deletions sea-orm-macros/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod entity;
mod entity_model;
mod from_query_result;
mod into_active_model;
mod migration;
mod model;
mod primary_key;
mod relation;
Expand All @@ -18,6 +19,7 @@ pub use entity::*;
pub use entity_model::*;
pub use from_query_result::*;
pub use into_active_model::*;
pub use migration::*;
pub use model::*;
pub use primary_key::*;
pub use relation::*;
27 changes: 27 additions & 0 deletions sea-orm-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,33 @@ pub fn derive_relation(input: TokenStream) -> TokenStream {
.into()
}

/// The DeriveMigrationName derive macro will implement `sea_orm_migration::MigrationName` for a migration.
///
/// ### Usage
///
/// ```ignore
/// #[derive(DeriveMigrationName)]
/// pub struct Migration;
/// ```
///
/// The derive macro above will provide following implementation,
/// given the file name is `m20220120_000001_create_post_table.rs`.
///
/// ```ignore
/// impl MigrationName for Migration {
/// fn name(&self) -> &str {
/// "m20220120_000001_create_post_table"
/// }
/// }
/// ```
#[proc_macro_derive(DeriveMigrationName)]
pub fn derive_migration_name(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
derives::expand_derive_migration_name(input)
.unwrap_or_else(Error::into_compile_error)
.into()
}

#[doc(hidden)]
#[proc_macro_attribute]
pub fn test(_: TokenStream, input: TokenStream) -> TokenStream {
Expand Down
1 change: 1 addition & 0 deletions sea-orm-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod manager;
pub mod migrator;
pub mod prelude;
pub mod seaql_migrations;
pub mod util;

pub use manager::*;
pub use migrator::*;
Expand Down
1 change: 1 addition & 0 deletions sea-orm-migration/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub use sea_orm;
pub use sea_orm::sea_query;
pub use sea_orm::sea_query::*;
pub use sea_orm::DbErr;
pub use sea_orm::DeriveMigrationName;
36 changes: 36 additions & 0 deletions sea-orm-migration/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pub fn get_file_stem(path: &str) -> &str {
std::path::Path::new(path)
.file_stem()
.map(|f| f.to_str().unwrap())
.unwrap()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_file_stem() {
let pair = vec![
(
"m20220101_000001_create_table.rs",
"m20220101_000001_create_table",
),
(
"src/m20220101_000001_create_table.rs",
"m20220101_000001_create_table",
),
(
"migration/src/m20220101_000001_create_table.rs",
"m20220101_000001_create_table",
),
(
"/migration/src/m20220101_000001_create_table.tmp.rs",
"m20220101_000001_create_table.tmp",
),
];
for (path, expect) in pair {
assert_eq!(get_file_stem(path), expect);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220118_000001_create_cake_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ use super::m20220118_000001_create_cake_table::Cake;
use sea_orm_migration::prelude::*;
use sea_orm_migration::sea_orm::DbBackend;

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220118_000002_create_fruit_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
use sea_orm_migration::prelude::*;
use sea_orm_migration::sea_orm::{entity::*, query::*};

#[derive(DeriveMigrationName)]
pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220118_000003_seed_cake_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ pub use schema::*;
#[cfg(feature = "macros")]
pub use sea_orm_macros::{
DeriveActiveEnum, DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn,
DeriveCustomColumn, DeriveEntity, DeriveEntityModel, DeriveIntoActiveModel, DeriveModel,
DerivePrimaryKey, DeriveRelation, FromQueryResult,
DeriveCustomColumn, DeriveEntity, DeriveEntityModel, DeriveIntoActiveModel,
DeriveMigrationName, DeriveModel, DerivePrimaryKey, DeriveRelation, FromQueryResult,
};

pub use sea_query;
Expand Down