Skip to content

Commit 90cd6ba

Browse files
committed
feat: skip-execution flag for migrate command
1 parent 4d8fb6e commit 90cd6ba

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

src/commands/migrate.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ exports.builder = (yargs) =>
2424
'Migration name. When specified, only this migration will be run. Mutually exclusive with --to and --from',
2525
type: 'string',
2626
conflicts: ['to', 'from'],
27+
})
28+
.option('skip-execution', {
29+
describe: 'Mark migration as completed without actually executing it',
30+
default: false,
31+
type: 'boolean',
2732
}).argv;
2833

2934
exports.handler = async function (args) {
@@ -85,9 +90,53 @@ function migrate(args) {
8590
}
8691
options.from = args.from;
8792
}
93+
if (args['skip-execution']) {
94+
let migrationsToRun = migrations;
95+
96+
if (args.name) {
97+
migrationsToRun = [args.name];
98+
} else {
99+
if (options.from) {
100+
const fromIndex = migrations.findIndex(
101+
(m) => m.file === options.from
102+
);
103+
if (fromIndex !== -1) {
104+
migrationsToRun = migrations.slice(fromIndex + 1);
105+
}
106+
}
107+
if (options.to) {
108+
const toIndex = migrationsToRun.findIndex(
109+
(m) => m.file === options.to
110+
);
111+
if (toIndex !== -1) {
112+
migrationsToRun = migrationsToRun.slice(0, toIndex + 1);
113+
}
114+
}
115+
}
116+
117+
if (migrationsToRun.length === 0) {
118+
helpers.view.log('No migrations to skip.');
119+
process.exit(0);
120+
}
121+
122+
return Promise.all(
123+
migrationsToRun.map((migration) => {
124+
helpers.view.log(
125+
'Marking migration as executed:',
126+
migration.file
127+
);
128+
return migrator.storage.logMigration(migration.file);
129+
})
130+
).then(() => {
131+
return { executed: true };
132+
});
133+
}
134+
88135
return options;
89136
})
90137
.then((options) => {
138+
if (options && options.executed) return;
139+
91140
if (args.name) {
92141
return migrator.up(args.name);
93142
} else {

test/db/migrate-skip.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const expect = require('expect.js');
2+
const Support = require(__dirname + '/../support');
3+
const helpers = require(__dirname + '/../support/helpers');
4+
const gulp = require('gulp');
5+
6+
describe(Support.getTestDialectTeaser('db:migrate --skip-execution'), () => {
7+
const prepare = function (callback) {
8+
const config = { url: helpers.getTestUrl() };
9+
const configContent = 'module.exports = ' + JSON.stringify(config);
10+
let result = '';
11+
12+
return gulp
13+
.src(Support.resolveSupportPath('tmp'))
14+
.pipe(helpers.clearDirectory())
15+
.pipe(helpers.runCli('init'))
16+
.pipe(helpers.removeFile('config/config.json'))
17+
.pipe(helpers.copyMigration('createPerson.js'))
18+
.pipe(helpers.overwriteFile(configContent, 'config/config.js'))
19+
.pipe(helpers.runCli('db:migrate --skip-execution', { pipeStdout: true }))
20+
.on('error', (e) => {
21+
callback(e);
22+
})
23+
.on('data', (data) => {
24+
result += data.toString();
25+
})
26+
.on('end', () => {
27+
callback(null, result);
28+
});
29+
};
30+
31+
beforeEach(function () {
32+
const queryInterface = this.sequelize.getQueryInterface();
33+
this.queryGenerator =
34+
queryInterface.queryGenerator || queryInterface.QueryGenerator;
35+
});
36+
37+
it('marks migration as executed but does not execute it', function (done) {
38+
prepare((err, output) => {
39+
if (err) return done(err);
40+
41+
helpers.readTables(this.sequelize, (tables) => {
42+
expect(tables).to.have.length(1);
43+
expect(tables).to.contain('SequelizeMeta');
44+
expect(tables).to.not.contain('Person');
45+
46+
helpers
47+
.execQuery(
48+
this.sequelize,
49+
this.queryGenerator.selectQuery('SequelizeMeta'),
50+
{ raw: true, type: 'SELECT' }
51+
)
52+
.then((items) => {
53+
expect(items.length).to.equal(1);
54+
expect(items[0].name).to.contain('createPerson');
55+
56+
expect(output).to.contain('Marking migration as executed');
57+
58+
done();
59+
})
60+
.catch((e) => done(e));
61+
});
62+
});
63+
});
64+
});

0 commit comments

Comments
 (0)