Skip to content

Commit 88bf9ca

Browse files
authored
fix: always autocomplete with read pref primaryPreferred MONGOSH-624 (#708)
This is also what we do for `show dbs`/`show collections` in order to ensure that they work on secondary nodes.
1 parent 3deb4be commit 88bf9ca

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ describe('e2e direct connection', () => {
2929
});
3030

3131
context('after rs.initiate()', () => {
32+
let dbname: string;
33+
3234
before(async() => {
3335
const replSetConfig = {
3436
_id: replSetId,
@@ -50,6 +52,15 @@ describe('e2e direct connection', () => {
5052
shell.assertContainsOutput(`me: '${await rs0.hostport()}'`);
5153
shell.assertContainsOutput(`setName: '${replSetId}'`);
5254
});
55+
dbname = `test-${Date.now()}-${(Math.random() * 100000) | 0}`;
56+
await shell.executeLine(`use ${dbname}`);
57+
await shell.executeLine('db.testcollection.insertOne({})');
58+
shell.writeInputLine('exit');
59+
});
60+
after(async() => {
61+
const shell = TestShell.start({ args: [await rs0.connectionString()] });
62+
await shell.executeLine(`db.getSiblingDB("${dbname}").dropDatabase()`);
63+
shell.writeInputLine('exit');
5364
});
5465

5566
context('connecting to secondary members directly', () => {
@@ -103,6 +114,14 @@ describe('e2e direct connection', () => {
103114
expect(await shell.executeLine('show collections')).to.include('system.version');
104115
expect(await shell.executeLine('show dbs')).to.include('admin');
105116
});
117+
it('autocompletes collection names', async() => {
118+
const shell = TestShell.start({ args: [`${await rs1.connectionString()}/${dbname}`], forceTerminal: true });
119+
await shell.waitForPrompt();
120+
shell.writeInput('db.testc\u0009\u0009');
121+
await eventually(() => {
122+
shell.assertContainsOutput('db.testcollection');
123+
});
124+
});
106125
});
107126

108127
context('connecting to primary', () => {
@@ -139,6 +158,14 @@ describe('e2e direct connection', () => {
139158
expect(await shell.executeLine('show collections')).to.include('system.version');
140159
expect(await shell.executeLine('show dbs')).to.include('admin');
141160
});
161+
it('autocompletes collection names', async() => {
162+
const shell = TestShell.start({ args: [`${await rs1.connectionString()}/${dbname}`], forceTerminal: true });
163+
await shell.waitForPrompt();
164+
shell.writeInput('db.testc\u0009\u0009');
165+
await eventually(() => {
166+
shell.assertContainsOutput('db.testcollection');
167+
});
168+
});
142169
});
143170
});
144171
});

packages/shell-api/src/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export default class Database extends ShellApiClass {
150150
async _getCollectionNamesForCompletion(): Promise<string[]> {
151151
return await Promise.race([
152152
(async() => {
153-
return await this._getCollectionNames();
153+
return await this._getCollectionNames({ readPreference: 'primaryPreferred' });
154154
})(),
155155
(async() => {
156156
// 200ms should be a good compromise between giving the server a chance

packages/shell-api/src/shell-internal-state.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ describe('ShellInternalState', () => {
5151
serviceProvider.listCollections
5252
.resolves([ { name: 'coll1' }, { name: 'coll2' } ]);
5353
expect(run('db = db.getSiblingDB("moo"); db.getName()')).to.equal('moo');
54-
expect(serviceProvider.listCollections.calledWith('moo', {}, { nameOnly: true })).to.equal(true);
54+
expect(serviceProvider.listCollections.calledWith('moo', {}, {
55+
readPreference: 'primaryPreferred',
56+
nameOnly: true
57+
})).to.equal(true);
5558
});
5659
});
5760

packages/shell-api/src/shell-internal-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default class ShellInternalState {
133133
this.context.sh = new Shard(this.currentDb);
134134
this.fetchConnectionInfo().catch(err => this.messageBus.emit('mongosh:error', err));
135135
// Pre-fetch for autocompletion.
136-
this.currentDb._getCollectionNames().catch(err => this.messageBus.emit('mongosh:error', err));
136+
this.currentDb._getCollectionNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err));
137137
return newDb;
138138
}
139139

0 commit comments

Comments
 (0)