diff --git a/lib/models/migration.js b/lib/models/migration.js index e269325..3ad26b1 100644 --- a/lib/models/migration.js +++ b/lib/models/migration.js @@ -44,6 +44,50 @@ module.exports = function(Migration, options) { return cb.promise; }; + /** + * Remote Method: Run specific migration by name. + * + * @param {String} [name] Name of migration script to run. + * @param {String} [record] Record the migration runtime to database. + * @param {Function} [cb] Callback function. + */ + Migration.migrateByName = function(name, record, cb) { + if (typeof cb === 'undefined' && typeof record === 'function') { + cb = record; + record = false; + } + + record = record || false; + cb = cb || utils.createPromiseCallback(); + assert(typeof name === 'string', 'The to argument must be a string, not ' + typeof name); + assert(typeof cb === 'function', 'The cb argument must be a function, not ' + typeof cb); + + Migration.log.info(name, 'running.'); + const scriptPath = path.resolve(path.join(Migration.migrationsDir, name)); + + try { + require(scriptPath).up(Migration.app, function(err) { + if (err) { + Migration.log.error(`Error running migration script ${name}:`, err); + Migration.finish(err); + return cb(err); + } else if (record) { + Migration.create({ + name: name, + runDtTm: new Date() + }); + } + cb(); + }); + } catch (err) { + Migration.log.error(`Error running migration script ${name}:`, err); + Migration.finish(err); + cb(err); + } + + return cb.promise; + }; + /** * Run migrations (up or down). * diff --git a/lib/models/migration.json b/lib/models/migration.json index 5ba964b..8c0fba0 100644 --- a/lib/models/migration.json +++ b/lib/models/migration.json @@ -34,6 +34,25 @@ "description": "Name of the migration script to migrate to. If no name is provided all pending migrations will run." }] }, + "migrateByName": { + "description": "Run specific migration by name", + "isStatic": true, + "http": { + "path": "/migrateByName", + "verb": "get" + }, + "accepts": [{ + "arg": "name", + "type": "String", + "required": true, + "description": "Name of the migration script to run." + }, + { + "arg" : "record", + "type" : "boolean", + "required" : false + }] + }, "rollbackTo": { "description": "Rollback migrations", "isStatic": true,