feat: ignore unrelated files in migration directory #28
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When the migration directory contains additional files,
sql-migrations
aborts with a rather cryptic error:The reason is twofold:
migration-provider.js
returns all files in the migration directory (the result offs.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 on1662812614706_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 inmigration-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.