Skip to content

Commit 75ec395

Browse files
feat: run specific migration file with --name option (#1034)
1 parent c06f806 commit 75ec395

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/commands/migrate.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ exports.builder = (yargs) =>
1717
.option('from', {
1818
describe: 'Migration name to start migrations from (excluding)',
1919
type: 'string',
20+
})
21+
.option('name', {
22+
describe:
23+
'Migration name. When specified, only this migration will be run. Mutually exclusive with --to and --from',
24+
type: 'string',
25+
conflicts: ['to', 'from'],
2026
}).argv;
2127

2228
exports.handler = async function (args) {
@@ -80,7 +86,13 @@ function migrate(args) {
8086
}
8187
return options;
8288
})
83-
.then((options) => migrator.up(options));
89+
.then((options) => {
90+
if (args.name) {
91+
return migrator.up(args.name);
92+
} else {
93+
return migrator.up(options);
94+
}
95+
});
8496
})
8597
.catch((e) => helpers.view.error(e));
8698
}

test/db/migrate.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,42 @@ describe(Support.getTestDialectTeaser('db:migrate'), () => {
580580
});
581581
});
582582
});
583+
584+
it('--name', function (done) {
585+
const migrationsPath = Support.resolveSupportPath('assets', 'migrations');
586+
const migrations = fs.readdirSync(migrationsPath);
587+
const createPersonMigration = migrations.find((migration) =>
588+
migration.includes('createPerson')
589+
);
590+
591+
prepare(`--name ${createPersonMigration}`, () => {
592+
helpers.readTables(this.sequelize, (tables) => {
593+
expect(tables).to.eql(['Person', 'SequelizeMeta']);
594+
done();
595+
});
596+
});
597+
});
598+
599+
it('--name array', function (done) {
600+
const migrationsPath = Support.resolveSupportPath('assets', 'migrations');
601+
const migrations = fs.readdirSync(migrationsPath);
602+
const createPersonMigration = migrations.find((migration) =>
603+
migration.includes('createPerson')
604+
);
605+
const createPostMigration = migrations.find((migration) =>
606+
migration.includes('createPost')
607+
);
608+
609+
prepare(
610+
`--name ${createPersonMigration} --name ${createPostMigration}`,
611+
() => {
612+
helpers.readTables(this.sequelize, (tables) => {
613+
expect(tables).to.eql(['Person', 'Post', 'SequelizeMeta']);
614+
done();
615+
});
616+
}
617+
);
618+
});
583619
});
584620
});
585621

0 commit comments

Comments
 (0)