Add optional models argument to autoupdate

This commit is contained in:
Raymond Feng 2013-08-08 08:35:29 -07:00
parent 9daf9ef91c
commit 90a3450488
1 changed files with 25 additions and 12 deletions

View File

@ -394,25 +394,38 @@ MySQL.prototype.all = function all(model, filter, callback) {
}; };
MySQL.prototype.autoupdate = function (cb) { MySQL.prototype.autoupdate = function (models, cb) {
var self = this; var self = this;
var wait = 0; var wait = 0;
Object.keys(this._models).forEach(function (model) { if ((!cb) && ('function' === typeof models)) {
wait += 1; cb = models;
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) { models = undefined;
self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) { }
if (!err && fields.length) { // First argument is a model name
self.alterTable(model, fields, indexes, done); if ('string' === typeof models) {
} else { models = [models];
self.createTable(model, done); }
}
models = models || Object.keys(this._models);
models.forEach(function (model) {
if (model in self._models) {
wait++;
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) {
if (!err && fields.length) {
self.alterTable(model, fields, indexes, done);
} else {
self.createTable(model, done);
}
});
}); });
}); }
}); });
function done(err) { function done(err) {
if (err) { if (err) {
console.log(err); console.error(err);
} }
if (--wait === 0 && cb) { if (--wait === 0 && cb) {
cb(); cb();