Skip to content

Commit

Permalink
Merge pull request #14029 from Automattic/IslandRhythms/gh14010
Browse files Browse the repository at this point in the history
fix: diffIndexes treats namespace error as empty
  • Loading branch information
vkarpov15 authored Nov 2, 2023
2 parents 5821568 + 1e9cca9 commit 3ccad37
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,12 @@ Model.diffIndexes = async function diffIndexes() {

const model = this;

let dbIndexes = await model.listIndexes();
let dbIndexes = await model.listIndexes().catch(err => {
if (err.codeName == 'NamespaceNotFound') {
return undefined;
}
throw err;
});
if (dbIndexes === undefined) {
dbIndexes = [];
}
Expand Down
10 changes: 10 additions & 0 deletions test/model.indexes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,5 +714,15 @@ describe('model', function() {
assert.deepStrictEqual(result.toDrop, ['age_1', 'weight_1']);
assert.deepStrictEqual(result.toCreate, [{ password: 1 }, { email: 1 }]);
});

it('running diffIndexes with a non-existent collection should not throw an error (gh-14010)', async function() {
const testSchema = new mongoose.Schema({
name: String
});

const Test = db.model('gh14010', testSchema);
const res = await Test.diffIndexes();
assert.ok(res);
});
});
});

0 comments on commit 3ccad37

Please sign in to comment.