summaryrefslogtreecommitdiff
path: root/jstests/core/txns/create_existing_indexes.js
blob: 5ec5dad43fef8040b25a69d1f1543c3602b25f51 (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
/* Tests that attempts to create indexes that already exist as of the createIndexes call are
 * permitted inside multi-document transactions. Also test that attempts to create new
 * indexes on existing collections are not permitted inside multi-document transactions.
 *
 * @tags: [uses_transactions,
 *         # Creating indexes inside multi-document transactions is supported only in v4.4 onwards.
 *         requires_fcv_44]
 */
(function() {
"use strict";

const session = db.getMongo().startSession({causalConsistency: false});
const collName = "create_existing_indexes";

let sessionDB = session.getDatabase("test");
let sessionColl = sessionDB[collName];
sessionColl.drop({writeConcern: {w: "majority"}});
assert.commandWorked(sessionDB.createCollection(collName, {writeConcern: {w: "majority"}}));
assert.commandWorked(sessionDB.runCommand({
    createIndexes: collName,
    indexes: [{key: {a: 1}, name: "a_1"}],
    writeConcern: {w: "majority"}
}));

jsTest.log("Testing createIndexes on an existing index in a transaction");
session.startTransaction({writeConcern: {w: "majority"}});

assert.commandWorked(
    sessionColl.runCommand({createIndexes: collName, indexes: [{key: {a: 1}, name: "a_1"}]}));

session.commitTransaction();

jsTest.log("Testing createIndexes on a conflicting index in a transaction (throws error)");
session.startTransaction({writeConcern: {w: "majority"}});

assert.commandFailedWithCode(
    sessionColl.runCommand({createIndexes: collName, indexes: [{key: {a: -1}, name: "a_1"}]}),
    ErrorCodes.IndexKeySpecsConflict);

assert.commandFailedWithCode(session.abortTransaction_forTesting(), ErrorCodes.NoSuchTransaction);

jsTest.log("Testing createIndexes on a new index in a transaction (throws error)");
session.startTransaction({writeConcern: {w: "majority"}});

assert.commandFailedWithCode(
    sessionColl.runCommand({createIndexes: collName, indexes: [{key: {b: 1}, name: "b_1"}]}),
    ErrorCodes.OperationNotSupportedInTransaction);
assert.commandFailedWithCode(session.abortTransaction_forTesting(), ErrorCodes.NoSuchTransaction);

session.endSession();
}());