Skip to content

Instantly share code, notes, and snippets.

@anatoliychakkaev
Created March 15, 2012 16:07
Show Gist options
  • Save anatoliychakkaev/2044984 to your computer and use it in GitHub Desktop.
Save anatoliychakkaev/2044984 to your computer and use it in GitHub Desktop.
RailwayJS database tools
exports.init = function () {
railway.tools.database = function db() {
var action = process.argv[3];
switch (action) {
case 'migrate':
case 'update':
perform(action, process.exit);
break;
default:
console.log('Unknown action', action);
break;
}
};
railway.tools.database.help = {
shortcut: 'db',
usage: 'db [migrate|update]',
description: 'Run database migrations'
};
};
function getUniqueSchemas() {
var schemas = [];
Object.keys(app.models).forEach(function (modelName) {
var Model = app.models[modelName];
var schema = Model.schema;
if (!~schemas.indexOf(schema)) {
schemas.push(schema);
}
});
return schemas;
}
function perform(action, callback) {
console.log('Perform', action, 'on');
var wait = 0;
getUniqueSchemas().forEach(function (schema) {
if (schema['auto' + action]) {
console.log(' - ' + schema.name);
wait += 1;
process.nextTick(function () {
schema['auto' + action](done);
});
}
});
if (wait === 0) done(); else console.log(wait);
function done() {
if (--wait === 0) callback();
}
}
require('db-tools');
@anatoliychakkaev
Copy link
Author

Put db-tools.js to node_modules dir, update npmfile.js

@joelongstreet
Copy link

The PG module throws and error. It can't find the log property.

If you comment it out it works brilliantly!

@anatoliychakkaev
Copy link
Author

Yep, I got this error. This is related to connection timeout. It will be fixed in next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment