Skip to content

Feature: Allow naming migration files via generate --name - #85

Open
moritzebeling wants to merge 3 commits into
mainfrom
feature/generate-migration-file-with-preset-name
Open

Feature: Allow naming migration files via generate --name#85
moritzebeling wants to merge 3 commits into
mainfrom
feature/generate-migration-file-with-preset-name

Conversation

@moritzebeling

@moritzebeling moritzebeling commented Mar 18, 2026

Copy link
Copy Markdown
Member

When generating a new migration, the filename always defaults to {timestamp}-migration.js, requiring a manual rename to something meaningful. This is especially annoying for automated workflows (e.g. AI coding agents).

This PR adds an optional -n, --name flag to generate that sluggifies the provided name into the filename and places it as a comment inside the generated file. Fully backward-compatible -- omitting the flag preserves the existing behavior.

Changes

  • Added -n, --name <name> option to the generate CLI command
  • Changed createMigration to use a slugified name in the filename and as in-file comment
  • Added lib/helpers/slugify.js utility (zero dependencies, 7 LOC)
  • Added lib/helpers/slugify.test.js with 9 test cases covering edge cases

How to test

# Existing behavior unchanged
npx migrations generate
# -> migrations/1710000000000-migration.js

# With name
npx migrations generate -n "Add hero section"
# -> migrations/1710000000000-add-hero-section.js
# File contains: // Add hero section

# Edge case: special-chars-only name falls back gracefully
npx migrations generate -n "---"
# -> migrations/1710000000000-migration.js

@moritzebeling moritzebeling self-assigned this Mar 18, 2026
@moritzebeling
moritzebeling requested a review from bezoerb March 18, 2026 11:18
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
4.8% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@bezoerb
bezoerb requested a review from Copilot March 19, 2026 06:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds optional naming for newly generated migration files via a -n, --name flag, producing a slugified filename suffix and embedding the provided name into the generated migration file content.

Changes:

  • Added -n, --name <name> option to the generate CLI command.
  • Updated createMigration to incorporate a slugified name into the generated migration filename and include a name-derived comment in the file body.
  • Introduced a new slugify helper with accompanying Jest tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
cli.js Adds --name option and forwards it to createMigration.
lib/migration.js Uses slugify(name) to build the migration filename and injects a comment into generated content.
lib/helpers/slugify.js New helper for slugifying a provided migration name.
lib/helpers/slugify.test.js Adds Jest coverage for slugify behavior and edge cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread lib/migration.js
Comment on lines +46 to 52
const hasName = name && slugify(name);
const slug = hasName || 'migration';
const filename = path.join(directory, `${timestamp}-${slug}.${module ? 'cjs' : 'js'}`);
const comment = hasName ? `// ${name}` : '// Add your migration code here';
const content = stripIndent`${migrationHeader}
// Add your migration code here
${comment}
})`;
Comment thread lib/helpers/slugify.js
Comment on lines +1 to +9
const slugify = (text) =>
text
.toString()
.replace(/([a-z])([A-Z])/g, '$1-$2')
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-/, '')
.replace(/-$/, '');
});

it('handles leading and trailing hyphens', () => {
expect(slugify('--hello--')).toBe('hello');
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