Skip to content

Commit

Permalink
feat: add sh.moveRange helper MONGOSH-1926 (#2397)
Browse files Browse the repository at this point in the history
* feat: add sh.moveRange helper

* Use a more realistic error
  • Loading branch information
nirinchev authored Mar 6, 2025
1 parent d5d554e commit 3233cbd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,12 @@ const translations: Catalog = {
'Moves the chunk that contains the document specified by the query to the destination shard. Uses the moveChunk command',
example: 'sh.moveChunk(ns, query, destination)',
},
moveRange: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.moveRange',
description:
'Moves a range of documents specified by the min and max keys to the destination shard. Uses the moveRange command',
example: 'sh.moveRange(ns, toShard, min?, max?)',
},
balancerCollectionStatus: {
link: 'https://docs.mongodb.com/manual/reference/method/sh.balancerCollectionStatus',
description:
Expand Down
50 changes: 50 additions & 0 deletions packages/shell-api/src/shard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { dummyOptions } from './helpers.spec';

describe('Shard', function () {
skipIfApiStrict();

describe('help', function () {
const apiClass: any = new Shard({} as any);
it('calls help function', async function () {
Expand All @@ -50,6 +51,7 @@ describe('Shard', function () {
);
});
});

describe('signatures', function () {
it('type', function () {
expect(signatures.Shard.type).to.equal('Shard');
Expand All @@ -70,6 +72,7 @@ describe('Shard', function () {
});
});
});

describe('Metadata', function () {
describe('toShellResult', function () {
const mongo = { _uri: 'test_uri' } as Mongo;
Expand All @@ -85,6 +88,7 @@ describe('Shard', function () {
});
});
});

describe('unit', function () {
let mongo: Mongo;
let serviceProvider: StubbedInstance<ServiceProvider>;
Expand Down Expand Up @@ -2058,6 +2062,52 @@ describe('Shard', function () {
expect(caughtError).to.equal(expectedError);
});
});

describe('moveRange', function () {
it('calls serviceProvider.runCommandWithCheck with arg', async function () {
serviceProvider.runCommandWithCheck.resolves({
ok: 1,
msg: 'isdbgrid',
});
await shard.moveRange('ns', 'destination', { key: 0 }, { key: 10 });

expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
ADMIN_DB,
{
moveRange: 'ns',
min: { key: 0 },
max: { key: 10 },
toShard: 'destination',
}
);
});

it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
const expectedResult = {
ok: 1,
operationTime: { $timestamp: { t: 1741189797, i: 1 } },
};
serviceProvider.runCommandWithCheck.resolves(expectedResult);
const result = await shard.moveRange(
'ns',
'destination',
{ key: 0 },
{ key: 10 }
);
expect(result).to.deep.equal(expectedResult);
});

it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
const expectedError = new Error(
"Missing required parameter 'min' or 'max'"
);
serviceProvider.runCommandWithCheck.rejects(expectedError);
const caughtError = await shard
.moveRange('ns', 'destination')
.catch((e) => e);
expect(caughtError).to.equal(expectedError);
});
});
});

describe('integration', function () {
Expand Down
27 changes: 26 additions & 1 deletion packages/shell-api/src/shard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export default class Shard extends ShellApiWithMongoClass {
async moveChunk(
ns: string,
query: Document,
destination: string | undefined
destination: string
): Promise<Document> {
assertArgsDefinedType(
[ns, query, destination],
Expand All @@ -481,6 +481,31 @@ export default class Shard extends ShellApiWithMongoClass {
});
}

@returnsPromise
@serverVersions(['6.0.0', ServerVersions.latest])
async moveRange(
ns: string,
toShard: string,
min?: Document,
max?: Document
): Promise<Document> {
assertArgsDefinedType(
[ns, toShard, min, max],
['string', 'string', ['object', undefined], ['object', undefined]],
'Shard.moveRange'
);

this._emitShardApiCall('moveRange', { ns, toShard, min, max });
await getConfigDB(this._database); // will error if not connected to mongos

return this._database._runAdminCommand({
moveRange: ns,
toShard,
min,
max,
});
}

@returnsPromise
@apiVersions([])
@serverVersions(['4.4.0', ServerVersions.latest])
Expand Down

0 comments on commit 3233cbd

Please sign in to comment.