Skip to content

Commit e62e5c9

Browse files
committed
fix(db): ensure dropDatabase always uses primary read preference
NODE-1378
1 parent 4f6109a commit e62e5c9

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

lib/db.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -905,11 +905,10 @@ Db.prototype.dropDatabase = function(options, callback) {
905905
decorateWithWriteConcern(cmd, this, options);
906906

907907
// Ensure primary only
908-
const finalOptions = Object.assign(
909-
{},
910-
{ readPreference: ReadPreference.PRIMARY },
911-
this.s.options
912-
);
908+
const finalOptions = Object.assign({}, this.s.options, {
909+
readPreference: ReadPreference.PRIMARY
910+
});
911+
913912
if (options.session) {
914913
finalOptions.session = options.session;
915914
}

test/unit/db_tests.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const EventEmitter = require('events');
4+
const expect = require('chai').expect;
5+
const sinon = require('sinon');
6+
7+
class MockTopology extends EventEmitter {
8+
constructor() {
9+
super();
10+
}
11+
12+
capabilities() {
13+
return {};
14+
}
15+
}
16+
17+
const test = {};
18+
describe('Database', function() {
19+
before(() => {
20+
// NOTE: These modules are being used prior to test run. In order to monkey-patch them
21+
// we must remove their cached versions.
22+
const resolvedUtils = require.resolve('../../lib/utils');
23+
const resolvedDb = require.resolve('../../lib/db');
24+
delete require.cache[resolvedUtils];
25+
delete require.cache[resolvedDb];
26+
test.utils = require('../../lib/utils');
27+
28+
// create a sandbox for stub cleanup
29+
test.sandbox = sinon.sandbox.create();
30+
});
31+
32+
afterEach(() => test.sandbox.restore());
33+
34+
it('should ignore a readPreference for dropDatabase', {
35+
metadata: { requires: { topology: 'single' } },
36+
test: function() {
37+
sinon.stub(test.utils, 'executeOperation').callsFake((topology, operation, args) => {
38+
const options = args[args.length - 2];
39+
expect(options.readPreference).to.equal('primary');
40+
});
41+
42+
const Db = require('../../lib/db');
43+
const db = new Db('fakeDb', new MockTopology(), { readPreference: 'nearest' });
44+
db.dropDatabase();
45+
}
46+
});
47+
});

0 commit comments

Comments
 (0)