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! in migration files #705

Closed
tyt2y3 opened this issue May 9, 2022 · 5 comments · Fixed by #736
Closed

Use file! in migration files #705

tyt2y3 opened this issue May 9, 2022 · 5 comments · Fixed by #736
Assignees
Labels
good first issue Good for newcomers
Milestone

Comments

@tyt2y3
Copy link
Member

tyt2y3 commented May 9, 2022

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

Can we use https://doc.rust-lang.org/std/macro.file.html instead of manually copying the name?

We'd probably want to change the migration template too

@smonv
Copy link
Contributor

smonv commented May 10, 2022

I did some testing and found out file!() return full filename with extension. So in order to use, we need extra step to remove the extension part too.

So I'm not sure it worth the effort to change for template, the string already generated by cli.

@tyt2y3
Copy link
Member Author

tyt2y3 commented May 12, 2022

I imagine we can use a compile time const fn to split out the file name.
I think this will reduce confusion when files got renamed.

@billy1624
Copy link
Member

This is doable. I guess we can introduce a derive for Migration struct.

pub struct Migration;

impl MigrationName for Migration {
    fn name(&self) -> &str {
        std::path::Path::new(file!())
            .file_stem()
            .map(|f| f.to_str().unwrap())
            .unwrap()
    }
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    ...
}

@billy1624 billy1624 added the good first issue Good for newcomers label May 13, 2022
@tyt2y3 tyt2y3 added this to the 0.9.0 milestone May 15, 2022
@billy1624 billy1624 moved this to Triage in SeaQL Dev Tracker May 16, 2022
@billy1624 billy1624 moved this from Triage to Next Up in SeaQL Dev Tracker May 16, 2022
@billy1624 billy1624 self-assigned this May 16, 2022
@billy1624 billy1624 moved this from Next Up to In Progress in SeaQL Dev Tracker May 16, 2022
@billy1624 billy1624 moved this from In Progress to Review in SeaQL Dev Tracker May 16, 2022
@billy1624 billy1624 moved this from Review to In Progress in SeaQL Dev Tracker May 16, 2022
@billy1624 billy1624 moved this from In Progress to Review in SeaQL Dev Tracker May 16, 2022
@lpotthast
Copy link

lpotthast commented Jun 16, 2022

I usually do the following:

In all migration files, include:

crate::migration_file!();

I defined the macro like this:

#[macro_export]
macro_rules! migration_file {
    () => {
        pub struct Migration;
        impl MigrationName for Migration {
            fn name(&self) -> &str {
                std::path::Path::new(file!())
                    .file_name()
                    .unwrap()
                    .to_str()
                    .unwrap()
                    .split(".")
                    .next()
                    .unwrap()
            }
        }
    };
}

@tyt2y3
Copy link
Member Author

tyt2y3 commented Jun 19, 2022

Thanks. This is quite neat!

@billy1624 billy1624 moved this from Review to Done in SeaQL Dev Tracker Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants