Skip to content

Commit 3ccad37

Browse files
authored
Merge pull request #14029 from Automattic/IslandRhythms/gh14010
fix: diffIndexes treats namespace error as empty
2 parents 5821568 + 1e9cca9 commit 3ccad37

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/model.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,12 @@ Model.diffIndexes = async function diffIndexes() {
15261526

15271527
const model = this;
15281528

1529-
let dbIndexes = await model.listIndexes();
1529+
let dbIndexes = await model.listIndexes().catch(err => {
1530+
if (err.codeName == 'NamespaceNotFound') {
1531+
return undefined;
1532+
}
1533+
throw err;
1534+
});
15301535
if (dbIndexes === undefined) {
15311536
dbIndexes = [];
15321537
}

test/model.indexes.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,5 +714,15 @@ describe('model', function() {
714714
assert.deepStrictEqual(result.toDrop, ['age_1', 'weight_1']);
715715
assert.deepStrictEqual(result.toCreate, [{ password: 1 }, { email: 1 }]);
716716
});
717+
718+
it('running diffIndexes with a non-existent collection should not throw an error (gh-14010)', async function() {
719+
const testSchema = new mongoose.Schema({
720+
name: String
721+
});
722+
723+
const Test = db.model('gh14010', testSchema);
724+
const res = await Test.diffIndexes();
725+
assert.ok(res);
726+
});
717727
});
718728
});

0 commit comments

Comments
 (0)