Use async and make sure errors are passed to callback

This commit is contained in:
Raymond Feng 2014-09-10 23:40:51 -07:00
parent 1b001010bb
commit c35a00b808
1 changed files with 21 additions and 29 deletions

View File

@ -6,6 +6,7 @@ var mysql = require('mysql');
var SqlConnector = require('loopback-connector').SqlConnector; var SqlConnector = require('loopback-connector').SqlConnector;
var EnumFactory = require('./enumFactory').EnumFactory; var EnumFactory = require('./enumFactory').EnumFactory;
var async = require('async');
var debug = require('debug')('loopback:connector:mysql'); var debug = require('debug')('loopback:connector:mysql');
/** /**
@ -637,7 +638,7 @@ MySQL.prototype.destroyAll = function destroyAll(model, where, callback) {
*/ */
MySQL.prototype.autoupdate = function (models, cb) { MySQL.prototype.autoupdate = function (models, cb) {
var self = this; var self = this;
var wait = 0;
if ((!cb) && ('function' === typeof models)) { if ((!cb) && ('function' === typeof models)) {
cb = models; cb = models;
models = undefined; models = undefined;
@ -649,9 +650,8 @@ MySQL.prototype.autoupdate = function (models, cb) {
models = models || Object.keys(this._models); models = models || Object.keys(this._models);
models.forEach(function (model) { async.each(models, function (model, done) {
if (model in self._models) { if (model in self._models) {
wait++;
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) { self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) { self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) {
if (!err && fields && fields.length) { if (!err && fields && fields.length) {
@ -662,16 +662,8 @@ MySQL.prototype.autoupdate = function (models, cb) {
}); });
}); });
} }
}); }, cb);
function done(err) {
if (err) {
console.error('%j', err);
}
if (--wait === 0 && cb) {
cb();
}
}
}; };
/*! /*!
@ -696,27 +688,27 @@ MySQL.prototype.createTable = function (model, cb) {
* @param {Function} [cb] The callback function * @param {Function} [cb] The callback function
*/ */
MySQL.prototype.isActual = function(cb) { MySQL.prototype.isActual = function(cb) {
var ok = false;
var self = this; var self = this;
var wait = 0; var ok = false;
Object.keys(this._models).forEach(function (model) { async.each(Object.keys(this._models), function(model, done) {
wait += 1;
self.query('SHOW FIELDS FROM ' + model, function(err, fields) { self.query('SHOW FIELDS FROM ' + model, function(err, fields) {
self.query('SHOW INDEXES FROM ' + model, function(err, indexes) { self.query('SHOW INDEXES FROM ' + model, function(err, indexes) {
self.alterTable(model, fields, indexes, done, true); self.alterTable(model, fields, indexes, function(err, needAlter) {
});
});
});
function done(err, needAlter) {
if (err) { if (err) {
debug(err); return done(err);
} } else {
ok = ok || needAlter; ok = ok || needAlter;
if (--wait === 0 && cb) { done(err);
}
}, true);
});
});
}, function(err) {
if (err) {
return err;
}
cb(null, !ok); cb(null, !ok);
} });
}
}; };
MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, checkOnly) { MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, checkOnly) {