summaryrefslogtreecommitdiff
path: root/jstests/replsets/background_index.js
blob: 3b30264443856f206a63439f354404638ad6df2d (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
/**
 * Tests that a background index will be successfully
 *  replicated to a secondary when the indexed collection
 *  is renamed.
 */

(function() {
"use strict";

// Bring up a 2 node replset.
var name = "bg_index_rename";
var rst = new ReplSetTest({name: name, nodes: 3});
rst.startSet();
rst.initiate();

// Create and populate a collection.
var primary = rst.getPrimary();
var coll = primary.getCollection("test.foo");
var adminDB = primary.getDB("admin");

for (var i = 0; i < 100; i++) {
    assert.writeOK(coll.insert({_id: i, x: i * 3, str: "hello world"}));
}

// Add a background index.
coll.ensureIndex({x: 1}, {background: true});

// Rename the collection.
assert.commandWorked(
    adminDB.runCommand({renameCollection: "test.foo", to: "bar.test", dropTarget: true}),
    "Call to renameCollection failed.");

// Await replication.
rst.awaitReplication();

// Step down the primary.
assert.commandWorked(adminDB.runCommand({replSetStepDown: 60, force: true}));

// Wait for new primary.
var newPrimary = rst.getPrimary();
assert.neq(primary, newPrimary);
var barDB = newPrimary.getDB("bar");
coll = newPrimary.getCollection("bar.test");
coll.insert({_id: 200, x: 600, str: "goodnight moon"});

// Check that the new primary has the index
// on the renamed collection.
var indexes = barDB.runCommand({listIndexes: "test"});
assert.eq(indexes.cursor.firstBatch.length, 2);

rst.stopSet();
}());