From 124d8904edfe11534297e9af23a056c3d2650cf8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 5 Mar 2021 16:42:03 +0100 Subject: [PATCH 1/2] fix: always autocomplete with read pref primaryPreferred MONGOSH-624 This is also what we do for `show dbs`/`show collections` in order to ensure that they work on secondary nodes. --- packages/cli-repl/test/e2e-direct.spec.ts | 27 +++++++++++++++++++ packages/shell-api/src/database.ts | 2 +- .../shell-api/src/shell-internal-state.ts | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/cli-repl/test/e2e-direct.spec.ts b/packages/cli-repl/test/e2e-direct.spec.ts index eb93620c9a..ca3dd4bab5 100644 --- a/packages/cli-repl/test/e2e-direct.spec.ts +++ b/packages/cli-repl/test/e2e-direct.spec.ts @@ -29,6 +29,8 @@ describe('e2e direct connection', () => { }); context('after rs.initiate()', () => { + let dbname: string; + before(async() => { const replSetConfig = { _id: replSetId, @@ -50,6 +52,15 @@ describe('e2e direct connection', () => { shell.assertContainsOutput(`me: '${await rs0.hostport()}'`); shell.assertContainsOutput(`setName: '${replSetId}'`); }); + dbname = `test-${Date.now()}-${(Math.random() * 100000) | 0}`; + await shell.executeLine(`use ${dbname}`); + await shell.executeLine('db.testcollection.insertOne({})'); + shell.writeInputLine('exit'); + }); + after(async() => { + const shell = TestShell.start({ args: [await rs0.connectionString()] }); + await shell.executeLine(`db.getSiblingDB("${dbname}").dropDatabase()`); + shell.writeInputLine('exit'); }); context('connecting to secondary members directly', () => { @@ -103,6 +114,14 @@ describe('e2e direct connection', () => { expect(await shell.executeLine('show collections')).to.include('system.version'); expect(await shell.executeLine('show dbs')).to.include('admin'); }); + it('autocompletes collection names', async() => { + const shell = TestShell.start({ args: [`${await rs1.connectionString()}/${dbname}`], forceTerminal: true }); + await shell.waitForPrompt(); + shell.writeInput('db.testc\u0009\u0009'); + await eventually(() => { + shell.assertContainsOutput('db.testcollection'); + }); + }); }); context('connecting to primary', () => { @@ -139,6 +158,14 @@ describe('e2e direct connection', () => { expect(await shell.executeLine('show collections')).to.include('system.version'); expect(await shell.executeLine('show dbs')).to.include('admin'); }); + it('autocompletes collection names', async() => { + const shell = TestShell.start({ args: [`${await rs1.connectionString()}/${dbname}`], forceTerminal: true }); + await shell.waitForPrompt(); + shell.writeInput('db.testc\u0009\u0009'); + await eventually(() => { + shell.assertContainsOutput('db.testcollection'); + }); + }); }); }); }); diff --git a/packages/shell-api/src/database.ts b/packages/shell-api/src/database.ts index 693e6a1c85..0d0c1fdde2 100644 --- a/packages/shell-api/src/database.ts +++ b/packages/shell-api/src/database.ts @@ -150,7 +150,7 @@ export default class Database extends ShellApiClass { async _getCollectionNamesForCompletion(): Promise { return await Promise.race([ (async() => { - return await this._getCollectionNames(); + return await this._getCollectionNames({ readPreference: 'primaryPreferred' }); })(), (async() => { // 200ms should be a good compromise between giving the server a chance diff --git a/packages/shell-api/src/shell-internal-state.ts b/packages/shell-api/src/shell-internal-state.ts index c7ef94a1bc..bc5984e94e 100644 --- a/packages/shell-api/src/shell-internal-state.ts +++ b/packages/shell-api/src/shell-internal-state.ts @@ -133,7 +133,7 @@ export default class ShellInternalState { this.context.sh = new Shard(this.currentDb); this.fetchConnectionInfo().catch(err => this.messageBus.emit('mongosh:error', err)); // Pre-fetch for autocompletion. - this.currentDb._getCollectionNames().catch(err => this.messageBus.emit('mongosh:error', err)); + this.currentDb._getCollectionNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err)); return newDb; } From 9d4ed77afd9bf664a89cbb0353947e8ce4be781f Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 5 Mar 2021 18:38:22 +0100 Subject: [PATCH 2/2] fixup! fix: always autocomplete with read pref primaryPreferred MONGOSH-624 --- packages/shell-api/src/shell-internal-state.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/shell-api/src/shell-internal-state.spec.ts b/packages/shell-api/src/shell-internal-state.spec.ts index 8868ebe261..d029586884 100644 --- a/packages/shell-api/src/shell-internal-state.spec.ts +++ b/packages/shell-api/src/shell-internal-state.spec.ts @@ -51,7 +51,10 @@ describe('ShellInternalState', () => { serviceProvider.listCollections .resolves([ { name: 'coll1' }, { name: 'coll2' } ]); expect(run('db = db.getSiblingDB("moo"); db.getName()')).to.equal('moo'); - expect(serviceProvider.listCollections.calledWith('moo', {}, { nameOnly: true })).to.equal(true); + expect(serviceProvider.listCollections.calledWith('moo', {}, { + readPreference: 'primaryPreferred', + nameOnly: true + })).to.equal(true); }); });