loopback-component-migrate
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -48,2 +48,46 @@ 'use strict'; | ||
/** | ||
* 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). | ||
@@ -50,0 +94,0 @@ * |
@@ -37,2 +37,21 @@ { | ||
}, | ||
"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": { | ||
@@ -39,0 +58,0 @@ "description": "Rollback migrations", |
{ | ||
"name": "loopback-component-migrate", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Migration framework for Loopback.", | ||
@@ -87,2 +87,2 @@ "author": { | ||
} | ||
} | ||
} |
34062
790
9