Skip to content

Commit 5516373

Browse files
authored
feat(shell-api): add shard moveCollection and unshardCollection helpers COMPASS-7500 (#2092)
* add helpers * cr fix
1 parent 3252c96 commit 5516373

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

packages/i18n/src/locales/en_US.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,30 @@ const translations: Catalog = {
20452045
'Returns a cursor with information about metadata inconsistencies',
20462046
example: 'sh.checkMetadataConsistency(<options>)',
20472047
},
2048+
moveCollection: {
2049+
link: 'https://docs.mongodb.com/manual/reference/method/sh.moveCollection',
2050+
description:
2051+
'Moves a single unsharded collection to a different shard.',
2052+
example: 'sh.moveCollection(ns, toShard)',
2053+
},
2054+
abortMoveCollection: {
2055+
link: 'https://docs.mongodb.com/manual/reference/method/sh.abortMoveCollection',
2056+
description:
2057+
'Abort the current moveCollection operation on a given collection',
2058+
example: 'sh.abortMoveCollection(ns)',
2059+
},
2060+
unshardCollection: {
2061+
link: 'https://docs.mongodb.com/manual/reference/method/sh.unshardCollection',
2062+
description:
2063+
'Unshard the given collection and move all data to the given shard.',
2064+
example: 'sh.unshardCollection(ns, toShard)',
2065+
},
2066+
abortUnshardCollection: {
2067+
link: 'https://docs.mongodb.com/manual/reference/method/sh.abortUnshardCollection',
2068+
description:
2069+
'Abort the current unshardCollection operation on a given collection',
2070+
example: 'sh.abortUnshardCollection(ns)',
2071+
},
20482072
},
20492073
},
20502074
},

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,120 @@ describe('Shard', function () {
17901790
);
17911791
});
17921792
});
1793+
1794+
describe('moveCollection', function () {
1795+
it('calls serviceProvider.runCommandWithCheck', async function () {
1796+
await shard.moveCollection('db.coll', 'shard1');
1797+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
1798+
ADMIN_DB,
1799+
{
1800+
moveCollection: 'db.coll',
1801+
toShard: 'shard1',
1802+
}
1803+
);
1804+
});
1805+
1806+
it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
1807+
const expectedResult = { ok: 1 };
1808+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
1809+
const result = await shard.moveCollection('db.coll', 'shard1');
1810+
expect(result).to.deep.equal(expectedResult);
1811+
});
1812+
1813+
it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
1814+
const expectedError = new Error();
1815+
serviceProvider.runCommandWithCheck.rejects(expectedError);
1816+
const caughtError = await shard
1817+
.moveCollection('db.coll', 'shard1')
1818+
.catch((e) => e);
1819+
expect(caughtError).to.equal(expectedError);
1820+
});
1821+
});
1822+
1823+
describe('abortMoveCollection', function () {
1824+
it('calls serviceProvider.runCommandWithCheck', async function () {
1825+
await shard.abortMoveCollection('db.coll');
1826+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
1827+
ADMIN_DB,
1828+
{
1829+
abortMoveCollection: 'db.coll',
1830+
}
1831+
);
1832+
});
1833+
1834+
it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
1835+
const expectedResult = { ok: 1 };
1836+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
1837+
const result = await shard.abortMoveCollection('db.coll');
1838+
expect(result).to.deep.equal(expectedResult);
1839+
});
1840+
1841+
it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
1842+
const expectedError = new Error();
1843+
serviceProvider.runCommandWithCheck.rejects(expectedError);
1844+
const caughtError = await shard
1845+
.abortMoveCollection('db.coll')
1846+
.catch((e) => e);
1847+
expect(caughtError).to.equal(expectedError);
1848+
});
1849+
});
1850+
1851+
describe('unshardCollection', function () {
1852+
it('calls serviceProvider.runCommandWithCheck', async function () {
1853+
await shard.unshardCollection('db.coll', 'shard1');
1854+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
1855+
ADMIN_DB,
1856+
{
1857+
unshardCollection: 'db.coll',
1858+
toShard: 'shard1',
1859+
}
1860+
);
1861+
});
1862+
1863+
it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
1864+
const expectedResult = { ok: 1 };
1865+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
1866+
const result = await shard.unshardCollection('db.coll', 'shard1');
1867+
expect(result).to.deep.equal(expectedResult);
1868+
});
1869+
1870+
it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
1871+
const expectedError = new Error();
1872+
serviceProvider.runCommandWithCheck.rejects(expectedError);
1873+
const caughtError = await shard
1874+
.unshardCollection('db.coll', 'shard1')
1875+
.catch((e) => e);
1876+
expect(caughtError).to.equal(expectedError);
1877+
});
1878+
});
1879+
1880+
describe('abortUnshardCollection', function () {
1881+
it('calls serviceProvider.runCommandWithCheck', async function () {
1882+
await shard.abortUnshardCollection('db.coll');
1883+
expect(serviceProvider.runCommandWithCheck).to.have.been.calledWith(
1884+
ADMIN_DB,
1885+
{
1886+
abortUnshardCollection: 'db.coll',
1887+
}
1888+
);
1889+
});
1890+
1891+
it('returns whatever serviceProvider.runCommandWithCheck returns', async function () {
1892+
const expectedResult = { ok: 1 };
1893+
serviceProvider.runCommandWithCheck.resolves(expectedResult);
1894+
const result = await shard.abortUnshardCollection('db.coll');
1895+
expect(result).to.deep.equal(expectedResult);
1896+
});
1897+
1898+
it('throws if serviceProvider.runCommandWithCheck rejects', async function () {
1899+
const expectedError = new Error();
1900+
serviceProvider.runCommandWithCheck.rejects(expectedError);
1901+
const caughtError = await shard
1902+
.abortUnshardCollection('db.coll')
1903+
.catch((e) => e);
1904+
expect(caughtError).to.equal(expectedError);
1905+
});
1906+
});
17931907
});
17941908

