Skip to content

Commit 3233cbd

Browse files
authored
feat: add sh.moveRange helper MONGOSH-1926 (#2397)
* feat: add sh.moveRange helper * Use a more realistic error
1 parent d5d554e commit 3233cbd

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

packages/i18n/src/locales/en_US.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,12 @@ const translations: Catalog = {
20282028
'Moves the chunk that contains the document specified by the query to the destination shard. Uses the moveChunk command',
20292029
example: 'sh.moveChunk(ns, query, destination)',
20302030
},
2031+
moveRange: {
2032+
link: 'https://docs.mongodb.com/manual/reference/method/sh.moveRange',
2033+
description:
2034+
'Moves a range of documents specified by the min and max keys to the destination shard. Uses the moveRange command',
2035+
example: 'sh.moveRange(ns, toShard, min?, max?)',
2036+
},
20312037
balancerCollectionStatus: {
20322038
link: 'https://docs.mongodb.com/manual/reference/method/sh.balancerCollectionStatus',
20332039
description:

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { dummyOptions } from './helpers.spec';
3535

3636
describe('Shard', function () {
3737
skipIfApiStrict();
38+
3839
describe('help', function () {
3940
const apiClass: any = new Shard({} as any);
4041
it('calls help function', async function () {
@@ -50,6 +51,7 @@ describe('Shard', function () {
5051
);
5152
});
5253
});
54+
5355
describe('signatures', function () {
5456
it('type', function () {
5557
expect(signatures.Shard.type).to.equal('Shard');
@@ -70,6 +72,7 @@ describe('Shard', function () {
7072
});
7173
});
7274
});
75+
7376
describe('Metadata', function () {
7477
describe('toShellResult', function () {
7578
const mongo = { _uri: 'test_uri' } as Mongo;
@@ -85,6 +88,7 @@ describe('Shard', function () {
8588
});
8689
});
8790
});
91+
8892
describe('unit', function () {
8993
let mongo: Mongo;
9094
let serviceProvider: StubbedInstance<ServiceProvider>;
@@ -2058,6 +2062,52 @@ describe('Shard', function () {
20582062
expect(caughtError).to.equal(expectedError);
20592063
});
20602064
});
2065+
2066+
describe('moveRange', function () {
2067+
it('calls serviceProvider.runCommandWithCheck with arg', async function () {
2068+
serviceProvider.runCommandWithCheck.resolves({
2069+
ok: 1,
2070+
msg: 'isdbgrid',
2071+
});
2072+
await shard.moveRange('ns', 'destination', { key: 0 }, { key: 10 });
2073+
2074+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
2075+
ADMIN_DB,
2076+
{
2077+
moveRange: 'ns',
2078+
min: { key: 0 },
2079+
max: { key: 10 },
2080+
toShard: 'destination',
2081+
}
2082+
);
2083+
});
2084+
2085+
it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
2086+
const expectedResult = {
2087+
ok: 1,
2088+
operationTime: { $timestamp: { t: 1741189797, i: 1 } },
2089+
};
2090+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
2091+
const result = await shard.moveRange(
2092+
'ns',
2093+
'destination',
2094+
{ key: 0 },
2095+
{ key: 10 }
2096+
);
2097+
expect(result).to.deep.equal(expectedResult);
2098+
});
2099+
2100+
it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
2101+
const expectedError = new Error(
2102+
"Missing required parameter 'min' or 'max'"
2103+
);
2104+
serviceProvider.runCommandWithCheck.rejects(expectedError);
2105+
const caughtError = await shard
2106+
.moveRange('ns', 'destination')
2107+
.catch((e) => e);
2108+
expect(caughtError).to.equal(expectedError);
2109+
});
2110+
});
20612111
});
20622112

20632113
describe('integration', function () {

packages/shell-api/src/shard.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ export default class Shard extends ShellApiWithMongoClass {
466466
async moveChunk(
467467
ns: string,
468468
query: Document,
469-
destination: string | undefined
469+
destination: string
470470
): Promise<Document> {
471471
assertArgsDefinedType(
472472
[ns, query, destination],
@@ -481,6 +481,31 @@ export default class Shard extends ShellApiWithMongoClass {
481481
});
482482
}
483483

484+
@returnsPromise
485+
@serverVersions(['6.0.0', ServerVersions.latest])
486+
async moveRange(
487+
ns: string,
488+
toShard: string,
489+
min?: Document,
490+
max?: Document
491+
): Promise<Document> {
492+
assertArgsDefinedType(
493+
[ns, toShard, min, max],
494+
['string', 'string', ['object', undefined], ['object', undefined]],
495+
'Shard.moveRange'
496+
);
497+
498+
this._emitShardApiCall('moveRange', { ns, toShard, min, max });
499+
await getConfigDB(this._database); // will error if not connected to mongos
500+
501+
return this._database._runAdminCommand({
502+
moveRange: ns,
503+
toShard,
504+
min,
505+
max,
506+
});
507+
}
508+
484509
@returnsPromise
485510
@apiVersions([])
486511
@serverVersions(['4.4.0', ServerVersions.latest])

0 commit comments

Comments
 (0)