-
Notifications
You must be signed in to change notification settings - Fork 2
/
preflight-downgrade22.js
57 lines (57 loc) · 1.99 KB
/
preflight-downgrade22.js
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
function isInt(n) {
return typeof n === 'number' && n % 1 == 0;
}
function isValidIndex(n) {
if(isInt(n)){
if (n == -1 || n == 1){ return true;}
} else {
if (n == "2d") { return true;}
}
return false;
}
function minVerCheck(v){
need = v.split(".")
ver = db.version().split(".");
if(ver[0] < need[0]){ return false;}
if(ver[1] < need[1]){ return false;}
if(ver[2] < need[2]){ return false;}
return true;
}
//Mongodb Upgrade Preflight script
doaggr = minVerCheck("2.2.0");
if(!doaggr){ print("--NOTICE-- You are running with " + db.version() + " which doesnt support aggregeation. Not running duplicate user check")}
var dbs = db.getMongo().getDBNames();
for (var i = 0; i < dbs.length; i++){
print("Checking db " + dbs[i])
//If we can use aggregation check for duplicate users
if(doaggr){
aggr = db.getSiblingDB(dbs[i]).system.users.aggregate([{ $group : { _id : "$user" , number : { $sum : 1 } } },{ $match : { number : { $gt : 1} } }]).result
if(aggr != ""){
print("Error, the following users are duplicated" + tojson(aggr));
}
}
if(dbs[i] != "local"){
//Bad Index Checking
indexes = db.getSiblingDB(dbs[i]).system.indexes.find();
for (var x = 0; x < indexes.length(); x++){
index = indexes[x];
for(var key in index.key){
if(!isValidIndex(index.key[key])){
print("--WARNING-- " + tojsononeline(index) + " has a bad value value as part of key " + tojson(index.key) + " this index should be removed")
}
}
}
//Capped without _id checking
var cols = db.getSiblingDB(dbs[i]).getCollectionNames();
for (var x = 0; x < cols.length; x++){
if(!cols[x].startsWith("system.")){
col = db.getSiblingDB(dbs[i]).getCollection(cols[x])
if(col.isCapped()){
if(db.getSiblingDB(dbs[i]).system.indexes.find({ns: dbs[i] + "." + cols[x], key : {_id :1 }}).count() == 0){
print("--WARNING-- " + dbs[i] + "." + cols[x] + " is capped but has no index on _id");
}
}
}
}
}
}