summaryrefslogtreecommitdiff
path: root/jstests/multiVersion/index_value_empty_string_upgrade.js
blob: aa9ebf3b4d6acd69043c1fbe9f4fe93894e842d1 (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
/**
 * Test that index keys with empty string values are allowed on 4.0 and that upgrading with
 * such indexes will succeed.
 */

(function() {
'use strict';

load('jstests/libs/get_index_helpers.js');

const dbpath = MongoRunner.dataPath + 'empty_string_index_value';
resetDbpath(dbpath);

const oldVersion = '4.0';
const newVersion = 'latest';

// We set noCleanData to true in order to preserve the data files across mongod restart.
const mongodOptions = {
    dbpath: dbpath,
    noCleanData: true,
    binVersion: oldVersion
};

// Start up an old binary version mongod.
let conn = MongoRunner.runMongod(mongodOptions);

assert.neq(null, conn, `mongod was unable able to start with version ${oldVersion}`);

// Set up a collection on a 4.0 binary version node with one document and an index with
// an empty string as index value, and then shut it down.
let testDB = conn.getDB('test');
assert.commandWorked(testDB.createCollection('testColl'));
assert.commandWorked(testDB.testColl.insert({a: 1}));
assert.commandWorked(testDB.testColl.createIndex({a: ""}));
MongoRunner.stopMongod(conn);

// Restart the mongod with the latest binary version and the 4.0 version data files.
mongodOptions.binVersion = newVersion;
conn = MongoRunner.runMongod(mongodOptions);
assert.neq(null, conn);

// Confirm that mongod startup does not fail due to the index specification
// containing an empty string.
testDB = conn.getDB('test');
testDB.testColl.find();
assert.eq(1,
          testDB.testColl.count({}, {hint: {a: ""}}),
          `data from ${oldVersion} should be available; options: ` + tojson(mongodOptions));

assert.neq(null,
           GetIndexHelpers.findByKeyPattern(testDB.testColl.getIndexes(), {a: ""}),
           `index from ${oldVersion} should be available; options: ` + tojson(mongodOptions));

// Verify that indexes with empty string values cannot be created
assert.commandFailedWithCode(testDB.testColl.createIndex({x: ""}), ErrorCodes.CannotCreateIndex);

MongoRunner.stopMongod(conn);
})();