Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): make duplicate index error a warning for now to prevent blocking upgrading #15135

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ Schema.prototype.index = function(fields, options) {

for (const existingIndex of this.indexes()) {
if (options.name == null && existingIndex[1].name == null && isIndexSpecEqual(existingIndex[0], fields)) {
throw new MongooseError(`Schema already has an index on ${JSON.stringify(fields)}`);
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.`);
}
}

Expand Down
55 changes: 35 additions & 20 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const DocumentObjectId = mongoose.Types.ObjectId;
const vm = require('vm');
const idGetter = require('../lib/helpers/schema/idGetter');
const applyPlugins = require('../lib/helpers/schema/applyPlugins');
const utils = require('../lib/utils');

/**
* Test Document constructor.
Expand Down Expand Up @@ -3279,29 +3280,43 @@ describe('schema', function() {
assert.equal(subdoc.getAnswer(), 42);
});
it('throws "already has an index" error if duplicate index definition (gh-15056)', function() {
const ObjectKeySchema = new mongoose.Schema({
key: {
type: String,
required: true,
unique: true
},
type: {
type: String,
required: false
}
});
sinon.stub(utils, 'warn').callsFake(() => {});
try {
const ObjectKeySchema = new mongoose.Schema({
key: {
type: String,
required: true,
unique: true
},
type: {
type: String,
required: false
}
});

assert.throws(() => {
ObjectKeySchema.index({ key: 1 });
}, /MongooseError.*already has an index/);
assert.equal(utils.warn.getCalls().length, 1);
let [message] = utils.warn.getCalls()[0].args;
assert.equal(
message,
'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.'
);

ObjectKeySchema.index({ key: 1, type: 1 });
assert.throws(() => {
ObjectKeySchema.index({ key: 1, type: 1 });
}, /MongooseError.*already has an index/);

ObjectKeySchema.index({ type: 1, key: 1 });
ObjectKeySchema.index({ key: 1, type: -1 });
ObjectKeySchema.index({ key: 1, type: 1 }, { unique: true, name: 'special index' });
assert.equal(utils.warn.getCalls().length, 1);
ObjectKeySchema.index({ key: 1, type: 1 });
assert.equal(utils.warn.getCalls().length, 2);
[message] = utils.warn.getCalls()[1].args;
assert.equal(
message,
'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.'
);

ObjectKeySchema.index({ type: 1, key: 1 });
ObjectKeySchema.index({ key: 1, type: -1 });
ObjectKeySchema.index({ key: 1, type: 1 }, { unique: true, name: 'special index' });
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
} finally {
sinon.restore();
}
});
});
Loading