17951909
describe('integration', function () {

packages/shell-api/src/shard.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,4 +693,61 @@ export default class Shard extends ShellApiWithMongoClass {
693693
checkMetadataConsistency: 1,
694694
});
695695
}
696+
697+
@serverVersions(['8.0.0', ServerVersions.latest])
698+
@apiVersions([])
699+
@returnsPromise
700+
async moveCollection(ns: string, toShard: string): Promise<Document> {
701+
assertArgsDefinedType(
702+
[ns, toShard],
703+
['string', 'string'],
704+
'Shard.moveCollection'
705+
);
706+
this._emitShardApiCall('moveCollection', { moveCollection: ns, toShard });
707+
return await this._database._runAdminCommand({
708+
moveCollection: ns,
709+
toShard,
710+
});
711+
}
712+
713+
@serverVersions(['8.0.0', ServerVersions.latest])
714+
@apiVersions([])
715+
@returnsPromise
716+
async abortMoveCollection(ns: string): Promise<Document> {
717+
assertArgsDefinedType([ns], ['string'], 'Shard.abortMoveCollection');
718+
this._emitShardApiCall('abortMoveCollection', { abortMoveCollection: ns });
719+
return await this._database._runAdminCommand({ abortMoveCollection: ns });
720+
}
721+
722+
@serverVersions(['8.0.0', ServerVersions.latest])
723+
@apiVersions([])
724+
@returnsPromise
725+
async unshardCollection(ns: string, toShard: string): Promise<Document> {
726+
assertArgsDefinedType(
727+
[ns, toShard],
728+
['string', 'string'],
729+
'Shard.unshardCollection'
730+
);
731+
this._emitShardApiCall('unshardCollection', {
732+
unshardCollection: ns,
733+
toShard,
734+
});
735+
return await this._database._runAdminCommand({
736+
unshardCollection: ns,
737+
toShard,
738+
});
739+
}
740+
741+
@serverVersions(['8.0.0', ServerVersions.latest])
742+
@apiVersions([])
743+
@returnsPromise
744+
async abortUnshardCollection(ns: string): Promise<Document> {
745+
assertArgsDefinedType([ns], ['string'], 'Shard.abortUnshardCollection');
746+
this._emitShardApiCall('abortUnshardCollection', {
747+
abortUnshardCollection: ns,
748+
});
749+
return await this._database._runAdminCommand({
750+
abortUnshardCollection: ns,
751+
});
752+
}
696753
}

0 commit comments

Comments
 (0)