Skip to content

Commit 3e2fbc1

Browse files
authored
feat(shell-api): make db.hello() fall back to legacy variant MONGOSH-558 (#827)
If `db.hello()` is not available, fall back to the legacy variant, and always make `isWritablePrimary` available in the return value for both variants.
1 parent 1bd47fe commit 3e2fbc1

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

packages/shell-api/src/database.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,22 +812,33 @@ export default class Database extends ShellApiClass {
812812
@returnsPromise
813813
async isMaster(): Promise<Document> {
814814
this._emitDatabaseApiCall('isMaster', {});
815-
return await this._runCommand(
815+
const result = await this._runCommand(
816816
{
817817
isMaster: 1,
818818
}
819819
);
820+
result.isWritablePrimary = result.ismaster;
821+
return result;
820822
}
821823

822824
@returnsPromise
823825
@serverVersions(['5.0.0', ServerVersions.latest])
824826
async hello(): Promise<Document> {
825827
this._emitDatabaseApiCall('hello', {});
826-
return await this._runCommand(
827-
{
828-
hello: 1,
828+
try {
829+
return await this._runCommand(
830+
{
831+
hello: 1,
832+
}
833+
);
834+
} catch (err) {
835+
if (err.codeName === 'CommandNotFound') {
836+
const result = await this.isMaster();
837+
delete result.ismaster;
838+
return result;
829839
}
830-
);
840+
throw err;
841+
}
831842
}
832843

833844
@returnsPromise

packages/shell-api/src/integration.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,15 +1927,18 @@ describe('Shell API (integration)', function() {
19271927
describe('database commands', () => {
19281928
it('db.isMaster() works', async() => {
19291929
expect((await database.isMaster()).ismaster).to.equal(true);
1930+
expect((await database.isMaster()).isWritablePrimary).to.equal(true);
1931+
});
1932+
1933+
it('db.hello() works', async() => {
1934+
const result = await database.hello();
1935+
expect(result.ismaster).to.equal(undefined);
1936+
expect(result.isWritablePrimary).to.equal(true);
19301937
});
19311938

19321939
context('with 5.0+ server', () => {
19331940
skipIfServerVersion(testServer, '<= 4.4');
19341941

1935-
it('db.hello() works', async() => {
1936-
expect((await database.hello()).isWritablePrimary).to.equal(true);
1937-
});
1938-
19391942
it('db.rotateCertificates() works', async() => {
19401943
expect((await database.rotateCertificates()).ok).to.equal(1);
19411944
expect((await database.rotateCertificates('message')).ok).to.equal(1);

0 commit comments

Comments
 (0)