Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 19 additions & 6 deletions src/operations/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,34 @@ export async function dropCollections(
try {
await executeOperation(db.client, dropOp, timeoutContext);
} catch (err) {
// Note in FLE the error code is an Int32, where we expect a JS number, so we test
// the message as well here and below.
if (
!(err instanceof MongoServerError) ||
err.code !== MONGODB_ERROR_CODES.NamespaceNotFound
(err.code !== MONGODB_ERROR_CODES.NamespaceNotFound &&
!/ns not found|ns does not exist/.test(err.message))
) {
throw err;
}
}
}
}

return await executeOperation(
db.client,
new DropCollectionOperation(db, name, options),
timeoutContext
);
try {
return await executeOperation(
db.client,
new DropCollectionOperation(db, name, options),
timeoutContext
);
} catch (err) {
if (
!(err instanceof MongoServerError) ||
(err.code !== MONGODB_ERROR_CODES.NamespaceNotFound && !/ns not found/.test(err.message))
) {
throw err;
}
return false;
}
}

/** @public */
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
@@ -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
10 changes: 5 additions & 5 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 @@ -1474,7 +1474,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 +1490,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 +1513,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 +1528,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
12 changes: 6 additions & 6 deletions test/integration/crud/document_validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';

import { MongoBulkWriteError, type MongoClient, MongoServerError } from '../../../src';
import { ignoreNsNotFound, setupDatabase } from '../shared';
import { setupDatabase } from '../shared';

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

// Drop the collection
await col.drop().catch(ignoreNsNotFound);
await col.drop();
// Create a collection with a validator
await db.createCollection('createValidationCollection', {
validator: { a: { $exists: true } }
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Document Validation', function () {
const col = db.collection('createValidationCollection');

// Drop the collection
await col.drop().catch(ignoreNsNotFound);
await col.drop();
// Create a collection with a validator
await db.createCollection('createValidationCollection', {
validator: { a: { $exists: true } }
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('Document Validation', function () {
const col = db.collection('createValidationCollection');

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

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

// Drop the collection
await col.drop().catch(ignoreNsNotFound);
await col.drop();
// Create a collection with a validator
await db.createCollection('createValidationCollectionOut', {
validator: { a: { $exists: true } }
Expand Down
4 changes: 2 additions & 2 deletions test/integration/crud/insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
Timestamp
} from '../../../src';
import { noop } from '../../../src/utils';
import { assert as test, ignoreNsNotFound, setupDatabase } from '../shared';
import { assert as test, setupDatabase } from '../shared';

describe('crud - insert', function () {
let client: MongoClient;
Expand Down Expand Up @@ -1590,7 +1590,7 @@ describe('crud - insert', function () {
});

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

const collection = client
.db()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';

import { type MongoClient, ObjectId } from '../../../../src';
import { assert as test, ignoreNsNotFound, setupDatabase } from '../../shared';
import { assert as test, setupDatabase } from '../../shared';

describe('Ignore Undefined', function () {
before(function () {
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('Ignore Undefined', function () {
describe('ignoreUndefined A server', function () {
it('should correctly execute insert culling undefined', async function () {
const coll = client.db().collection('insert1');
await coll.drop().catch(ignoreNsNotFound);
await coll.drop().catch();
const objectId = new ObjectId();
const res = await coll.insertOne(
{ _id: objectId, a: 1, b: undefined },
Expand All @@ -197,7 +197,7 @@ describe('Ignore Undefined', function () {

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

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

it('should correctly execute remove not culling undefined', async function () {
const coll = client.db().collection('remove1');
await coll.drop().catch(ignoreNsNotFound);
await coll.drop();
const objectId = new ObjectId();
const res = await coll.insertMany([
{ id: objectId, a: 1, b: undefined },
Expand Down
7 changes: 1 addition & 6 deletions test/integration/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function delay(timeout) {
}

function dropCollection(dbObj, collectionName, options = {}) {
return dbObj.dropCollection(collectionName, options).catch(ignoreNsNotFound);
return dbObj.dropCollection(collectionName, options);
}

/**
Expand Down Expand Up @@ -86,10 +86,6 @@ function filterOutCommands(commands, bag) {
};
}

function ignoreNsNotFound(err) {
if (!err.message.match(/ns not found/)) throw err;
}

async function setupDatabase(configuration, dbsToClean) {
dbsToClean = Array.isArray(dbsToClean) ? dbsToClean : [];
const configDbName = configuration.db;
Expand Down Expand Up @@ -204,7 +200,6 @@ module.exports = {
dropCollection,
filterForCommands,
filterOutCommands,
ignoreNsNotFound,
setupDatabase,
withCursor,
APMEventCollector
Expand Down
30 changes: 8 additions & 22 deletions test/tools/spec-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,26 +283,16 @@ function prepareDatabaseForSuite(suite, context) {
}
return coll.drop(options);
})
.catch(err => {
if (!err.message.match(/ns not found/)) throw err;
})
.then(() => {
if (suite.key_vault_data) {
const dataKeysCollection = context.sharedClient.db('keyvault').collection('datakeys');
return dataKeysCollection
.drop({ writeConcern: { w: 'majority' } })
.catch(err => {
if (!err.message.match(/ns not found/)) {
throw err;
}
})
.then(() => {
if (suite.key_vault_data.length) {
return dataKeysCollection.insertMany(suite.key_vault_data, {
writeConcern: { w: 'majority' }
});
}
});
return dataKeysCollection.drop({ writeConcern: { w: 'majority' } }).then(() => {
if (suite.key_vault_data.length) {
return dataKeysCollection.insertMany(suite.key_vault_data, {
writeConcern: { w: 'majority' }
});
}
});
}
})
.then(() => {
Expand Down Expand Up @@ -698,11 +688,7 @@ const kOperations = new Map([
const collectionName = operation.arguments.collection;
const encryptedFields = operation.arguments.encryptedFields;
const session = maybeSession(operation, context);
return db.dropCollection(collectionName, { session, encryptedFields }).catch(err => {
if (!err.message.match(/ns not found/)) {
throw err;
}
});
return db.dropCollection(collectionName, { session, encryptedFields });
}
],
[
Expand Down
10 changes: 1 addition & 9 deletions test/tools/unified-spec-runner/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,7 @@ operations.set('dropCollection', async ({ entities, operation }) => {
const db = entities.getEntity('db', operation.object);
const { collection, ...opts } = operation.arguments!;

// TODO(NODE-4243): dropCollection should suppress namespace not found errors
try {
await db.dropCollection(collection, opts);
} catch (err) {
if (!/ns not found/.test(err.message)) {
throw err;
}
return false;
}
return await db.dropCollection(collection, opts);
});

operations.set('drop', async ({ entities, operation }) => {
Expand Down