summaryrefslogtreecommitdiff
path: root/jstests/replsets/rename_collection_temp.js
blob: 3add505a4f6da15a8754e8804ddf4b2686d873ae (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
// 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.

(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({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({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);
}());