1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/**
* Tests that an index with unique or prepareUnique option cannot be built on a sharded collection
* with an incompatible shard key, or the collection cannot be sharded with an incompatible
* unique/prepareUnique index.
*
* @tags: [
* # TODO(SERVER-61182): Fix WiredTigerKVEngine::alterIdentMetadata() under inMemory.
* requires_persistence,
* ]
*/
(function() {
"use strict";
const st = new ShardingTest({shards: 1, mongos: 1});
const dbName = "db";
const db = st.getDB(dbName);
const collNamePrefix = jsTestName();
let count = 0;
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
// Cannot shard the collection with a conflicting prepareUnique index.
let coll = db[collNamePrefix + count++];
assert.commandWorked(coll.createIndex({a: 1}, {prepareUnique: 1}));
assert.commandFailedWithCode(st.s.adminCommand({shardCollection: coll.getFullName(), key: {b: 1}}),
ErrorCodes.InvalidOptions);
// Can shard the collection with a compatible prepareUnique index.
assert.commandWorked(st.s.adminCommand({shardCollection: coll.getFullName(), key: {a: 1}}));
// Cannot shard the collection with a conflicting unique index.
coll = db[collNamePrefix + count++];
assert.commandWorked(coll.createIndex({a: 1}, {unique: 1}));
assert.commandFailedWithCode(st.s.adminCommand({shardCollection: coll.getFullName(), key: {b: 1}}),
ErrorCodes.InvalidOptions);
// Can shard the collection with a compatible unique index.
assert.commandWorked(st.s.adminCommand({shardCollection: coll.getFullName(), key: {a: 1}}));
// Cannot create the index with a conflicting shard key.
coll = db[collNamePrefix + count++];
assert.commandWorked(st.s.adminCommand({shardCollection: coll.getFullName(), key: {b: 1}}));
assert.commandFailedWithCode(coll.createIndex({a: 1}, {prepareUnique: 1}),
ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({a: 1}, {unique: 1}), ErrorCodes.CannotCreateIndex);
// Can create the index with a compatible shard key.
assert.commandWorked(coll.createIndex({b: 1, a: 1}, {prepareUnique: 1}));
assert.commandWorked(coll.createIndex({b: 1, c: 1}, {unique: 1}));
// Cannot convert an index to prepareUnique with a conflicting shard key.
coll = db[collNamePrefix + count++];
assert.commandWorked(st.s.adminCommand({shardCollection: coll.getFullName(), key: {b: 1}}));
assert.commandWorked(coll.createIndex({a: 1}));
assert.commandFailedWithCode(
db.runCommand({collMod: coll.getName(), index: {keyPattern: {a: 1}, prepareUnique: true}}),
ErrorCodes.InvalidOptions);
// Can convert an index to prepareUnique and unique with a compatible shard key.
assert.commandWorked(coll.createIndex({b: 1, d: 1}));
assert.commandWorked(db.runCommand(
{collMod: coll.getName(), index: {keyPattern: {b: 1, d: 1}, prepareUnique: true}}));
assert.commandWorked(
db.runCommand({collMod: coll.getName(), index: {keyPattern: {b: 1, d: 1}, unique: true}}));
st.stop();
})();
|