summaryrefslogtreecommitdiff
path: root/jstests/sharding/implicit_db_creation.js
blob: 40edd62ac11a21ac7a67831436b7b974f9f74941 (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
/**
 * This tests the basic cases for implicit database creation in a sharded cluster.
 */
(function() {
"use strict";

const st = new ShardingTest({shards: 2});
const configDB = st.s.getDB('config');

assert.eq(null, configDB.databases.findOne());

const testDB = st.s.getDB('test');

// Test that reads will not result into a new config.databases entry.
assert.eq(null, testDB.user.findOne());
assert.eq(null, configDB.databases.findOne({_id: 'test'}));

assert.commandWorked(testDB.user.insert({x: 1}));

const testDBDoc = configDB.databases.findOne();
assert.eq('test', testDBDoc._id, tojson(testDBDoc));

// Test that inserting to another collection in the same database will not modify the existing
// config.databases entry.
assert.commandWorked(testDB.bar.insert({y: 1}));
assert.eq(testDBDoc, configDB.databases.findOne());

st.s.adminCommand({enableSharding: 'foo'});
assert.neq(null, configDB.databases.findOne({_id: 'foo'}));

const newShard = new ReplSetTest({nodes: 1});
newShard.startSet({shardsvr: ""});
newShard.initiate();

const unshardedDB = newShard.getPrimary().getDB('unshardedDB');

unshardedDB.user.insert({z: 1});

assert.commandWorked(st.s.adminCommand({addShard: newShard.getURL()}));

assert.neq(null, configDB.databases.findOne({_id: 'unshardedDB'}));

newShard.stopSet();
st.stop();
})();