Skip to content

Conversation

Huliiiiii
Copy link
Member

PR Info

Currently, migration names are generated by the derive macro DeriveMigrationName, which obtains the file name of the current migration file. However, this approach does not support a directory-based layout like:

- migration_name
  - up.sql
  - down.sql
  - mod.rs

In this PR, I modified the get_file_stem function to return the parent directory name when the current file is mod.rs. This allows migration names to be correctly resolved even when using this layout.

New Features

  • Support migration_name/mod.rs migration layout

Bug Fixes

Breaking Changes

Changes

@Huliiiiii
Copy link
Member Author

To be honest, I don't think this is a good solution, shouldn't we specify names when generating migrations that don't depend on the file location?

@Stefan99353
Copy link

I am currently using this as a workaroung:

impl MigrationName for Migration {
    fn name(&self) -> &str {
        module_path!()
            .split("::")
            .last()
            .expect("Module path must have at least one element")
    }
}

@Huliiiiii
Copy link
Member Author

fn create_new_migration(migration_name: &str, migration_dir: &str) -> Result<(), Box<dyn Error>> {
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 mut migration_file = fs::File::create(migration_filepath)?;
migration_file.write_all(migration_template.as_bytes())?;
Ok(())
}

I think the better solution is to put the migration name in the generated file instead of using the runtime method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants