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: diffIndexes treats namespace error as empty #14029

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);
});
});
});