Skip to content

Commit

Permalink
fix(model): always emit 'index', even if no indexes
Browse files Browse the repository at this point in the history
Re: #3347
  • Loading branch information
vkarpov15 committed May 15, 2017
1 parent 5908ef0 commit 25c350f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
17 changes: 9 additions & 8 deletions lib/model.js
Expand Up @@ -929,14 +929,6 @@ Model.ensureIndexes = function ensureIndexes(options, callback) {

function _ensureIndexes(model, options, callback) {
var indexes = model.schema.indexes();
if (!indexes.length) {
setImmediate(function() {
callback && callback();
});
return;
}
// Indexes are created one-by-one to support how MongoDB < 2.4 deals
// with background indexes.

var done = function(err) {
if (err && model.schema.options.emitIndexErrors) {
Expand All @@ -946,6 +938,15 @@ function _ensureIndexes(model, options, callback) {
callback && callback(err);
};

if (!indexes.length) {
setImmediate(function() {
done();
});
return;
}
// Indexes are created one-by-one to support how MongoDB < 2.4 deals
// with background indexes.

var indexSingleDone = function(err, fields, options, name) {
model.emit('index-single-done', err, fields, options, name);
};
Expand Down
13 changes: 7 additions & 6 deletions test/model.indexes.test.js
Expand Up @@ -226,16 +226,17 @@ describe('model', function() {
schema = new Schema({name: {type: String}}),
Test = db.model('IndexError', schema, 'x' + random());

Test.on('index', function(err) {
db.close();
assert.ok(/E11000 duplicate key error/.test(err.message), err);
done();
});

Test.create({name: 'hi'}, {name: 'hi'}, function(err) {
assert.strictEqual(err, null);
Test.schema.index({name: 1}, {unique: true});
Test.schema.index({other: 1});

Test.on('index', function(err) {
db.close();
assert.ok(/E11000 duplicate key error/.test(err.message), err);
done();
});

Test.init();
});
});
Expand Down

0 comments on commit 25c350f

Please sign in to comment.