Skip to content

Commit c632fa9

Browse files
author
Andrew Kerr
committed
Add --down-using-run-order option to run down migrations based on run order rather than creation order.
1 parent c909258 commit c632fa9

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ Database migration framework for node.js
1818
Usage: db-migrate [up|down|create] migrationName [options]
1919
2020
Options:
21-
--env, -e The environment to run the migrations under. [default: "dev"]
22-
--migrations-dir, -m The directory containing your migration files. [default: "./migrations"]
23-
--count, -c Max number of migrations to run.
24-
--dry-run Prints the SQL but doesn't run it. [boolean]
25-
--verbose, -v Verbose mode. [default: false]
26-
--config Location of the database.json file. [default: "./database.json"]
27-
--force-exit Call system.exit() after migration run [default: false]
21+
--env, -e The environment to run the migrations under. [default: "dev"]
22+
--migrations-dir, -m The directory containing your migration files. [default: "./migrations"]
23+
--count, -c Max number of migrations to run.
24+
--dry-run Prints the SQL but doesn't run it. [boolean]
25+
--verbose, -v Verbose mode. [default: false]
26+
--config Location of the database.json file. [default: "./database.json"]
27+
--force-exit Call system.exit() after migration run [default: false]
28+
--down-using-run-order Run down migrations in reverse run order, [default: false]
29+
rather than reverse creation order.
2830
```
2931

3032
## Creating Migrations

bin/db-migrate

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var argv = optimist
2020
.default({
2121
verbose: false,
2222
"force-exit": false,
23+
"down-using-run-order": false,
2324
config: process.cwd() + '/database.json',
2425
'migrations-dir': process.cwd() + '/migrations' })
2526
.usage('Usage: db-migrate [up|down|create] migrationName [options]')
@@ -42,6 +43,9 @@ var argv = optimist
4243
.describe('force-exit', 'Forcibly exit the migration process on completion.')
4344
.boolean('force-exit')
4445

46+
.describe('down-using-run-order', 'Run down migrations in reverse run order, rather than reverse creation order.')
47+
.boolean('down-using-run-order')
48+
4549
.describe('verbose', 'Verbose mode.')
4650
.alias('v', 'verbose')
4751
.boolean('v')
@@ -71,6 +75,7 @@ if (argv.help || argv._.length == 0) {
7175

7276
global.verbose = argv.verbose;
7377
global.dryRun = argv['dry-run'];
78+
global.downUsingRunOrder = argv['down-using-run-order'];
7479
if(global.dryRun) {
7580
log.info('dry run');
7681
}
@@ -165,11 +170,20 @@ function run() {
165170
case 'down':
166171
loadConfig();
167172
if(argv._.length > 0) {
168-
argv.destination = argv._.shift().toString();
173+
if (global.downUsingRunOrder) {
174+
log.info('Ignoring migration name since --down-using-run-order is set.');
175+
} else {
176+
argv.destination = argv._.shift().toString();
177+
}
169178
}
170179
if(action == 'up') {
171180
executeUp();
172181
} else {
182+
if(global.downUsingRunOrder) {
183+
log.info('using run order for down migrations');
184+
} else {
185+
log.info('using creation order for down migrations');
186+
}
173187
executeDown();
174188
}
175189
break;

lib/migration.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ Migration.loadFromFilesystem = function(dir, callback) {
134134

135135
Migration.loadFromDatabase = function(dir, driver, callback) {
136136
log.verbose('loading migrations from database');
137-
driver.all('SELECT * FROM migrations ORDER BY name DESC', function(err, dbResults) {
137+
var query = 'SELECT * FROM migrations ORDER BY ';
138+
query += global.downUsingRunOrder ? 'run_on DESC, name DESC' : 'name DESC';
139+
140+
driver.all(query, function(err, dbResults) {
138141
if (err) { callback(err); return; }
139142
var migrations = dbResults.map(function(result) {
140143
return new Migration(path.join(dir, result.name));

0 commit comments

Comments
 (0)