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

feat(migrations): add support for custom migration names #4250

Conversation

AbdlrahmanSaberAbdo
Copy link
Contributor

This PR adds a new option to the migrations:create command that allows developers to specify custom names for migration files. Previously, the migration files were automatically named based on the current timestamp, but with this new option, developers can now give the migration file a more descriptive name that is easier to understand and manage.

This feature was inspired by the functionality in Laravel and Ruby on Rails, and I believe it will bring many benefits, including improved readability and organization of migration files.

I created a discussion before on this: #3987

Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

thanks!

packages/migrations/src/Migrator.ts Show resolved Hide resolved
packages/migrations/src/MigrationGenerator.ts Outdated Show resolved Hide resolved
@@ -452,7 +452,7 @@ export type MigrationsOptions = {
snapshotName?: string;
emit?: 'js' | 'ts' | 'cjs';
generator?: Constructor<IMigrationGenerator>;
fileName?: (timestamp: string) => string;
fileName?: (timestamp: string, name?: string) => string;
Copy link
Member

Choose a reason for hiding this comment

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

the default implementation should respect the name out of box

fileName: (timestamp: string) => `Migration${timestamp}`,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, sorry, I wasn't aware that the update was necessary. Thank you for pointing out this. as for the default implementation, would it suffice to simply add the parameter, or should we consider a different approach, such as using Migration${timestamp}${name}? Let me know your thoughts.

Copy link
Member

Choose a reason for hiding this comment

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

I'd go with Migration${timestamp}${name ? '_' + name : ''}

@B4nan
Copy link
Member

B4nan commented Apr 21, 2023

and dont forget to update the docs

Comment on lines 156 to 163
> To specify a name for your migration file, we can use `npx mikro-orm migration:create --name=newName`

You can customize the naming convention for your migration file by utilizing the `fileName` callback and overriding its default behavior.
```sh
migrations: {
fileName: (time, name) =>`Migration-${time}+${name}`
}
```
Copy link
Member

@B4nan B4nan Apr 21, 2023

Choose a reason for hiding this comment

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

this should have its own section, its not just about "Using via CLI", should be more like "using custom migration names"

also, the parameter is not time but timestamp, we should have type hints in there for both, and the language is not sh but ts, plus bad indent. in this example, we should also throw in case the name is not provided.

migrations: {
  fileName: (timestamp: string, name?: string) => {
    if (!name) {
      throw new Error('Specify migration name via `mikro-orm migration:create --name=...`');
    }

    return `Migration${timestamp}_${name}`;
  },
},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you very much for your input! I have added a new section. Please let me know if I have missed anything else or if you think additional information should be included.

In terms of naming conventions, throwing an error may not always be necessary and will depend on the user's specific case. If the user intends to always specify a name in the migration file (which I personally recommend), then throwing an error is necessary. However, if the user only wants to customize the file naming convention and check if a name has been provided, they can do so without throwing an error. I included two cases in the doc as well

@AbdlrahmanSaberAbdo AbdlrahmanSaberAbdo force-pushed the add-custom-name-option-to-migration-create branch from f9ca5e3 to 997745f Compare April 21, 2023 12:48
@AbdlrahmanSaberAbdo AbdlrahmanSaberAbdo force-pushed the add-custom-name-option-to-migration-create branch from 997745f to 660749e Compare April 21, 2023 12:54
Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

the code is ok now, lets please try to improve the docs, now its more like few disconnected notes.

if you struggle with this, no worries, i can rewrite it myself if you want

docs/docs/migrations.md Show resolved Hide resolved
docs/docs/migrations.md Outdated Show resolved Hide resolved
docs/docs/migrations.md Outdated Show resolved Hide resolved
docs/docs/migrations.md Outdated Show resolved Hide resolved
@AbdlrahmanSaberAbdo
Copy link
Contributor Author

the code is ok now, lets please try to improve the docs, now its more like few disconnected notes.

if you struggle with this, no worries, i can rewrite it myself if you want

Thanks, Martin! I made the updates to the document based on your feedback, but I'm afraid I might have missed other important points. I would appreciate it if you could share your thoughts on the document and feel free to make any necessary changes.

@AbdlrahmanSaberAbdo AbdlrahmanSaberAbdo force-pushed the add-custom-name-option-to-migration-create branch from eb663a9 to 412a2f9 Compare April 21, 2023 18:16
@codecov
Copy link

codecov bot commented Apr 21, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (8bb1373) 99.70% compared to head (412a2f9) 99.70%.

❗ Current head 412a2f9 differs from pull request most recent head 370a74f. Consider uploading reports for the commit 370a74f to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4250   +/-   ##
=======================================
  Coverage   99.70%   99.70%           
=======================================
  Files         214      214           
  Lines       13965    13986   +21     
  Branches     3277     3283    +6     
=======================================
+ Hits        13924    13945   +21     
  Misses         40       40           
  Partials        1        1           
Impacted Files Coverage Δ
packages/core/src/typings.ts 100.00% <ø> (ø)
...ckages/cli/src/commands/MigrationCommandFactory.ts 100.00% <100.00%> (ø)
packages/core/src/utils/Configuration.ts 100.00% <100.00%> (ø)
packages/migrations/src/MigrationGenerator.ts 100.00% <100.00%> (ø)
packages/migrations/src/Migrator.ts 97.90% <100.00%> (ø)

... and 8 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@B4nan B4nan changed the title feat: add --name option to migration:create command feat(migrations): add support for custom migration names Apr 21, 2023
Copy link
Member

@B4nan B4nan left a comment

Choose a reason for hiding this comment

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

improved the docs a bit

@B4nan B4nan merged commit fb2879e into mikro-orm:master Apr 21, 2023
7 of 8 checks passed
jsprw pushed a commit to jsprw/mikro-orm-full-text-operators that referenced this pull request May 7, 2023
)

This PR adds a new option to the `migrations:create` command that allows
developers to specify custom names for migration files. Previously, the
migration files were automatically named based on the current timestamp,
but with this new option, developers can now give the migration file a
more descriptive name that is easier to understand and manage.

This feature was inspired by the functionality in Laravel and Ruby on
Rails, and I believe it will bring many benefits, including improved
readability and organization of migration files.

I created a discussion before on this:
mikro-orm#3987

---------

Co-authored-by: Martin Adámek <banan23@gmail.com>
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.

None yet

2 participants