From 90a3450488be3b1b33940d7d53bc899f67a83eca Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 8 Aug 2013 08:35:29 -0700 Subject: [PATCH] Add optional models argument to autoupdate --- lib/mysql.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index db0c09a..700eeec 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -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 wait = 0; - Object.keys(this._models).forEach(function (model) { - wait += 1; - 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); - } + if ((!cb) && ('function' === typeof models)) { + cb = models; + models = undefined; + } + // First argument is a model name + if ('string' === typeof models) { + models = [models]; + } + + 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) { if (err) { - console.log(err); + console.error(err); } if (--wait === 0 && cb) { cb();