summaryrefslogtreecommitdiff
path: root/jstests/sharding/shard_key_index_must_exist.js
blob: d9c6ff9072399570579b9d4a6f5dd5a92bd7729d (plain)
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
65
66
67
68
69
70
71
72
73
74
(function() {
'use strict';

let st = new ShardingTest({shards: 1});

assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));

let testDB = st.getDB('test');

let checkIndex = function(collName, expectedIndexNames) {
    let indexNotSeen = expectedIndexNames;

    testDB.getCollection(collName).getIndexes().forEach((index) => {
        assert(expectedIndexNames.includes(index.name),
               'index should not expected to exist: ' + tojson(index));

        indexNotSeen = indexNotSeen.filter((name) => {
            return name == index.name;
        });
    });

    assert.eq([], indexNotSeen);
};

(() => {
    assert.commandWorked(st.s.adminCommand({shardCollection: 'test.user', key: {x: 1}}));
    assert.commandFailedWithCode(testDB.runCommand({dropIndexes: 'user', index: {x: 1}}),
                                 ErrorCodes.CannotDropShardKeyIndex);

    assert.commandWorked(testDB.runCommand({
        createIndexes: 'user',
        indexes: [
            {key: {x: 1, y: 1}, name: 'xy'},
            {key: {x: 1, z: 1}, name: 'xz'},
            {key: {a: 1}, name: 'a'}
        ]
    }));

    assert.commandWorked(testDB.runCommand({dropIndexes: 'user', index: '*'}));
    checkIndex('user', ['_id_', 'x_1', 'xy', 'xz']);

    assert.commandWorked(testDB.runCommand({dropIndexes: 'user', index: {x: 1}}));
    assert.commandWorked(testDB.runCommand({dropIndexes: 'user', index: {x: 1, z: 1}}));

    assert.commandFailedWithCode(testDB.runCommand({dropIndexes: 'user', index: {x: 1, y: 1}}),
                                 ErrorCodes.CannotDropShardKeyIndex);
    assert.commandFailedWithCode(testDB.runCommand({dropIndexes: 'user', index: 'xy'}),
                                 ErrorCodes.CannotDropShardKeyIndex);

    checkIndex('user', ['_id_', 'xy']);

    assert.commandWorked(testDB.runCommand({drop: 'user'}));
})();

(() => {
    assert.commandWorked(st.s.adminCommand({shardCollection: 'test.hashed', key: {x: 'hashed'}}));

    assert.commandWorked(
        testDB.runCommand({createIndexes: 'hashed', indexes: [{key: {x: 1, y: 1}, name: 'xy'}]}));

    assert.commandWorked(testDB.runCommand({dropIndexes: 'hashed', index: '*'}));

    checkIndex('hashed', ['_id_', 'x_hashed']);

    assert.commandFailedWithCode(testDB.runCommand({dropIndexes: 'hashed', index: 'x_hashed'}),
                                 ErrorCodes.CannotDropShardKeyIndex);

    checkIndex('hashed', ['_id_', 'x_hashed']);

    assert.commandWorked(testDB.runCommand({drop: 'hashed'}));
})();

st.stop();
})();