summaryrefslogtreecommitdiff
path: root/jstests/replsets/copydb.js
blob: e63f6e4b39303a67b440b433ffd6b728ee552728 (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
88
89
90
91
92
93
94
95
96
97
// Tests the copydb command in a replica set.
// Ensures that documents and indexes are replicated to secondary.

(function() {
    'use strict';

    var replTest = new ReplSetTest({name: 'copydbTest', nodes: 3});

    replTest.startSet();
    replTest.initiate();
    var primary = replTest.getPrimary();
    var secondary = replTest.liveNodes.slaves[0];

    var sourceDBName = 'copydb-repl-test-source';
    var targetDBName = 'copydb-repl-test-target';

    var primarySourceDB = primary.getDB(sourceDBName);
    assert.commandWorked(primarySourceDB.dropDatabase(),
                         'failed to drop source database ' + sourceDBName + ' on primary');

    var primaryTargetDB = primary.getDB(targetDBName);
    assert.commandWorked(primaryTargetDB.dropDatabase(),
                         'failed to drop target database ' + targetDBName + ' on primary');

    assert.writeOK(primarySourceDB.foo.save({a: 1}),
                   'failed to insert document in source collection');
    assert.commandWorked(primarySourceDB.foo.ensureIndex({a: 1}),
                         'failed to create index in source collection on primary');
    assert.commandWorked(primarySourceDB.runCommand({create: "fooView", viewOn: "foo"}),
                         'failed to create view on source collection on primary');

    assert.eq(1,
              primarySourceDB.foo.find().itcount(),
              'incorrect number of documents in source collection on primary before copy');
    assert.eq(1,
              primarySourceDB.fooView.find().itcount(),
              'incorrect number of documents in source view on primary before copy');
    assert.eq(0,
              primaryTargetDB.foo.find().itcount(),
              'target collection on primary should be empty before copy');

    assert.commandWorked(
        primarySourceDB.copyDatabase(primarySourceDB.getName(), primaryTargetDB.getName()),
        'failed to copy database');

    assert.eq(primarySourceDB.foo.find().itcount(),
              primaryTargetDB.foo.find().itcount(),
              'incorrect number of documents in target collection on primary after copy');

    // Confirm that 'fooView' is still a view namespace after copy.
    let res = primaryTargetDB.runCommand({listCollections: 1, filter: {name: "fooView"}});
    assert.commandWorked(res);
    assert(res.cursor.firstBatch.length === 1);
    assert(res.cursor.firstBatch[0].hasOwnProperty("type"), tojson(res));
    assert.eq("view",
              res.cursor.firstBatch[0].type,
              "Namespace exected to be view: " + tojson(res.cursor.firstBatch[0]));

    assert.eq(primarySourceDB.fooView.find().itcount(),
              primaryTargetDB.fooView.find().itcount(),
              'incorrect number of documents in target view on primary after copy');

    assert.eq(primarySourceDB.foo.getIndexes().length,
              primaryTargetDB.foo.getIndexes().length,
              'incorrect number of indexes in target collection on primary after copy');

    replTest.awaitReplication();

    var secondarySourceDB = secondary.getDB(sourceDBName);

    assert.eq(primarySourceDB.foo.find().itcount(),
              secondarySourceDB.foo.find().itcount(),
              'incorrect number of documents in source collection on secondary after copy');

    assert.eq(primarySourceDB.fooView.find().itcount(),
              secondarySourceDB.fooView.find().itcount(),
              'incorrect number of documents in source view on secondary after copy');

    assert.eq(primarySourceDB.foo.getIndexes().length,
              secondarySourceDB.foo.getIndexes().length,
              'incorrect number of indexes in source collection on secondary after copy');

    var secondaryTargetDB = secondary.getDB(targetDBName);

    assert.eq(primaryTargetDB.foo.find().itcount(),
              secondaryTargetDB.foo.find().itcount(),
              'incorrect number of documents in target collection on secondary after copy');

    assert.eq(primaryTargetDB.fooView.find().itcount(),
              secondaryTargetDB.fooView.find().itcount(),
              'incorrect number of documents in target view on secondary after copy');

    assert.eq(primaryTargetDB.foo.getIndexes().length,
              secondaryTargetDB.foo.getIndexes().length,
              'incorrect number of indexes in target collection on secondary after copy');
    replTest.stopSet();
}());