From 81837002c1e0a29ca87b24fb12edcbe7f14f22dc Mon Sep 17 00:00:00 2001 From: Bastian Krol Date: Sat, 10 Sep 2022 15:22:49 +0200 Subject: [PATCH] feat: ignore unrelated files in migration directory 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. --- commands/run-migrations-command.js | 7 ++++++- migration-provider.js | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/commands/run-migrations-command.js b/commands/run-migrations-command.js index f521cba..eff862e 100644 --- a/commands/run-migrations-command.js +++ b/commands/run-migrations-command.js @@ -33,7 +33,12 @@ module.exports = function (migrationProvider, adapter, minMigrationTime, logger) function getPending(migrationsList, appliedMigrationIds, minMigrationTime) { var pending = []; migrationsList.forEach(function (migration) { - var id = migration.match(/^(\d+)/)[0]; + var idMatch = migration.match(/^(\d+)/); + if (!idMatch) { + console.log(`Ignoring file ${migration}, it does not match the naming pattern.`); + return; + } + var id = idMatch[0]; if ((!minMigrationTime || id >= minMigrationTime) && !~appliedMigrationIds.indexOf(id) && migration.match(/^\d+\_up.*$/)) { pending.push(migration); } diff --git a/migration-provider.js b/migration-provider.js index 26aec63..e724fe8 100644 --- a/migration-provider.js +++ b/migration-provider.js @@ -4,7 +4,8 @@ var path = require('path'); module.exports = function (config) { return { getMigrationsList: function () { - return fs.readdirSync(config.migrationsDir); + return fs.readdirSync(config.migrationsDir) + .filter(filename => filename.endsWith('.sql')); }, /** * @param {Array} migrationsList list of filenames