Skip to content

Commit edc589a

Browse files
authored
fix(shell-api): apply requested read pref to db listings MONGOSH-1057 (#1156)
1 parent 7fdbd38 commit edc589a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/cli-repl/test/e2e-direct.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,39 @@ describe('e2e direct connection', () => {
121121
shell.assertContainsOutput("name: 'system.version'");
122122
});
123123

124+
it('fails to list databases without explicit readPreference', async() => {
125+
const shell = TestShell.start({ args: [`${await rs1.connectionString()}`] });
126+
await shell.waitForPrompt();
127+
await shell.executeLine('use admin');
128+
await shell.executeLine('db.getMongo().getDBs()');
129+
shell.assertContainsError('MongoServerError: not primary');
130+
});
131+
132+
it('lists databases when readPreference is in the connection string', async() => {
133+
const shell = TestShell.start({ args: [`${await rs1.connectionString()}?readPreference=secondaryPreferred`] });
134+
await shell.waitForPrompt();
135+
await shell.executeLine('use admin');
136+
await shell.executeLine('db.getMongo().getDBs()');
137+
shell.assertContainsOutput("name: 'admin'");
138+
});
139+
140+
it('lists databases when readPreference is set via Mongo', async() => {
141+
const shell = TestShell.start({ args: [`${await rs1.connectionString()}`] });
142+
await shell.waitForPrompt();
143+
await shell.executeLine('use admin');
144+
await shell.executeLine('db.getMongo().setReadPref("secondaryPreferred")');
145+
await shell.executeLine('db.getMongo().getDBs()');
146+
shell.assertContainsOutput("name: 'admin'");
147+
});
148+
124149
it('lists collections and dbs using show by default', async() => {
125150
const shell = TestShell.start({ args: [`${await rs1.connectionString()}`] });
126151
await shell.waitForPrompt();
127152
await shell.executeLine('use admin');
128153
expect(await shell.executeLine('show collections')).to.include('system.version');
129154
expect(await shell.executeLine('show dbs')).to.include('admin');
130155
});
156+
131157
it('autocompletes collection names', async function() {
132158
if (process.arch === 's390x') {
133159
return this.skip(); // https://jira.mongodb.org/browse/MONGOSH-746

packages/shell-api/src/mongo.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,10 @@ export default class Mongo extends ShellApiClass {
242242
}
243243

244244
async _listDatabases(opts: ListDatabasesOptions = {}): Promise<{ databases: {name: string, sizeOnDisk: number, empty: boolean}[] }> {
245-
const result = await this._serviceProvider.listDatabases('admin', { ...opts });
245+
const result = await this._serviceProvider.listDatabases('admin', {
246+
...this._getExplicitlyRequestedReadPref(),
247+
...opts
248+
});
246249
if (!('databases' in result)) {
247250
const err = new MongoshRuntimeError('Got invalid result from "listDatabases"', CommonErrors.CommandFailed);
248251
this._instanceState.messageBus.emit('mongosh:error', err, 'shell-api');

0 commit comments

Comments
 (0)