Use async and make sure errors are passed to callback
This commit is contained in:
parent
1b001010bb
commit
c35a00b808
50
lib/mysql.js
50
lib/mysql.js
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -695,28 +687,28 @@ MySQL.prototype.createTable = function (model, cb) {
|
||||||
* @param {String[]} [models] A model name or an array of model names. If not present, apply to all models
|
* @param {String[]} [models] A model name or an array of model names. If not present, apply to all models
|
||||||
* @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, function(err, needAlter) {
|
||||||
self.alterTable(model, fields, indexes, done, true);
|
if (err) {
|
||||||
|
return done(err);
|
||||||
|
} else {
|
||||||
|
ok = ok || needAlter;
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
}, function(err) {
|
||||||
|
|
||||||
function done(err, needAlter) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
debug(err);
|
return err;
|
||||||
}
|
}
|
||||||
ok = ok || needAlter;
|
cb(null, !ok);
|
||||||
if (--wait === 0 && cb) {
|
});
|
||||||
cb(null, !ok);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, checkOnly) {
|
MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, checkOnly) {
|
||||||
|
|
Loading…
Reference in New Issue