summaryrefslogtreecommitdiff
path: root/jstests/change_streams/ddl_create_event.js
blob: c5516bc54b53c59ca10b84b1977a3ca5a9b49cb1 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
 * Test that change streams returns create events which captures the options specified on the
 * original user command.
 *
 * @tags: [ requires_fcv_60, ]
 */
(function() {
"use strict";

load('jstests/libs/collection_drop_recreate.js');  // For 'assertDropAndRecreateCollection' and
                                                   // 'assertDropCollection'.
load('jstests/libs/change_stream_util.js');        // For 'ChangeStreamTest' and
                                                   // 'assertChangeStreamEventEq'.
load("jstests/libs/feature_flag_util.js");

const testDB = db.getSiblingDB(jsTestName());

const dbName = testDB.getName();
const collName = jsTestName();
const ns = {
    db: dbName,
    coll: collName
};

const test = new ChangeStreamTest(testDB);

function getCollectionUuid(coll) {
    const collInfo = testDB.getCollectionInfos({name: coll})[0];
    return collInfo.info.uuid;
}

function assertNextChangeEvent(cursor, expectedEvent) {
    const event = test.getOneChange(cursor);

    // Check the presence and the type of 'wallTime' field. We have no way to check the correctness
    // of 'wallTime' value, so we delete it afterwards.
    assert(event.wallTime instanceof Date);
    delete event.wallTime;

    expectedEvent.collectionUUID = getCollectionUuid(collName);
    assertChangeStreamEventEq(event, expectedEvent);
}

function runTest(startChangeStream) {
    function validateExpectedEventAndDropCollection(command, expectedOutput) {
        const cursor = startChangeStream();
        assert.commandWorked(testDB.runCommand(command));
        assertNextChangeEvent(cursor, expectedOutput);
        assertDropCollection(testDB, collName);
    }

    // Basic create collection command.
    validateExpectedEventAndDropCollection({create: collName}, {
        operationType: "create",
        ns: ns,
        operationDescription: {idIndex: {v: 2, key: {_id: 1}, name: "_id_"}}
    });

    // With implicit create collection through insert.
    let cursor = startChangeStream();
    assert.commandWorked(testDB.runCommand({insert: collName, documents: [{_id: 0}]}));
    assertNextChangeEvent(cursor, {
        operationType: "create",
        ns: ns,
        operationDescription: {idIndex: {v: 2, key: {_id: 1}, name: "_id_"}}
    });
    assertNextChangeEvent(
        cursor, {operationType: "insert", fullDocument: {_id: 0}, ns: ns, documentKey: {_id: 0}});
    assertDropCollection(testDB, collName);

    // With capped collection parameters.
    let expectedSize;

    if (FeatureFlagUtil.isEnabled(testDB, "CappedCollectionsRelaxedSize")) {
        expectedSize = 1000;
    } else {
        expectedSize = 1024;
    }
    validateExpectedEventAndDropCollection({create: collName, capped: true, size: 1000, max: 1000},
                                           {
                                               operationType: "create",
                                               ns: ns,
                                               operationDescription: {
                                                   idIndex: {v: 2, key: {_id: 1}, name: "_id_"},
                                                   capped: true,
                                                   size: expectedSize,
                                                   max: 1000
                                               }
                                           });

    // With wired tiger setting.
    const customWiredTigerSettings = {wiredTiger: {configString: "block_compressor=zlib"}};
    validateExpectedEventAndDropCollection(
        {
            create: collName,
            indexOptionDefaults: {storageEngine: customWiredTigerSettings},
            storageEngine: customWiredTigerSettings
        },
        {
            operationType: "create",
            ns: ns,
            operationDescription: {
                idIndex: {v: 2, key: {_id: 1}, name: "_id_"},
                indexOptionDefaults: {storageEngine: customWiredTigerSettings},
                storageEngine: customWiredTigerSettings
            }
        });

    // With validator collection parameters.
    validateExpectedEventAndDropCollection(
        {create: collName, validator: {a: 1}, validationLevel: "off", validationAction: "warn"}, {
            operationType: "create",
            ns: ns,
            operationDescription: {
                idIndex: {v: 2, key: {_id: 1}, name: "_id_"},
                validator: {a: 1},
                validationLevel: "off",
                validationAction: "warn"
            }
        });

    // With collation.
    const collation = {
        locale: "en",
        "caseLevel": false,
        "caseFirst": "off",
        "strength": 3,
        "numericOrdering": false,
        "alternate": "non-ignorable",
        "maxVariable": "punct",
        "normalization": false,
        "backwards": true,
        "version": "57.1"
    };
    validateExpectedEventAndDropCollection({create: collName, collation: collation}, {
        operationType: "create",
        ns: ns,
        operationDescription: {
            collation: collation,
            idIndex: {v: 2, key: {_id: 1}, name: "_id_", collation: collation}
        }
    });

    // With clustered index.
    validateExpectedEventAndDropCollection(
        {
            create: collName,
            clusteredIndex: {key: {_id: 1}, name: "newName", unique: true},
            expireAfterSeconds: 10
        },
        {
            operationType: "create",
            ns: ns,
            operationDescription: {
                clusteredIndex: {v: 2, key: {_id: 1}, name: "newName", unique: true},
                expireAfterSeconds: NumberLong(10)
            }
        });

    // With idIndex field.
    validateExpectedEventAndDropCollection(
        {create: collName, idIndex: {v: 2, key: {_id: 1}, name: "ignored"}}, {
            operationType: "create",
            ns: ns,
            operationDescription: {
                idIndex: {v: 2, key: {_id: 1}, name: "_id_"},
            }
        });
    validateExpectedEventAndDropCollection(
        {create: collName, idIndex: {v: 1, key: {_id: 1}, name: "new"}},
        {operationType: "create", ns: ns, operationDescription: {}});

    // Verify that the time-series create command does not produce an event.
    cursor = startChangeStream();
    assert.commandWorked(testDB.runCommand({create: collName, timeseries: {timeField: "t"}}));
    test.assertNextChangesEqual({cursor: cursor, expectedChanges: []});
    assertDropCollection(testDB, collName);

    if (FixtureHelpers.isMongos(db)) {
        cursor = startChangeStream();

        assert.commandWorked(testDB.adminCommand({enableSharding: ns.db}));
        assert.commandWorked(
            testDB.adminCommand({shardCollection: ns.db + "." + collName, key: {a: 1}}));

        assertNextChangeEvent(cursor, {
            operationType: "create",
            ns,
            operationDescription: {
                idIndex: {v: 2, key: {_id: 1}, name: "_id_"},
            }
        });
        assertDropCollection(testDB, collName);
    }
}

const pipeline = [{$changeStream: {showExpandedEvents: true}}];
runTest(() => test.startWatchingChanges({pipeline, collection: 1}));
runTest(() => test.startWatchingChanges({pipeline, collection: collName}));
}());