summaryrefslogtreecommitdiff
path: root/jstests/replsets/rename_collection_temp.js
blob: 57d33f8beb524d03b97fc360b98f82e80a4ba556 (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
// In SERVER-23274, the renameCollection command was found to incorrectly swap the "dropTarget" and
// "stayTemp" arguments when run on a replica set. In this test, we check that the arguments are
// correctly propagated.
// @tags: [requires_replication]

(function() {
    "use strict";

    function checkCollectionTemp(db, collName, expectedTempValue) {
        var collectionInformation = db.getCollectionInfos();

        var hasSeenCollection = false;
        for (var i = 0; i < collectionInformation.length; i++) {
            var collection = collectionInformation[i];

            if (collection.name === collName) {
                hasSeenCollection = true;

                if (expectedTempValue) {
                    // We expect this collection to be temporary.
                    assert.eq(collection.options.temp, true);
                } else {
                    // We expect this collection to be permanent, thus the temp option will not show
                    // up.
                    assert.isnull(collection.options.temp);
                }
            }
        }
    }

    var replTest = new ReplSetTest({name: 'renameCollectionTest', nodes: 2});
    var nodes = replTest.startSet();

    replTest.initiate();

    var master = replTest.getPrimary();

    // Create a temporary collection.
    var dbFoo = master.getDB("foo");

    assert.commandWorked(dbFoo.runCommand({
        applyOps:
            [{op: "c", ns: dbFoo.getName() + ".$cmd", o: {create: "tempColl", temp: true}}]
    }));
    checkCollectionTemp(dbFoo, "tempColl", true);

    // Rename the collection.
    assert.commandWorked(
        master.adminCommand({renameCollection: "foo.tempColl", to: "foo.permanentColl"}));

    // Confirm that it is no longer temporary.
    checkCollectionTemp(dbFoo, "permanentColl", false);

    replTest.awaitReplication();

    var secondary = replTest.getSecondary();
    var secondaryFoo = secondary.getDB("foo");

    secondaryFoo.permanentColl.setSlaveOk(true);

    // Get the information on the secondary to ensure it was replicated correctly.
    checkCollectionTemp(secondaryFoo, "permanentColl", false);

    // Check the behavior when the "dropTarget" flag is passed to renameCollection.
    dbFoo.permanentColl.drop();

    assert.commandWorked(dbFoo.runCommand({
        applyOps:
            [{op: "c", ns: dbFoo.getName() + ".$cmd", o: {create: "tempColl", temp: true}}]
    }));
    checkCollectionTemp(dbFoo, "tempColl", true);

    // Construct an empty collection that will be dropped on rename.
    assert.commandWorked(dbFoo.runCommand({create: "permanentColl"}));

    // Rename, dropping "permanentColl" and replacing it.
    assert.commandWorked(master.adminCommand(
        {renameCollection: "foo.tempColl", to: "foo.permanentColl", dropTarget: true}));

    checkCollectionTemp(dbFoo, "permanentColl", false);

    replTest.awaitReplication();

    checkCollectionTemp(secondaryFoo, "permanentColl", false);

    replTest.stopSet();
}());