Skip to content

Commit c62b72e

Browse files
authored
Merge pull request #15135 from Automattic/vkarpov15/gh-15109-2
fix(schema): make duplicate index error a warning for now to prevent blocking upgrading
2 parents 8a0f581 + a5e4e61 commit c62b72e

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

lib/schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ Schema.prototype.index = function(fields, options) {
21482148

21492149
for (const existingIndex of this.indexes()) {
21502150
if (options.name == null && existingIndex[1].name == null && isIndexSpecEqual(existingIndex[0], fields)) {
2151-
throw new MongooseError(`Schema already has an index on ${JSON.stringify(fields)}`);
2151+
utils.warn(`Duplicate schema index on ${JSON.stringify(fields)} found. This is often due to declaring an index using both "index: true" and "schema.index()". Please remove the duplicate index definition.`);
21522152
}
21532153
}
21542154

test/schema.test.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const DocumentObjectId = mongoose.Types.ObjectId;
1919
const vm = require('vm');
2020
const idGetter = require('../lib/helpers/schema/idGetter');
2121
const applyPlugins = require('../lib/helpers/schema/applyPlugins');
22+
const utils = require('../lib/utils');
2223

2324
/**
2425
* Test Document constructor.
@@ -3279,29 +3280,44 @@ describe('schema', function() {
32793280
assert.equal(subdoc.getAnswer(), 42);
32803281
});
32813282
it('throws "already has an index" error if duplicate index definition (gh-15056)', function() {
3282-
const ObjectKeySchema = new mongoose.Schema({
3283-
key: {
3284-
type: String,
3285-
required: true,
3286-
unique: true
3287-
},
3288-
type: {
3289-
type: String,
3290-
required: false
3291-
}
3292-
});
3283+
sinon.stub(utils, 'warn').callsFake(() => {});
3284+
try {
3285+
const ObjectKeySchema = new mongoose.Schema({
3286+
key: {
3287+
type: String,
3288+
required: true,
3289+
unique: true
3290+
},
3291+
type: {
3292+
type: String,
3293+
required: false
3294+
}
3295+
});
32933296

3294-
assert.throws(() => {
32953297
ObjectKeySchema.index({ key: 1 });
3296-
}, /MongooseError.*already has an index/);
3298+
assert.equal(utils.warn.getCalls().length, 1);
3299+
let [message] = utils.warn.getCalls()[0].args;
3300+
assert.equal(
3301+
message,
3302+
'Duplicate schema index on {"key":1} found. This is often due to declaring an index using both "index: true" and "schema.index()". Please remove the duplicate index definition.'
3303+
);
32973304

3298-
ObjectKeySchema.index({ key: 1, type: 1 });
3299-
assert.throws(() => {
33003305
ObjectKeySchema.index({ key: 1, type: 1 });
3301-
}, /MongooseError.*already has an index/);
3302-
3303-
ObjectKeySchema.index({ type: 1, key: 1 });
3304-
ObjectKeySchema.index({ key: 1, type: -1 });
3305-
ObjectKeySchema.index({ key: 1, type: 1 }, { unique: true, name: 'special index' });
3306+
assert.equal(utils.warn.getCalls().length, 1);
3307+
ObjectKeySchema.index({ key: 1, type: 1 });
3308+
assert.equal(utils.warn.getCalls().length, 2);
3309+
[message] = utils.warn.getCalls()[1].args;
3310+
assert.equal(
3311+
message,
3312+
'Duplicate schema index on {"key":1,"type":1} found. This is often due to declaring an index using both "index: true" and "schema.index()". Please remove the duplicate index definition.'
3313+
);
3314+
3315+
ObjectKeySchema.index({ type: 1, key: 1 });
3316+
ObjectKeySchema.index({ key: 1, type: -1 });
3317+
ObjectKeySchema.index({ key: 1, type: 1 }, { unique: true, name: 'special index' });
3318+
assert.equal(utils.warn.getCalls().length, 2);
3319+
} finally {
3320+
sinon.restore();
3321+
}
33063322
});
33073323
});

0 commit comments

Comments
 (0)