Skip to content

Commit c8896a5

Browse files
committed
feat(shell-api): reset current cursor when db changes or auths MONGOSH-13
Reset the current cursor on: - Assignment to `db` - `db.auth()` - `db.logout()`
1 parent 3f74929 commit c8896a5

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ describe('Auth e2e', function() {
717717
afterEach(async() => {
718718
db.command({ dropAllUsersFromDatabase: 1 });
719719
});
720-
describe('auth', async() => {
720+
describe('auth', () => {
721721
it('logs in with simple user/pwd', async() => {
722722
shell.writeInputLine(`use ${dbName}`);
723723
shell.writeInputLine(
@@ -796,7 +796,7 @@ describe('Auth e2e', function() {
796796
});
797797
});
798798
});
799-
describe('logout', async() => {
799+
describe('logout', () => {
800800
it('logs out after authenticating', async() => {
801801
shell.writeInputLine(`use ${dbName}`);
802802
shell.writeInputLine(
@@ -826,6 +826,35 @@ describe('Auth e2e', function() {
826826
shell.assertNoErrors();
827827
});
828828
});
829+
describe('resetting current cursor', () => {
830+
beforeEach(async() => {
831+
await db.collection('test').insertMany(
832+
[...Array(200).keys()].map(i => ({ i }))
833+
);
834+
});
835+
it('is reset after auth, db reassign and logout', async() => {
836+
await shell.executeLine(`use ${dbName}`);
837+
expect(await shell.executeLine('db.test.find()')).to.include('i: 10');
838+
expect(await shell.executeLine('it')).to.include('i: 30');
839+
840+
expect(await shell.executeLine('db.auth("anna", "pwd")')).to.include('ok: 1');
841+
expect(await shell.executeLine('it')).to.include('no cursor');
842+
expect(await shell.executeLine('db.test.find()')).to.include('i: 10');
843+
expect(await shell.executeLine('it')).to.include('i: 30');
844+
845+
expect(await shell.executeLine(`db = db.getSiblingDB("${dbName}")`)).to.include(`${dbName}\n`);
846+
expect(await shell.executeLine('it')).to.include('no cursor');
847+
expect(await shell.executeLine('db.test.find()')).to.include('i: 10');
848+
expect(await shell.executeLine('it')).to.include('i: 30');
849+
850+
expect(await shell.executeLine('db.logout()')).to.include('ok: 1');
851+
expect(await shell.executeLine('it')).to.include('no cursor');
852+
expect(await shell.executeLine('db.test.find()')).to.include('i: 10');
853+
expect(await shell.executeLine('it')).to.include('i: 30');
854+
855+
shell.assertNoErrors();
856+
});
857+
});
829858
});
830859
});
831860
describe('with options in URI on on the command line', () => {

packages/shell-api/src/database.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ export default class Database extends ShellApiWithMongoClass {
452452
@apiVersions([])
453453
async logout(): Promise<Document> {
454454
this._emitDatabaseApiCall('logout', {});
455+
this._mongo._internalState.currentCursor = null;
455456
return await this._runCommand({ logout: 1 });
456457
}
457458

@@ -516,6 +517,7 @@ export default class Database extends ShellApiWithMongoClass {
516517
);
517518
}
518519
authDoc.authDb = this._name;
520+
this._mongo._internalState.currentCursor = null;
519521
return await this._mongo._serviceProvider.authenticate(authDoc);
520522
}
521523

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export default class ShellInternalState {
174174
// Pre-fetch for autocompletion.
175175
this.currentDb._getCollectionNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err));
176176
this.currentDb._mongo._getDatabaseNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err));
177+
this.currentCursor = null;
177178
return newDb;
178179
}
179180

0 commit comments

Comments
 (0)