Skip to content

Commit a5a6f05

Browse files
committed
feat(NODE-4243): drop collection checks ns not found
1 parent f8a855f commit a5a6f05

11 files changed

Lines changed: 53 additions & 90 deletions

File tree

src/operations/drop.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,35 @@ export async function dropCollections(
8686
try {
8787
await executeOperation(db.client, dropOp, timeoutContext);
8888
} catch (err) {
89+
console.log(collectionName, err);
8990
if (
9091
!(err instanceof MongoServerError) ||
91-
err.code !== MONGODB_ERROR_CODES.NamespaceNotFound
92+
err.code !== MONGODB_ERROR_CODES.NamespaceNotFound ||
93+
!/ns not found/.test(err.message)
9294
) {
9395
throw err;
9496
}
9597
}
9698
}
9799
}
98100

99-
return await executeOperation(
100-
db.client,
101-
new DropCollectionOperation(db, name, options),
102-
timeoutContext
103-
);
101+
try {
102+
return await executeOperation(
103+
db.client,
104+
new DropCollectionOperation(db, name, options),
105+
timeoutContext
106+
);
107+
} catch (err) {
108+
console.log(name, err);
109+
if (
110+
!(err instanceof MongoServerError) ||
111+
err.code !== MONGODB_ERROR_CODES.NamespaceNotFound ||
112+
!/ns not found/.test(err.message)
113+
) {
114+
throw err;
115+
}
116+
return false;
117+
}
104118
}
105119

106120
/** @public */

