diff --git a/lib/datasource.js b/lib/datasource.js index 56550ca0..549e9734 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -830,9 +830,12 @@ DataSource.prototype.autoupdate = function (models, cb) { models = undefined; } + cb = cb || utils.createPromiseCallback(); + if (!this.connector.autoupdate) { // NOOP - return cb && process.nextTick(cb); + process.nextTick(cb); + return cb.promise; } // First argument is a model name @@ -846,7 +849,8 @@ DataSource.prototype.autoupdate = function (models, cb) { models = models || Object.keys(attachedModels); if (models.length === 0) { - return process.nextTick(cb); + process.nextTick(cb); + return cb.promise; } var invalidModels = models.filter(function (m) { @@ -854,16 +858,16 @@ DataSource.prototype.autoupdate = function (models, cb) { }); if (invalidModels.length) { - return process.nextTick(function () { - if (cb) { - cb(new Error('Cannot migrate models not attached to this datasource: ' + - invalidModels.join(' '))); - } + process.nextTick(function () { + cb(new Error('Cannot migrate models not attached to this datasource: ' + + invalidModels.join(' '))); }); + return cb.promise; } } this.connector.autoupdate(models, cb); + return cb.promise; }; /** diff --git a/test/memory.test.js b/test/memory.test.js index 2f0909bc..158a6f43 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -548,6 +548,95 @@ describe('Memory connector', function() { }); }); + describe('With mocked autoupdate', function() { + var ds, model; + beforeEach(function() { + ds = new DataSource({ + connector: 'memory' + }); + + ds.connector.autoupdate = function(models, cb) { + process.nextTick(cb); + }; + + model = ds.createModel('m1', { + name: String + }); + + ds.automigrate(); + + ds.createModel('m1', { + name: String, + address: String + }); + }); + + it('autoupdates all models', function(done) { + ds.autoupdate(function(err, result){ + done(err); + }); + }); + + it('autoupdates all models - promise variant', function(done) { + ds.autoupdate() + .then(function(result) { + done(); + }) + .catch(function(err){ + done(err); + }); + }); + + it('autoupdates one model', function(done) { + ds.autoupdate('m1', function(err) { + done(err); + }); + }); + + it('autoupdates one model - promise variant', function(done) { + ds.autoupdate('m1') + .then(function(result) { + done(); + }) + .catch(function(err){ + done(err); + }); + }); + + it('autoupdates one or more models in an array', function(done) { + ds.autoupdate(['m1'], function(err) { + done(err); + }); + }); + + it('autoupdates one or more models in an array - promise variant', function(done) { + ds.autoupdate(['m1']) + .then(function(result) { + done(); + }) + .catch(function(err){ + done(err); + }); + }); + + it('autoupdate reports errors for models not attached', function(done) { + ds.autoupdate(['m1', 'm2'], function(err) { + err.should.be.an.instanceOf(Error); + done(); + }); + }); + + it('autoupdate reports errors for models not attached - promise variant', function(done) { + ds.autoupdate(['m1', 'm2']) + .then(function(){ + done(new Error('automigrate() should have failed')); + }) + .catch(function(err){ + err.should.be.an.instanceOf(Error); + done(); + }); + }); + }); }); describe('Optimized connector', function() {