summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/atomic_rename_collection.js
blob: cdc7e336c9149defeadf54511c33bf177588d6e2 (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
// @tags: [requires_replication,tenant_migration_incompatible]
(function() {
// SERVER-28285 When renameCollection drops the target collection, it should just generate
// a single oplog entry, so we cannot end up in a state where the drop has succeeded, but
// the rename didn't.
let rs = new ReplSetTest({nodes: 1});
rs.startSet();
rs.initiate();

let prim = rs.getPrimary();
let first = prim.getDB("first");
let second = prim.getDB("second");
let local = prim.getDB("local");

// Test both for rename within a database as across databases.
const tests = [
    {
        source: first.x,
        target: first.y,
        expectedOplogEntries: 1,
    },
    {
        source: first.x,
        target: second.x,
        expectedOplogEntries: 4,
    }
];
tests.forEach((test) => {
    test.source.drop();
    assert.commandWorked(test.source.insert({}));
    assert.commandWorked(test.target.insert({}));
    // Other things may be going on in the system; look only at oplog entries affecting the
    // particular databases under test.
    const dbregex =
        "^(" + test.source.getDB().getName() + ")|(" + test.target.getDB().getName() + ")\\.";

    let ts = local.oplog.rs.find().sort({$natural: -1}).limit(1).next().ts;
    let cmd = {
        renameCollection: test.source.toString(),
        to: test.target.toString(),
        dropTarget: true
    };
    assert.commandWorked(local.adminCommand(cmd), tojson(cmd));
    ops =
        local.oplog.rs.find({ts: {$gt: ts}, ns: {'$regex': dbregex}}).sort({$natural: 1}).toArray();
    assert.eq(ops.length,
              test.expectedOplogEntries,
              "renameCollection was supposed to only generate " + test.expectedOplogEntries +
                  " oplog entries: " + tojson(ops));
});
rs.stopSet();
})();