@@ -303,6 +303,36 @@ dbmigrate.prototype = {
303
303
} . bind ( this ) ) . asCallback ( callback ) ;
304
304
} ,
305
305
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
+
306
336
/**
307
337
* Executes down for all currently migrated migrations.
308
338
*/
@@ -520,7 +550,8 @@ function setDefaultArgv(internals, isModule) {
520
550
'ignore-completed-migrations' : false
521
551
} )
522
552
. 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]'
524
555
)
525
556
526
557
. describe ( 'env' ,
@@ -558,14 +589,16 @@ function setDefaultArgv(internals, isModule) {
558
589
. string ( 'config' )
559
590
560
591
. 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.'
562
594
)
563
595
. boolean ( 'sql-file' )
564
596
565
597
. describe ( 'coffee-file' , 'Create a coffeescript migration file' )
566
598
. boolean ( 'coffee-file' )
567
599
. 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)'
569
602
) . boolean ( 'ignore-on-init' )
570
603
571
604
. describe ( 'migration-table' ,
@@ -883,6 +916,42 @@ function executeUp(internals, config, callback) {
883
916
} ) ;
884
917
}
885
918
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
+
886
955
function executeDown ( internals , config , callback ) {
887
956
888
957
migrationHook ( internals )
@@ -1095,6 +1164,24 @@ function run(internals, config) {
1095
1164
}
1096
1165
executeCreateMigration ( internals , config ) ;
1097
1166
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 ;
1098
1185
case 'up' :
1099
1186
case 'down' :
1100
1187
case 'reset' :
@@ -1104,10 +1191,9 @@ function run(internals, config) {
1104
1191
1105
1192
if ( internals . argv . _ . length > 0 ) {
1106
1193
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 ( ) ;
1111
1197
} else {
1112
1198
internals . argv . destination = internals . argv . _ . shift ( ) . toString ( ) ;
1113
1199
}
@@ -1166,7 +1252,8 @@ function run(internals, config) {
1166
1252
}
1167
1253
else {
1168
1254
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].' ) ;
1170
1257
optimist . showHelp ( ) ;
1171
1258
process . exit ( 1 ) ;
1172
1259
}
0 commit comments