Skip to content
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
20 changes: 9 additions & 11 deletions src/operations/drop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Connection, MongoServerError } from '..';
import { type Connection, type MongoError, MongoServerError } from '..';
import type { Document } from '../bson';
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
import { CursorTimeoutContext } from '../cursor/abstract_cursor';
Expand Down Expand Up @@ -40,6 +40,13 @@ export class DropCollectionOperation extends CommandOperation<boolean> {
override handleOk(_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): boolean {
return true;
}

override handleError(error: MongoError): boolean {
if (!(error instanceof MongoServerError)) throw error;
if (Number(error.code) !== MONGODB_ERROR_CODES.NamespaceNotFound) throw error;

return false;
}
}

export async function dropCollections(
Expand Down Expand Up @@ -83,16 +90,7 @@ export async function dropCollections(
for (const collectionName of [escCollection, ecocCollection]) {
// Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
const dropOp = new DropCollectionOperation(db, collectionName, options);
try {
await executeOperation(db.client, dropOp, timeoutContext);
} catch (err) {
if (
!(err instanceof MongoServerError) ||
err.code !== MONGODB_ERROR_CODES.NamespaceNotFound
) {
throw err;
}
}
await executeOperation(db.client, dropOp, timeoutContext);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/driver_bench/src/driver.mts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class DriverTester {
const utilClient = new MongoClient(MONGODB_URI, MONGODB_CLIENT_OPTIONS);
const db = utilClient.db(DB_NAME);
const collection = db.collection(COLLECTION_NAME);
await collection.drop().catch(() => null);
await collection.drop();
await db.dropDatabase().catch(() => null);
await utilClient.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function before() {

bucket = driver.bucket(driver.client.db(driver.DB_NAME));

await bucket.drop().catch(() => null);
await bucket.drop();

// Create the bucket.
const stream = bucket.openUploadStream('gridfstest');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function before() {

bucket = driver.bucket(driver.client.db(driver.DB_NAME));

await bucket.drop().catch(() => null);
await bucket.drop();
}

export async function beforeEach() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function before() {
await driver.resetTmpDir();

bucket = driver.bucket(driver.client.db(driver.DB_NAME));
await bucket.drop().catch(() => null);
await bucket.drop();

const gridfs_multi = path.resolve(PARALLEL_DIRECTORY, 'gridfs_multi');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function before() {

bucket = driver.bucket(driver.client.db(driver.DB_NAME));

await bucket.drop().catch(() => null);
await bucket.drop();
}

export async function beforeEach() {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/change-streams/change_stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ describe('Change Streams', function () {
async test() {
const collection = db.collection('resumeAfterTest2');

await collection.drop().catch(() => null);
await collection.drop();

let resumeToken;
const docs = [{ a: 0 }, { a: 1 }, { a: 2 }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ describe('Client Side Encryption Prose Corpus Test', function () {
await client.connect();
// 3. Using ``client``, drop the collection ``keyvault.datakeys``. Insert the documents `corpus/corpus-key-local.json <../corpus/corpus-key-local.json>`_ and `corpus/corpus-key-aws.json <../corpus/corpus-key-aws.json>`_.
const keyDb = client.db(keyVaultDbName);
await keyDb
.dropCollection(keyVaultCollName, { writeConcern: new WriteConcern('majority') })
.catch((e: Error) => {
if (!/ns/i.test(e.message)) {
throw e;
}
});
await keyDb.dropCollection(keyVaultCollName, { writeConcern: new WriteConcern('majority') });
const keyColl = keyDb.collection(keyVaultCollName);
await keyColl.insertMany(
[corpusKeyLocal, corpusKeyAws, corpusKeyAzure, corpusKeyGcp, corpusKeyKmip],
Expand All @@ -164,13 +158,7 @@ describe('Client Side Encryption Prose Corpus Test', function () {
beforeEach(async function () {
// 2. Using ``client``, drop and create the collection ``db.coll`` configured with the included JSON schema `corpus/corpus-schema.json <../corpus/corpus-schema.json>`_.
const dataDb = client.db(dataDbName);
await dataDb
.dropCollection(dataCollName, { writeConcern: new WriteConcern('majority') })
.catch((e: Error) => {
if (!/ns/i.test(e.message)) {
throw e;
}
});
await dataDb.dropCollection(dataCollName, { writeConcern: new WriteConcern('majority') });
await dataDb.createCollection(dataCollName, {
validator: { $jsonSchema: corpusSchema },
writeConcern: new WriteConcern('majority')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('14. Decryption Events', metadata, function () {
.collection('decryption_events')
.deleteMany({})
.catch(() => null);
await db.dropCollection('decryption_events').catch(() => null);
await db.dropCollection('decryption_events');
await db.createCollection('decryption_events');
// Create a ClientEncryption object named ``clientEncryption`` with these options:
// ClientEncryptionOpts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,12 @@ describe('Range Explicit Encryption', function () {

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

await utilClient
.db('db')
.dropCollection('explicit_encryption')
.catch(e => {
if (!/ns not found/.test(e.message)) {
throw e;
}
});
await utilClient.db('db').dropCollection('explicit_encryption');
await utilClient.db('db').createCollection('explicit_encryption', {
encryptedFields
});

await utilClient
.db('keyvault')
.dropCollection('datakeys')
.catch(e => {
if (!/ns not found/.test(e.message)) {
throw e;
}
});
await utilClient.db('keyvault').dropCollection('datakeys');

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2239,10 +2239,7 @@ describe('Client Side Encryption Prose Tests', metadata, function () {
await client.connect();

// Using client, drop the collection keyvault.datakeys.
await client
.db('keyvault')
.dropCollection('datakeys')
.catch(() => null);
await client.db('keyvault').dropCollection('datakeys');

await client
.db('keyvault')
Expand Down Expand Up @@ -2389,10 +2386,7 @@ describe('Client Side Encryption Prose Tests', metadata, function () {
client1.mongoLogger?.trace('client', 'dropping datakeys collection');

// Step 1. Drop the collection ``keyvault.datakeys``
await client1
.db('keyvault')
.dropCollection('datakeys')
.catch(() => null);
await client1.db('keyvault').dropCollection('datakeys');

client1.mongoLogger?.trace('client', 'dropped datakeys collection');

Expand Down
23 changes: 7 additions & 16 deletions test/integration/client-side-encryption/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ describe('Client Side Encryption Functional', function () {
const dataDb = client.db(dataDbName);
const keyVaultDb = client.db(keyVaultDbName);

await dataDb.dropCollection(dataCollName).catch(() => null);
await keyVaultDb.dropCollection(keyVaultCollName).catch(() => null);
await dataDb.dropCollection(dataCollName);
await keyVaultDb.dropCollection(keyVaultCollName);
await keyVaultDb.createCollection(keyVaultCollName);
const dataKey = await encryption.createDataKey('local');

Expand Down Expand Up @@ -314,8 +314,8 @@ describe('Client Side Encryption Functional', function () {
const dataDb = client.db(dataDbName);
const keyVaultDb = client.db(keyVaultDbName);

await dataDb.dropCollection(dataCollName).catch(() => null);
await keyVaultDb.dropCollection(keyVaultCollName).catch(() => null);
await dataDb.dropCollection(dataCollName);
await keyVaultDb.dropCollection(keyVaultCollName);
await keyVaultDb.createCollection(keyVaultCollName);
const dataKey = await encryption.createDataKey('local');

Expand Down Expand Up @@ -448,8 +448,7 @@ describe('Client Side Encryption Functional', function () {
const internalClient = this.configuration.newClient();
await internalClient
.db('keyvault')
.dropCollection('datakeys', { writeConcern: { w: 'majority' } })
.catch(() => null);
.dropCollection('datakeys', { writeConcern: { w: 'majority' } });
await internalClient.db('keyvault').createCollection('datakeys');
await internalClient.close();

Expand Down Expand Up @@ -1121,16 +1120,8 @@ describe('CSOT', function () {
configureFailPoint: 'failCommand',
mode: 'off'
} as FailCommandFailPoint);
await client
.db('db')
.collection('newnew')
.drop()
.catch(() => null);
await client
.db('keyvault')
.collection('datakeys')
.drop()
.catch(() => null);
await client.db('db').collection('newnew').drop();
await client.db('keyvault').collection('datakeys').drop();
await client.close();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';

import { type MongoClient, ObjectId, ReadPreference } from '../../mongodb';
import { filterForCommands, ignoreNsNotFound, setupDatabase } from '../shared';
import { filterForCommands, setupDatabase } from '../shared';

describe('Command Monitoring', function () {
let client: MongoClient;
Expand Down Expand Up @@ -164,7 +164,6 @@ describe('Command Monitoring', function () {
return db
.collection('apm_test_2')
.drop()
.catch(ignoreNsNotFound)
.then(() => {
// Insert test documents
return db
Expand Down Expand Up @@ -235,7 +234,6 @@ describe('Command Monitoring', function () {
return db
.collection('apm_test_2')
.drop()
.catch(ignoreNsNotFound)
.then(() => {
// Insert test documents
return db
Expand Down Expand Up @@ -328,7 +326,6 @@ describe('Command Monitoring', function () {
return db
.collection('apm_test_2')
.drop()
.catch(ignoreNsNotFound)
.then(() =>
db
.collection('apm_test_2')
Expand Down Expand Up @@ -531,12 +528,10 @@ describe('Command Monitoring', function () {
const desiredEvents = ['aggregate', 'getMore'];
client.on('commandStarted', filterForCommands(desiredEvents, started));
client.on('commandSucceeded', filterForCommands(desiredEvents, succeeded));

const db = client.db(this.configuration.db);
return db
.collection('apm_test_u_4')
.drop()
.catch(ignoreNsNotFound)
.then(() => db.collection('apm_test_u_4').insertMany(docs))
.then(r => {
expect(r).to.exist;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Connections Survive Primary Step Down - prose', function () {
// - Create a collection object from the ``MongoClient``, using ``step-down`` for the database and collection name.
collection = client.db('step-down').collection('step-down');
// - Drop the test collection, using ``writeConcern`` "majority".
await collection.drop({ writeConcern: { w: 'majority' } }).catch(() => null);
await collection.drop({ writeConcern: { w: 'majority' } });
// - Execute the "create" command to recreate the collection, using writeConcern: "majority".
collection = await client
.db('step-down')
Expand Down
18 changes: 7 additions & 11 deletions test/integration/crud/bulk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
MongoDriverError,
MongoInvalidArgumentError
} from '../../../src';
import { assert as test, ignoreNsNotFound } from '../shared';
import { assert as test } from '../shared';

const MAX_BSON_SIZE = 16777216;
const DB_NAME = 'bulk_operations_tests';
Expand Down Expand Up @@ -1432,9 +1432,7 @@ describe('Bulk', function () {
const documents = [{ s: largeString }, { s: largeString }];

const db = client.db();
await db.dropCollection('doesnt_matter').catch(() => {
// ignore
});
await db.dropCollection('doesnt_matter');
await db.createCollection('doesnt_matter');
const coll = db.collection('doesnt_matter');
await coll.insertMany(documents, { ordered: true });
Expand All @@ -1446,9 +1444,7 @@ describe('Bulk', function () {
const documents = [{ s: largeString }, { s: largeString }];

const db = client.db();
await db.dropCollection('doesnt_matter').catch(() => {
// ignore
});
await db.dropCollection('doesnt_matter');
await db.createCollection('doesnt_matter');
const coll = db.collection('doesnt_matter');
await coll.insertMany(documents, { ordered: false });
Expand All @@ -1474,7 +1470,7 @@ describe('Bulk', function () {

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

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

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

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