Skip to content

Commit fa1a161

Browse files
committed
feat(sync): add sync mode
Refers to #383 Resolves #313 Refers to #222
1 parent 376fdc3 commit fa1a161

File tree

3 files changed

+124
-12
lines changed

3 files changed

+124
-12
lines changed

api.js

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,36 @@ dbmigrate.prototype = {
303303
}.bind(this)).asCallback(callback);
304304
},
305305

306+
/**
307+
* Executes up a given number of migrations or a specific one.
308+
*
309+
* Defaults to up all migrations if no count is given.
310+
*/
311+
sync: function(specification, opts, callback) {
312+
313+
if (arguments.length > 0) {
314+
if (typeof(specification) === 'string') {
315+
316+
this.internals.argv.destination = specification;
317+
}
318+
319+
if (typeof(opts) === 'string') {
320+
321+
this.internals.migrationMode = opts;
322+
this.internals.matching = opts;
323+
}
324+
else if (typeof(opts) === 'function') {
325+
326+
callback = opts;
327+
}
328+
}
329+
330+
return Promise.fromCallback(function(callback) {
331+
332+
executeSync(this.internals, this.config, callback);
333+
}.bind(this)).asCallback(callback);
334+
},
335+
306336
/**
307337
* Executes down for all currently migrated migrations.
308338
*/
@@ -520,7 +550,8 @@ function setDefaultArgv(internals, isModule) {
520550
'ignore-completed-migrations': false
521551
})
522552
.usage(
523-
'Usage: db-migrate [up|down|reset|create|db|seed|transition] [[dbname/]migrationName|all] [options]'
553+
'Usage: db-migrate [up|down|reset|sync|create|db|seed|transition] ' +
554+
'[[dbname/]migrationName|all] [options]'
524555
)
525556