test/integration/client-side-encryption/client_side_encryption.prose.22.range_explicit_encryption.test.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,12 @@ describe('Range Explicit Encryption', function () {
171171

172172
await utilClient.db('db').dropDatabase();
173173

174-
await utilClient
175-
.db('db')
176-
.dropCollection('explicit_encryption')
177-
.catch(e => {
178-
if (!/ns not found/.test(e.message)) {
179-
throw e;
180-
}
181-
});
174+
await utilClient.db('db').dropCollection('explicit_encryption');
182175
await utilClient.db('db').createCollection('explicit_encryption', {
183176
encryptedFields
184177
});
185178

186-
await utilClient
187-
.db('keyvault')
188-
.dropCollection('datakeys')
189-
.catch(e => {
190-
if (!/ns not found/.test(e.message)) {
191-
throw e;
192-
}
193-
});
179+
await utilClient.db('keyvault').dropCollection('datakeys');
194180

195181
await utilClient.db('keyvault').createCollection('datakeys');
196182

test/integration/client-side-encryption/client_side_encryption.spec.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../../tools/spec-runner';
99
import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';
1010

11-
describe('Client Side Encryption (Legacy)', function () {
11+
describe.only('Client Side Encryption (Legacy)', function () {
1212
const testContext = new TestRunnerContext({ requiresCSFLE: true });
1313
const testSuites = gatherTestSuites(
1414
path.join(__dirname, '../../spec/client-side-encryption/tests/legacy'),

test/integration/command-logging-and-monitoring/command_monitoring.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22

33
import { type MongoClient, ObjectId, ReadPreference } from '../../mongodb';
4-
import { filterForCommands, ignoreNsNotFound, setupDatabase } from '../shared';
4+
import { filterForCommands, setupDatabase } from '../shared';
55

66
describe('Command Monitoring', function () {
77
let client: MongoClient;
@@ -164,7 +164,6 @@ describe('Command Monitoring', function () {
164164
return db
165165
.collection('apm_test_2')
166166
.drop()
167-
.catch(ignoreNsNotFound)
168167
.then(() => {
169168
// Insert test documents
170169
return db
@@ -235,7 +234,6 @@ describe('Command Monitoring', function () {
235234
return db
236235
.collection('apm_test_2')
237236
.drop()
238-
.catch(ignoreNsNotFound)
239237
.then(() => {
240238
// Insert test documents
241239
return db
@@ -328,7 +326,6 @@ describe('Command Monitoring', function () {
328326
return db
329327
.collection('apm_test_2')
330328
.drop()
331-
.catch(ignoreNsNotFound)
332329
.then(() =>
333330
db
334331
.collection('apm_test_2')
@@ -531,12 +528,10 @@ describe('Command Monitoring', function () {
531528
const desiredEvents = ['aggregate', 'getMore'];
532529
client.on('commandStarted', filterForCommands(desiredEvents, started));
533530
client.on('commandSucceeded', filterForCommands(desiredEvents, succeeded));
534-
535531
const db = client.db(this.configuration.db);
536532
return db
537533
.collection('apm_test_u_4')
538534
.drop()
539-
.catch(ignoreNsNotFound)
540535
.then(() => db.collection('apm_test_u_4').insertMany(docs))
541536
.then(r => {
542537
expect(r).to.exist;

test/integration/crud/bulk.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
MongoDriverError,
1313
MongoInvalidArgumentError
1414
} from '../../../src';
15-
import { assert as test, ignoreNsNotFound } from '../shared';
15+
import { assert as test } from '../shared';
1616

1717
const MAX_BSON_SIZE = 16777216;
1818
const DB_NAME = 'bulk_operations_tests';
@@ -1474,7 +1474,7 @@ describe('Bulk', function () {
14741474

14751475
it('should promote a single error to the top-level message, and preserve writeErrors', async function () {
14761476
const coll = client.db().collection<{ _id: number; a: number }>('single_bulk_write_error');
1477-
await coll.drop().catch(ignoreNsNotFound);
1477+
await coll.drop();
14781478
await coll.insertMany(Array.from({ length: 4 }, (_, i) => ({ _id: i, a: i })));
14791479
const err = await coll
14801480
.bulkWrite([
@@ -1490,7 +1490,7 @@ describe('Bulk', function () {
14901490

14911491
it('should preserve order of operation index in unordered bulkWrite', async function () {
14921492
const coll = client.db().collection<{ _id: number; a: number }>('bulk_write_ordering_test');
1493-
await coll.drop().catch(ignoreNsNotFound);
1493+
await coll.drop();
14941494
await coll.insertMany(Array.from({ length: 4 }, (_, i) => ({ _id: i, a: i })));
14951495
await coll.createIndex({ a: 1 }, { unique: true });
14961496
const err = await coll
@@ -1513,7 +1513,7 @@ describe('Bulk', function () {
15131513

15141514
it('should preserve order of operation index in unordered bulk operation', async function () {
15151515
const coll = client.db().collection('unordered_preserve_order');
1516-
await coll.drop().catch(ignoreNsNotFound);
1516+
await coll.drop();
15171517
const batch = coll.initializeUnorderedBulkOp();
15181518
batch.insert({ _id: 1, a: 0 });
15191519
batch.insert({ _id: 1, a: 0 });
@@ -1528,7 +1528,7 @@ describe('Bulk', function () {
15281528

15291529
it('should not fail on the first error in an unorderd bulkWrite', async function () {
15301530
const coll = client.db().collection('bulk_op_ordering_test');
1531-
await coll.drop().catch(ignoreNsNotFound);
1531+
await coll.drop();
15321532
await coll.createIndex({ email: 1 }, { unique: true, background: false });
15331533
await Promise.all([
15341534
coll.updateOne(

test/integration/crud/document_validation.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22

33
import { MongoBulkWriteError, type MongoClient, MongoServerError } from '../../../src';
4-
import { ignoreNsNotFound, setupDatabase } from '../shared';
4+
import { setupDatabase } from '../shared';
55

66
describe('Document Validation', function () {
77
let client: MongoClient;
@@ -28,7 +28,7 @@ describe('Document Validation', function () {
2828
const col = db.collection('createValidationCollection');
2929

3030
// Drop the collection
31-
await col.drop().catch(ignoreNsNotFound);
31+
await col.drop();
3232
// Create a collection with a validator
3333
await db.createCollection('createValidationCollection', {
3434
validator: { a: { $exists: true } }
@@ -56,7 +56,7 @@ describe('Document Validation', function () {
5656
const col = db.collection('createValidationCollection');
5757

5858
// Drop the collection
59-
await col.drop().catch(ignoreNsNotFound);
59+
await col.drop();
6060
// Create a collection with a validator
6161
await db.createCollection('createValidationCollection', {
6262
validator: { a: { $exists: true } }
@@ -97,7 +97,7 @@ describe('Document Validation', function () {
9797
const col = db.collection('createValidationCollection');
9898

9999
// Drop the collection
100-
await col.drop().catch(ignoreNsNotFound);
100+
await col.drop();
101101
// Create a collection with a validator
102102
await db.createCollection('createValidationCollection', {
103103
validator: { a: { $exists: true } }
@@ -123,7 +123,7 @@ describe('Document Validation', function () {
123123
const col = db.collection('createValidationCollection');
124124

125125
// Drop the collection
126-
await col.drop().catch(ignoreNsNotFound);
126+
await col.drop();
127127
// Create a collection with a validator
128128
await db.createCollection('createValidationCollection', {
129129
validator: { a: { $exists: true } }
@@ -176,7 +176,7 @@ describe('Document Validation', function () {
176176
const col = db.collection('createValidationCollectionOut');
177177

178178
// Drop the collection
179-
await col.drop().catch(ignoreNsNotFound);
179+
await col.drop();
180180
// Create a collection with a validator
181181
await db.createCollection('createValidationCollectionOut', {
182182
validator: { a: { $exists: true } }

test/integration/crud/insert.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
Timestamp
2424
} from '../../../src';
2525
import { noop } from '../../../src/utils';
26-
import { assert as test, ignoreNsNotFound, setupDatabase } from '../shared';
26+
import { assert as test, setupDatabase } from '../shared';
2727

2828
describe('crud - insert', function () {
2929
let client: MongoClient;
@@ -1590,7 +1590,7 @@ describe('crud - insert', function () {
15901590
});
15911591

15921592
it('MongoBulkWriteError and BulkWriteResult should respect BulkWrite', async function () {
1593-
await client.db().collection('test_insertMany_bulkResult').drop().catch(ignoreNsNotFound);
1593+
await client.db().collection('test_insertMany_bulkResult').drop();
15941594

15951595
const collection = client
15961596
.db()

test/integration/node-specific/bson-options/ignore_undefined.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from 'chai';
22

33
import { type MongoClient, ObjectId } from '../../../../src';
4-
import { assert as test, ignoreNsNotFound, setupDatabase } from '../../shared';
4+
import { assert as test, setupDatabase } from '../../shared';
55

66
describe('Ignore Undefined', function () {
77
before(function () {
@@ -181,7 +181,7 @@ describe('Ignore Undefined', function () {
181181
describe('ignoreUndefined A server', function () {
182182
it('should correctly execute insert culling undefined', async function () {
183183
const coll = client.db().collection('insert1');
184-
await coll.drop().catch(ignoreNsNotFound);
184+
await coll.drop().catch();
185185
const objectId = new ObjectId();
186186
const res = await coll.insertOne(
187187
{ _id: objectId, a: 1, b: undefined },
@@ -197,7 +197,7 @@ describe('Ignore Undefined', function () {
197197

198198
it('should correctly execute update culling undefined', async function () {
199199
const coll = client.db().collection('update1');
200-
await coll.drop().catch(ignoreNsNotFound);
200+
await coll.drop();
201201
const objectId = new ObjectId();
202202
const res = await coll.updateOne(
203203
{ _id: objectId, a: 1, b: undefined },
@@ -214,7 +214,7 @@ describe('Ignore Undefined', function () {
214214

215215
it('should correctly execute remove culling undefined', async function () {
216216
const coll = client.db().collection('remove1');
217-
await coll.drop().catch(ignoreNsNotFound);
217+
await coll.drop();
218218
const objectId = new ObjectId();
219219
const res = await coll.insertMany([
220220
{ id: objectId, a: 1, b: undefined },
@@ -228,7 +228,7 @@ describe('Ignore Undefined', function () {
228228

229229
it('should correctly execute remove not culling undefined', async function () {
230230
const coll = client.db().collection('remove1');
231-
await coll.drop().catch(ignoreNsNotFound);
231+
await coll.drop();
232232
const objectId = new ObjectId();
233233
const res = await coll.insertMany([
234234
{ id: objectId, a: 1, b: undefined },

test/integration/shared.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function delay(timeout) {
3939
}
4040

4141
function dropCollection(dbObj, collectionName, options = {}) {
42-
return dbObj.dropCollection(collectionName, options).catch(ignoreNsNotFound);
42+
return dbObj.dropCollection(collectionName, options);
4343
}
4444

4545
/**
@@ -86,10 +86,6 @@ function filterOutCommands(commands, bag) {
8686
};
8787
}
8888

89-
function ignoreNsNotFound(err) {
90-
if (!err.message.match(/ns not found/)) throw err;
91-
}
92-
9389
async function setupDatabase(configuration, dbsToClean) {
9490
dbsToClean = Array.isArray(dbsToClean) ? dbsToClean : [];
9591
const configDbName = configuration.db;
@@ -204,7 +200,6 @@ module.exports = {
204200
dropCollection,
205201
filterForCommands,
206202
filterOutCommands,
207-
ignoreNsNotFound,
208203
setupDatabase,
209204
withCursor,
210205
APMEventCollector

test/tools/spec-runner/index.js

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -283,26 +283,16 @@ function prepareDatabaseForSuite(suite, context) {
283283
}
284284
return coll.drop(options);
285285
})
286-
.catch(err => {
287-
if (!err.message.match(/ns not found/)) throw err;
288-
})
289286
.then(() => {
290287
if (suite.key_vault_data) {
291288
const dataKeysCollection = context.sharedClient.db('keyvault').collection('datakeys');
292-
return dataKeysCollection
293-
.drop({ writeConcern: { w: 'majority' } })
294-
.catch(err => {
295-
if (!err.message.match(/ns not found/)) {
296-
throw err;
297-
}
298-
})
299-
.then(() => {
300-
if (suite.key_vault_data.length) {
301-
return dataKeysCollection.insertMany(suite.key_vault_data, {
302-
writeConcern: { w: 'majority' }
303-
});
304-
}
305-
});
289+
return dataKeysCollection.drop({ writeConcern: { w: 'majority' } }).then(() => {
290+
if (suite.key_vault_data.length) {
291+
return dataKeysCollection.insertMany(suite.key_vault_data, {
292+
writeConcern: { w: 'majority' }
293+
});
294+
}
295+
});
306296
}
307297
})
308298
.then(() => {
@@ -698,11 +688,7 @@ const kOperations = new Map([
698688
const collectionName = operation.arguments.collection;
699689
const encryptedFields = operation.arguments.encryptedFields;
700690
const session = maybeSession(operation, context);
701-
return db.dropCollection(collectionName, { session, encryptedFields }).catch(err => {
702-
if (!err.message.match(/ns not found/)) {
703-
throw err;
704-
}
705-
});
691+
return db.dropCollection(collectionName, { session, encryptedFields });
706692
}
707693
],
708694
[
@@ -783,12 +769,7 @@ const kOperations = new Map([
783769
.toArray()
784770
.then(results => results.map(({ name }) => name))
785771
.then(indexes => expect(indexes).to.not.include(indexName))
786-
).catch(err => {
787-
// The error message can differ slightly with the same error code.
788-
if (!err.message.match(/ns not found|ns does not exist/)) {
789-
throw err;
790-
}
791-
});
772+
);
792773
}
793774
]
794775
]);

0 commit comments

Comments
 (0)