jstests/sharding/rename_sharded.js
/**
 * Initialize a "from" sharded collection with 2 chunks - on 2 different nodes - each containing 1
 * document. Rename to `toNs` with the provided options and get sure it succeeds/fails as expected.
 */
Initialize a "from" sharded collection with 2 chunks - on 2 different nodes - each containing 1
document. Rename to `toNs` with the provided options and get sure it succeeds/fails as expected.
Initialize a " from " sharded collection with 2 chunks - on 2 different nodes - each containing 1 document . Rename to ` toNs ` with the provided options and get sure it succeeds / fails as expected .
function testRename(st, dbName, toNs, dropTarget, mustFail) {
    const db = st.getDB(dbName);
    const mongos = st.s0;
    const fromNs = dbName + '.from';
    assert.commandWorked(
        mongos.adminCommand({enablesharding: dbName, primaryShard: st.shard0.shardName}));
    assert.commandWorked(mongos.adminCommand({shardCollection: fromNs, key: {x: 1}}));
    const fromColl = mongos.getCollection(fromNs);
    fromColl.insert({x: 0});
    fromColl.insert({x: 2});
    assert.commandWorked(mongos.adminCommand({split: fromNs, middle: {x: 1}}));
    const fromUUID = getUUIDFromConfigCollections(mongos, fromNs);
    const aChunk = mongos.getDB('config').chunks.findOne({uuid: fromUUID});
    assert.commandWorked(mongos.adminCommand({
        moveChunk: fromNs,
        bounds: [aChunk.min, aChunk.max],
        to: st.shard1.shardName,
    }));
    const res = fromColl.renameCollection(toNs.split('.')[1], dropTarget);
    if (mustFail) {
        assert.commandFailed(res);
        return;
    }
    assert.commandWorked(res);
    const toUUID = getUUIDFromConfigCollections(mongos, toNs);
    const chunks = mongos.getDB('config').chunks.find({uuid: toUUID});
    const chunk0 = chunks.next();
    const chunk1 = chunks.next();
    assert(!chunks.hasNext(), 'Target collection expected to have exactly 2 chunks');
    assert(chunk0.shard != chunk1.shard, 'Chunks expected to be on different shards');
    const toColl = mongos.getCollection(toNs);
    assert.eq(toColl.find({x: 0}).itcount(), 1, 'Expected exactly one document on the shard');
    assert.eq(toColl.find({x: 2}).itcount(), 1, 'Expected exactly one document on the shard');
}
function testRename ( st , dbName , toNs , dropTarget , mustFail ) { const db = st . getDB ( dbName ) ; const mongos = st . s0 ; const fromNs = dbName + '.from' ; assert . commandWorked ( mongos . adminCommand ( { enablesharding : dbName , primaryShard : st . shard0 . shardName } ) ) ; assert . commandWorked ( mongos . adminCommand ( { shardCollection : fromNs , key : { x : 1 } } ) ) ; const fromColl = mongos . getCollection ( fromNs ) ; fromColl . insert ( { x : 0 } ) ; fromColl . insert ( { x : 2 } ) ; assert . commandWorked ( mongos . adminCommand ( { split : fromNs , middle : { x : 1 } } ) ) ; const fromUUID = getUUIDFromConfigCollections ( mongos , fromNs ) ; const aChunk = mongos . getDB ( 'config' ) . chunks . findOne ( { uuid : fromUUID } ) ; assert . commandWorked ( mongos . adminCommand ( { moveChunk : fromNs , bounds : [ aChunk . min , aChunk . max ] , to : st . shard1 . shardName , } ) ) ; const res = fromColl . renameCollection ( toNs . split ( '.' ) [ 1 ] , dropTarget ) ; if ( mustFail ) { assert . commandFailed ( res ) ; return ; } assert . commandWorked ( res ) ; const toUUID = getUUIDFromConfigCollections ( mongos , toNs ) ; const chunks = mongos . getDB ( 'config' ) . chunks . find ( { uuid : toUUID } ) ; const chunk0 = chunks . next ( ) ; const chunk1 = chunks . next ( ) ; assert ( ! chunks . hasNext ( ) , 'Target collection expected to have exactly 2 chunks' ) ; assert ( chunk0 . shard != chunk1 . shard , 'Chunks expected to be on different shards' ) ; const toColl = mongos . getCollection ( toNs ) ; assert . eq ( toColl . find ( { x : 0 } ) . itcount ( ) , 1 , 'Expected exactly one document on the shard' ) ; assert . eq ( toColl . find ( { x : 2 } ) . itcount ( ) , 1 , 'Expected exactly one document on the shard' ) ; }
Initialize a "from" sharded collection with 2 chunks - on 2 different nodes - each containing 1
 document.
Initialize a " from " sharded collection with 2 chunks - on 2 different nodes - each containing 1 document .