Skip to content

Commit 4a9d343

Browse files
author
Andrew Kerr
committed
Making running down migrations in reverse run order the default (and only) option.
1 parent c632fa9 commit 4a9d343

File tree

4 files changed

+21
-60
lines changed

4 files changed

+21
-60
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ Database migration framework for node.js
1717
```
1818
Usage: db-migrate [up|down|create] migrationName [options]
1919
20+
Down migrations are run in reverse run order, so migrationName is ignored for down migrations.
21+
Use the --count option to control how many down migrations are run (default is 1).
22+
2023
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]
28-
--down-using-run-order Run down migrations in reverse run order, [default: false]
29-
rather than reverse creation order.
24+
--env, -e The environment to run the migrations under. [default: "dev"]
25+
--migrations-dir, -m The directory containing your migration files. [default: "./migrations"]
26+
--count, -c Max number of migrations to run.
27+
--dry-run Prints the SQL but doesn't run it. [boolean]
28+
--verbose, -v Verbose mode. [default: false]
29+
--config Location of the database.json file. [default: "./database.json"]
30+
--force-exit Call system.exit() after migration run [default: false]
3031
```
3132

3233
## Creating Migrations

bin/db-migrate

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var argv = optimist
2020
.default({
2121
verbose: false,
2222
"force-exit": false,
23-
"down-using-run-order": false,
2423
config: process.cwd() + '/database.json',
2524
'migrations-dir': process.cwd() + '/migrations' })
2625
.usage('Usage: db-migrate [up|down|create] migrationName [options]')
@@ -43,9 +42,6 @@ var argv = optimist
4342
.describe('force-exit', 'Forcibly exit the migration process on completion.')
4443
.boolean('force-exit')
4544

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-
4945
.describe('verbose', 'Verbose mode.')
5046
.alias('v', 'verbose')
5147
.boolean('v')
@@ -75,7 +71,6 @@ if (argv.help || argv._.length == 0) {
7571

7672
global.verbose = argv.verbose;
7773
global.dryRun = argv['dry-run'];
78-
global.downUsingRunOrder = argv['down-using-run-order'];
7974
if(global.dryRun) {
8075
log.info('dry run');
8176
}
@@ -139,7 +134,8 @@ function executeUp() {
139134
}
140135

141136
function executeDown() {
142-
if(!argv.destination && !argv.count) {
137+
if(!argv.count) {
138+
log.info('Defaulting to running 1 down migration.');
143139
argv.count = 1;
144140
}
145141
index.connect(config.getCurrent().settings, function(err, migrator) {
@@ -170,20 +166,16 @@ function run() {
170166
case 'down':
171167
loadConfig();
172168
if(argv._.length > 0) {
173-
if (global.downUsingRunOrder) {
174-
log.info('Ignoring migration name since --down-using-run-order is set.');
169+
if (action == 'down') {
170+
log.info('Ignoring migration name for down migrations. Use --count to control how many down migrations are run.');
171+
argv.destination = null;
175172
} else {
176173
argv.destination = argv._.shift().toString();
177174
}
178175
}
179176
if(action == 'up') {
180177
executeUp();
181178
} 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-
}
187179
executeDown();
188180
}
189181
break;

lib/migration.js

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

135135
Migration.loadFromDatabase = function(dir, driver, callback) {
136136
log.verbose('loading migrations from database');
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) {
137+
driver.all('SELECT * FROM migrations ORDER BY run_on DESC, name DESC', function(err, dbResults) {
141138
if (err) { callback(err); return; }
142139
var migrations = dbResults.map(function(result) {
143140
return new Migration(path.join(dir, result.name));

lib/migrator.js

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ function isIncludedInUp(migration, destination) {
1212
return migrationTest <= destinationTest;
1313
}
1414

15-
function isIncludedInDown(migration, destination) {
16-
if(!destination) {
17-
return true;
18-
}
19-
var migrationTest = migration.name.substring(0, Math.min(migration.name.length, destination.length));
20-
var destinationTest = destination.substring(0, Math.min(migration.name.length, destination.length));
21-
return migrationTest >= destinationTest;
22-
}
23-
2415
function filterUp(allMigrations, completedMigrations, destination, count) {
2516
var sortFn = function(a, b) {
2617
return a.name.slice(0, a.name.indexOf('-')) - b.name.slice(0, b.name.indexOf('-'));
@@ -39,12 +30,8 @@ function filterUp(allMigrations, completedMigrations, destination, count) {
3930
.slice(0, count);
4031
}
4132

42-
function filterDown(completedMigrations, destination, count) {
43-
return completedMigrations
44-
.filter(function(completedMigration) {
45-
return isIncludedInDown(completedMigration, destination);
46-
})
47-
.slice(0, count);
33+
function filterDown(completedMigrations, count) {
34+
return completedMigrations.slice(0, count);
4835
}
4936

5037

@@ -94,18 +81,10 @@ Migrator.prototype = {
9481
if (dbmUtil.isFunction(funcOrOpts)) {
9582
funcOrOpts(this.driver, callback);
9683
} else {
97-
this.downToBy(funcOrOpts.destination, funcOrOpts.count, callback);
84+
this.downToBy(funcOrOpts.count, callback);
9885
}
9986
},
10087

101-
upBy: function(count, callback) {
102-
this.upToBy('99999999999999', count, callback);
103-
},
104-
105-
upTo: function(partialName, callback) {
106-
this.upToBy(partialName, Number.MAX_VALUE, callback);
107-
},
108-
10988
upToBy: function(partialName, count, callback) {
11089
var self = this;
11190
Migration.loadFromFilesystem(self.migrationsDir, function(err, allMigrations) {
@@ -134,20 +113,12 @@ Migrator.prototype = {
134113
});
135114
},
136115

137-
downBy: function(count, callback) {
138-
this.downToBy('00000000000000', count, callback);
139-
},
140-
141-
downTo: function(partialName, callback) {
142-
this.downToBy(partialName, Number.MAX_VALUE, callback);
143-
},
144-
145-
downToBy: function(partialName, count, callback) {
116+
downToBy: function(count, callback) {
146117
var self = this;
147118
Migration.loadFromDatabase(self.migrationsDir, self.driver, function(err, completedMigrations) {
148119
if (err) { return callback(err); }
149120

150-
var toRun = filterDown(completedMigrations, partialName, count);
121+
var toRun = filterDown(completedMigrations, count);
151122

152123
if (toRun.length === 0) {
153124
log.info('No migrations to run');

0 commit comments

Comments
 (0)