526557
.describe('env',
@@ -558,14 +589,16 @@ function setDefaultArgv(internals, isModule) {
558589
.string('config')
559590

560591
.describe('sql-file',
561-
'Automatically create two sql files for up and down statements in /sqls and generate the javascript code that loads them.'
592+
'Automatically create two sql files for up and down statements in ' +
593+
'/sqls and generate the javascript code that loads them.'
562594
)
563595
.boolean('sql-file')
564596

565597
.describe('coffee-file', 'Create a coffeescript migration file')
566598
.boolean('coffee-file')
567599
.describe('ignore-on-init',
568-
'Create files that will run only if ignore-on-init in the env is set to false (currently works onlt with SQL)'
600+
'Create files that will run only if ignore-on-init in the env is set ' +
601+
'to false (currently works onlt with SQL)'
569602
).boolean('ignore-on-init')
570603

571604
.describe('migration-table',
@@ -883,6 +916,42 @@ function executeUp(internals, config, callback) {
883916
});
884917
}
885918

919+
function executeSync(internals, config, callback) {
920+
921+
migrationHook(internals)
922+
.then(function() {
923+
924+
var Migrator = require('./lib/migrator.js');
925+
var index = require('./connect');
926+
927+
if (!internals.argv.count) {
928+
internals.argv.count = Number.MAX_VALUE;
929+
}
930+
index.connect({
931+
config: config.getCurrent().settings,
932+
internals: internals
933+
}, Migrator, function(err, migrator) {
934+
assert.ifError(err);
935+
936+
if (internals.locTitle)
937+
migrator.migrationsDir = path.resolve(internals.argv['migrations-dir'],
938+
internals.locTitle);
939+
else
940+
migrator.migrationsDir = path.resolve(internals.argv['migrations-dir']);
941+
942+
internals.migrationsDir = migrator.migrationsDir;
943+
944+
migrator.driver.createMigrationsTable(function(err) {
945+
assert.ifError(err);
946+
log.verbose('migration table created');
947+
948+
migrator.sync(internals.argv, internals.onComplete.bind(this,
949+
migrator, internals, callback));
950+
});
951+
});
952+
});
953+
}
954+
886955
function executeDown(internals, config, callback) {
887956

888957
migrationHook(internals)
@@ -1095,6 +1164,24 @@ function run(internals, config) {
10951164
}
10961165
executeCreateMigration(internals, config);
10971166
break;
1167+
case 'sync':
1168+
1169+
if (internals.argv._.length === 0) {
1170+
1171+
log.error('Missing sync destination!');
1172+
process.exit(1);
1173+
}
1174+
1175+
internals.argv.count = Number.MAX_VALUE;
1176+
internals.argv.destination = internals.argv._.shift().toString();
1177+
1178+
if (folder[1]) {
1179+
internals.matching = folder[1];
1180+
internals.migrationMode = folder[1];
1181+
}
1182+
1183+
executeSync(internals, config);
1184+
break;
10981185
case 'up':
10991186
case 'down':
11001187
case 'reset':
@@ -1104,10 +1191,9 @@ function run(internals, config) {
11041191

11051192
if (internals.argv._.length > 0) {
11061193
if (action === 'down') {
1107-
log.info(
1108-
'Ignoring migration name for down migrations. Use --count to control how many down migrations are run.'
1109-
);
1110-
internals.argv.destination = null;
1194+
1195+
internals.argv.count = internals.argv.count || Number.MAX_VALUE;
1196+
internals.argv.destination = internals.argv._.shift().toString();
11111197
} else {
11121198
internals.argv.destination = internals.argv._.shift().toString();
11131199
}
@@ -1166,7 +1252,8 @@ function run(internals, config) {
11661252
}
11671253
else {
11681254

1169-
log.error('Invalid Action: Must be [up|down|create|reset|seed|db].');
1255+
log.error('Invalid Action: Must be [up|down|create|reset|sync|seed|' +
1256+
'db|transition].');
11701257
optimist.showHelp();
11711258
process.exit(1);
11721259
}

lib/migrator.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,35 @@ Migrator.prototype = {
119119
if (dbmUtil.isFunction(funcOrOpts)) {
120120
return funcOrOpts(this.driver, callback);
121121
} else {
122-
this.downToBy(funcOrOpts.count, callback);
122+
this.downToBy(funcOrOpts.destination, funcOrOpts.count, callback);
123123
}
124124
},
125125

126+
sync: function(funcOrOpts, callback) {
127+
128+
var self = this;
129+
130+
Migration.loadFromDatabase(self.migrationsDir, self._driver,
131+
self.internals, function(err, completedMigrations) {
132+
if (err) { callback(err); return; }
133+
134+
var mode = dbmUtil.syncMode(
135+
completedMigrations,
136+
funcOrOpts.destination
137+
);
138+
if(mode === 1) {
139+
140+
log.info('Syncing upwards.');
141+
self.up(funcOrOpts, callback);
142+
}
143+
else {
144+
145+
log.info('Syncing downwards.');
146+
self.down(funcOrOpts, callback);
147+
}
148+
});
149+
},
150+
126151
upToBy: function(partialName, count, callback) {
127152
var self = this;
128153
Migration.loadFromFilesystem(self.migrationsDir, self.internals, function(err, allMigrations) {
@@ -173,12 +198,12 @@ Migrator.prototype = {
173198
});
174199
},
175200

176-
downToBy: function(count, callback) {
201+
downToBy: function(partialName, count, callback) {
177202
var self = this;
178203
Migration.loadFromDatabase(self.migrationsDir, self._driver, self.internals, function(err, completedMigrations) {
179204
if (err) { return callback(err); }
180205

181-
var toRun = dbmUtil.filterDown(completedMigrations, count);
206+
var toRun = dbmUtil.filterDown(completedMigrations, partialName, count);
182207

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"dependencies": {
4747
"balanced-match": "^0.4.2",
4848
"bluebird": "^3.1.1",
49-
"db-migrate-shared": "^1.0.2",
49+
"db-migrate-shared": "^1.1.2",
5050
"dotenv": "^2.0.0",
5151
"final-fs": "^1.6.0",
5252
"inflection": "^1.10.0",

0 commit comments

Comments
 (0)