Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/models/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think record should be an optional property - you need to add something like this just above this line:

    if (typeof cb === 'undefined' && typeof record === 'function') {
      cb = 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()
});
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a call to cb() after a successful migration.

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).
*
Expand Down
19 changes: 19 additions & 0 deletions lib/models/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down