-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongo.migrate.js
106 lines (99 loc) · 2.7 KB
/
mongo.migrate.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
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
98
99
100
101
102
103
104
105
106
var mode="do"; // only "do" will do
var appName = 'hcc';
var modules = {
'auth': [
'musers',
'musergroups',
'maccounts',
'mroles',
'maccountRoles',
'mmodules',
'mpubmodules',
'mpermissions',
'mpubaccesses',
],
'file': [
'mfiles',
'mfilegroups',
],
'email': [
'emailsettings',
'emailtemplates'
],
'publicinfo': [
'Events',
"Articles",
'Faqs',
'GeneralInfos',
'KeyNotes',
'Alerts',
'Pages',
],
'summerProgram': [
'SPEnrollments',
],
'teachforlife': [
'Tutors',
'Enrollments',
],
};
var pluralizeWords = {
'mpubaccesses': 'mpubaccess',
'emailsettings': 'emailsettings',
}
var ignoreSchema = ['system.indexes'];
var formattedModules = {}
for (var i in modules) {
var schemas = modules[i];
formattedModules[i] = [];
for (var j = 0; j < schemas.length; j++) {
formattedModules[i].push(schemas[j].toLowerCase());
}
}
var findModuleName = function(schemaName) {
var sn = schemaName;
for (var i in formattedModules) {
var schemas = formattedModules[i];
for (var j = 0; j < schemas.length; j++) {
if (schemas[j] === sn) {
return i;
}
}
}
return null;
}
var collections = db.getCollectionNames();
for (var i = 0; i < collections.length; i+=1) {
collection = collections[i];
if (ignoreSchema.includes(collection)) {
print('collection ignored --- ' + collection);
} else if (collection.startsWith(appName)) {
print('started with app name!!! ' + collection);
} else {
let mname = findModuleName(collection);
if (mname) {
let newCollection = collection.slice(0, -1);
if (pluralizeWords[collection]) {
newCollection = pluralizeWords[collection];
}
var newName = `${appName}_${mname}_${newCollection}`;
if (collections.includes(newName)) {
print(`need to migrate data ++++> ${collection} to ${newName}`);
if (mode === 'do') {
db[newName].drop();
db[collection].renameCollection(newName, false);
}
} else {
print(`need to change name ===> ${collection} to ${newName}`);
if (mode === 'do') {
db[collection].renameCollection(newName, false);
}
}
} else {
print(`need to delete collection <--- ${collection}`);
if (mode === 'do') {
db[collection].drop();
}
}
}
}