Skip to content

feat: ignore unrelated files in migration directory #28

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

basti1302
Copy link

When the migration directory contains additional files, sql-migrations aborts with a rather cryptic error:

    ERROR: TypeError: Cannot read properties of null (reading '0')
        at /path/to/project/node_modules/sql-migrations/commands/run-migrations-command.js:36:43

The reason is twofold:

  • migration-provider.js returns all files in the migration directory (the result of fs.readdir unconditionally and unfiltered.
  • commands/run-migrations-command.js matches on the file name to extract the migration ID/timestamp and has no check if there is a match or not, it just uses the first match unconditionally.

This is especially annoying when working on migrations with editors that create a temporary file in the current directory, like vim does (vim automatically creates .1662812614706_up_my_migration.sql.swp when working on 1662812614706_up_my_migration.sql in the same directory). There might be other editors with similar behavior.

The fix in this change is also twofold:

  • migration-provider.js now filters on files that end with .sql and will ignore other files. This might be debatable. Do people also use other file formats/suffixes? I am fine with dropping this change if you feel this is too restrictive.
  • commands/run-migrations-command.js now checks if there is actually a match and prints a useful warning about the file that didn't match the expected pattern. Even with the first fix in migration-provider.js in place, this is still useful if there is an .sql file in the directory that does not match the expected file name pattern.

Also, provide a useful message when the a migration files does not
match the naming scheme.

This fixes
    ERROR: TypeError: Cannot read properties of null (reading '0')
        at /path/to/project/node_modules/sql-migrations/commands/run-migrations-command.js:36:43
when the migration folder contains a file that does not comply withthe
naming pattern.
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.

1 participant