Skip to content
Open
Changes from all 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
62 changes: 55 additions & 7 deletions sea-orm-cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,41 @@ fn create_new_migration(migration_name: &str, migration_dir: &str) -> Result<(),
let migration_filepath =
get_full_migration_dir(migration_dir).join(format!("{}.rs", &migration_name));
println!("Creating migration file `{}`", migration_filepath.display());
// TODO: make OS agnostic
let migration_template =
include_str!("../../template/migration/src/m20220101_000001_create_table.rs");

let migration_template = fmt_migration_tempelate(migration_name);
let mut migration_file = fs::File::create(migration_filepath)?;
migration_file.write_all(migration_template.as_bytes())?;
Ok(())
}

fn fmt_migration_tempelate(migration_name: &str) -> String {
format! {
r#"use sea_orm_migration::{{prelude::*, schema::*}};

pub struct Migration;

impl MigrationName for Migration {{
fn name(&self) -> &str {{
"{migration_name}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for invalid input?

}}
}}

#[async_trait::async_trait]
impl MigrationTrait for Migration {{
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {{
// Replace the sample below with your own migration scripts
todo!();
}}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {{
// Replace the sample below with your own migration scripts
todo!();
}}
}}
"#
}
}

/// `get_migrator_filepath` looks for a file `migration_dir/src/lib.rs`
/// and returns that path if found.
///
Expand Down Expand Up @@ -275,6 +302,30 @@ impl Error for MigrationCommandError {}
mod tests {
use super::*;

const EXPECTED_TEMPLATE: &str = r#"use sea_orm_migration::{prelude::*, schema::*};

pub struct Migration;

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

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
todo!();
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
todo!();
}
}
"#;

#[test]
fn test_create_new_migration() {
let migration_name = "test_name";
Expand All @@ -286,10 +337,7 @@ mod tests {
.join(format!("{migration_name}.rs"));
assert!(migration_filepath.exists());
let migration_content = fs::read_to_string(migration_filepath).unwrap();
assert_eq!(
&migration_content,
include_str!("../../template/migration/src/m20220101_000001_create_table.rs")
);
assert_eq!(&migration_content, EXPECTED_TEMPLATE);
fs::remove_dir_all("/tmp/sea_orm_cli_test_new_migration/").unwrap();
}

Expand Down