Skip to content

Commit

Permalink
feat: run specific migration file with --name option (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark authored May 13, 2022
1 parent c06f806 commit 75ec395
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/commands/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ exports.builder = (yargs) =>
.option('from', {
describe: 'Migration name to start migrations from (excluding)',
type: 'string',
})
.option('name', {
describe:
'Migration name. When specified, only this migration will be run. Mutually exclusive with --to and --from',
type: 'string',
conflicts: ['to', 'from'],
}).argv;

exports.handler = async function (args) {
Expand Down Expand Up @@ -80,7 +86,13 @@ function migrate(args) {
}
return options;
})
.then((options) => migrator.up(options));
.then((options) => {
if (args.name) {
return migrator.up(args.name);
} else {
return migrator.up(options);
}
});
})
.catch((e) => helpers.view.error(e));
}
Expand Down
36 changes: 36 additions & 0 deletions test/db/migrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,42 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => {
});
});
});

it('--name', function (done) {
const migrationsPath = Support.resolveSupportPath('assets', 'migrations');
const migrations = fs.readdirSync(migrationsPath);
const createPersonMigration = migrations.find((migration) =>
migration.includes('createPerson')
);

prepare(`--name ${createPersonMigration}`, () => {
helpers.readTables(this.sequelize, (tables) => {
expect(tables).to.eql(['Person', 'SequelizeMeta']);
done();
});
});
});

it('--name array', function (done) {
const migrationsPath = Support.resolveSupportPath('assets', 'migrations');
const migrations = fs.readdirSync(migrationsPath);
const createPersonMigration = migrations.find((migration) =>
migration.includes('createPerson')
);
const createPostMigration = migrations.find((migration) =>
migration.includes('createPost')
);

prepare(
`--name ${createPersonMigration} --name ${createPostMigration}`,
() => {
helpers.readTables(this.sequelize, (tables) => {
expect(tables).to.eql(['Person', 'Post', 'SequelizeMeta']);
done();
});
}
);
});
});
});

Expand Down

0 comments on commit 75ec395

Please sign in to comment.