Revision 59470
Added by Konstantina Galouni over 3 years ago
migrateCommunityIn_db.js | ||
---|---|---|
1 | 1 |
//version compatibility: 1.1.1-SNAPSHOT |
2 | 2 |
print("migrateCommunityIn_db script running..."); |
3 | 3 |
|
4 |
function migrate_community(communityPid, beta_db_name, prod_db_name) {
|
|
4 |
function migrate_portal(portalPid, beta_db_name, prod_db_name) {
|
|
5 | 5 |
beta_db = db.getSiblingDB(beta_db_name); |
6 | 6 |
prod_db = db.getSiblingDB(prod_db_name); |
7 | 7 |
|
8 |
print("migrate_community: "+communityPid+" - both dbs are here");
|
|
8 |
print("migrate_portal: "+portalPid+" - both dbs are here");
|
|
9 | 9 |
|
10 |
var beta_community = beta_db.community.findOne({ pid: communityPid } );
|
|
10 |
var beta_portal = beta_db.portal.findOne({ pid: portalPid } );
|
|
11 | 11 |
|
12 |
if(!beta_community) {
|
|
13 |
print("ERROR: Community with pid: "+communityPid+" does not exist in (beta) database: "+ beta_db_name);
|
|
12 |
if(!beta_portal) {
|
|
13 |
print("ERROR: Portal with pid: "+portalPid+" does not exist in (beta) database: "+ beta_db_name);
|
|
14 | 14 |
return; |
15 | 15 |
} |
16 | 16 |
|
17 |
var prod_communityOld = prod_db.community.findOne( { pid: communityPid } );
|
|
18 |
var prod_communityOldId;
|
|
19 |
if(prod_communityOld) {
|
|
20 |
prod_communityOldId = prod_communityOld._id.str;
|
|
17 |
var prod_portalOld = prod_db.portal.findOne( { pid: portalPid } );
|
|
18 |
var prod_portalOldId;
|
|
19 |
if(prod_portalOld) {
|
|
20 |
prod_portalOldId = prod_portalOld._id.str;
|
|
21 | 21 |
} |
22 | 22 |
|
23 |
// delete community from production db
|
|
24 |
prod_db.community.remove({"pid" : communityPid});
|
|
25 |
print("Community with pid: "+communityPid+" is deleted (if existed) from production db: "+prod_db_name);
|
|
23 |
// delete portal from production db
|
|
24 |
prod_db.portal.remove({"pid" : portalPid});
|
|
25 |
print("Portal with pid: "+portalPid+" is deleted (if existed) from production db: "+prod_db_name);
|
|
26 | 26 |
|
27 |
// migrate community pages
|
|
28 |
//beta_community_pages = beta_db.community.findOne({ pid: communityPid } ).map( function(community) { return community.pages } );
|
|
29 |
var beta_community_pages = beta_community.pages;
|
|
30 |
var prod_community_pages = {}
|
|
31 |
for (var beta_pageId in beta_community_pages) {
|
|
27 |
// migrate portal pages
|
|
28 |
//beta_portal_pages = beta_db.portal.findOne({ pid: portalPid } ).map( function(portal) { return portal.pages } );
|
|
29 |
var beta_portal_pages = beta_portal.pages;
|
|
30 |
var prod_portal_pages = {}
|
|
31 |
for (var beta_pageId in beta_portal_pages) {
|
|
32 | 32 |
var beta_pageObjectId = ObjectId(beta_pageId); |
33 |
//var prod_pageId = prod_db.community.find( { route: beta_db.page.find( { _id: beta_pageObjectId }).map( function(page) { return page.route; } ).toString() });
|
|
33 |
//var prod_pageId = prod_db.portal.find( { route: beta_db.page.find( { _id: beta_pageObjectId }).map( function(page) { return page.route; } ).toString() });
|
|
34 | 34 |
var beta_page = beta_db.page.findOne( { _id: beta_pageObjectId } ); |
35 | 35 |
if(beta_page) { |
36 |
var prod_page = prod_db.page.findOne( { route: beta_page.route } ); |
|
36 |
var prod_page = prod_db.page.findOne( { route: beta_page.route, portalType: beta_page.portalType } );
|
|
37 | 37 |
if(prod_page) { |
38 |
prod_community_pages[prod_page._id.str] = beta_community_pages[beta_pageId];
|
|
38 |
prod_portal_pages[prod_page._id.str] = beta_portal_pages[beta_pageId];
|
|
39 | 39 |
} else { |
40 | 40 |
print("\nERROR: Page with route: "+beta_page.route+" does not exist on production db: "+prod_db_name+"\n"); |
41 | 41 |
} |
... | ... | |
43 | 43 |
print("\nERROR: Page with id: "+beta_pageId+" does not exist on beta db: "+beta_db_name+"\n"); |
44 | 44 |
} |
45 | 45 |
} |
46 |
print("Community pages are ready to be migrated");
|
|
46 |
print("Portal pages are ready to be migrated");
|
|
47 | 47 |
|
48 |
// migrate community entities
|
|
49 |
var beta_community_entities = beta_community.entities;
|
|
50 |
var prod_community_entities = {}
|
|
51 |
for (var beta_entityId in beta_community_entities) {
|
|
48 |
// migrate portal entities
|
|
49 |
var beta_portal_entities = beta_portal.entities;
|
|
50 |
var prod_portal_entities = {}
|
|
51 |
for (var beta_entityId in beta_portal_entities) {
|
|
52 | 52 |
var beta_entityObjectId = ObjectId(beta_entityId); |
53 | 53 |
var beta_entity = beta_db.entity.findOne( { _id: beta_entityObjectId } ); |
54 | 54 |
if(beta_entity) { |
55 | 55 |
var prod_entity = prod_db.entity.findOne( { pid: beta_entity.pid } ); |
56 | 56 |
if(prod_entity) { |
57 |
prod_community_entities[prod_entity._id.str] = beta_community_entities[beta_entityId];
|
|
57 |
prod_portal_entities[prod_entity._id.str] = beta_portal_entities[beta_entityId];
|
|
58 | 58 |
} else { |
59 | 59 |
print("\nERROR: Entity with pid: "+beta_entity.pid+" does not exist on production db: "+prod_db_name+"\n"); |
60 | 60 |
} |
... | ... | |
63 | 63 |
} |
64 | 64 |
} |
65 | 65 |
|
66 |
print("Community entities are ready to be migrated");
|
|
66 |
print("Portal entities are ready to be migrated");
|
|
67 | 67 |
|
68 |
prod_db.community.save({"name" : beta_community.name, "pid" : communityPid, "pages" : prod_community_pages, "entities" : prod_community_entities}); |
|
68 |
prod_db.portal.save({ |
|
69 |
"name" : beta_portal.name, "pid" : portalPid, "type": beta_portal.type, |
|
70 |
"pages" : prod_portal_pages, "entities" : prod_portal_entities |
|
71 |
}); |
|
69 | 72 |
|
70 |
print("Community is migrated");
|
|
73 |
print("Portal is migrated");
|
|
71 | 74 |
|
72 |
// delete community statistics from production db
|
|
73 |
prod_db.statistics.remove({"pid" : communityPid});
|
|
74 |
print("Statistics for community with pid: "+communityPid+" are deleted (if existed) from production db: "+prod_db_name);
|
|
75 |
// migrate statistics for community
|
|
76 |
var beta_statistics = beta_db.statistics.findOne({ pid: communityPid } );
|
|
75 |
// delete portal statistics from production db
|
|
76 |
prod_db.statistics.remove({"pid" : portalPid});
|
|
77 |
print("Statistics for portal with pid: "+portalPid+" are deleted (if existed) from production db: "+prod_db_name);
|
|
78 |
// migrate statistics for portal
|
|
79 |
var beta_statistics = beta_db.statistics.findOne({ pid: portalPid } );
|
|
77 | 80 |
prod_db.statistics.save(beta_statistics); |
78 | 81 |
|
79 |
print("Statistics for community were migrated");
|
|
82 |
print("Statistics for portal were migrated");
|
|
80 | 83 |
|
81 |
// migrate subscribers of community
|
|
82 |
var beta_communitySubscr = beta_db.communitySubscribers.findOne({ pid: communityPid } );
|
|
83 |
if(beta_communitySubscr) {
|
|
84 |
var beta_communitySubscribers = beta_communitySubscr.subscribers;
|
|
85 |
var prod_communitySubscribers = [];
|
|
86 |
var prod_communitySubscribersEmails = [];
|
|
87 |
var prod_communitySubscr = prod_db.communitySubscribers.findOne({ "pid" : communityPid });
|
|
88 |
if(prod_communitySubscr) {
|
|
89 |
prod_communitySubscribers = prod_communitySubscr.subscribers;
|
|
90 |
prod_communitySubscribersEmails = prod_communitySubscr.subscribers.map(function(subscriber){return subscriber.email});
|
|
84 |
// migrate subscribers of portal
|
|
85 |
var beta_portalSubscr = beta_db.portalSubscribers.findOne({ pid: portalPid } );
|
|
86 |
if(beta_portalSubscr) {
|
|
87 |
var beta_portalSubscribers = beta_portalSubscr.subscribers;
|
|
88 |
var prod_portalSubscribers = [];
|
|
89 |
var prod_portalSubscribersEmails = [];
|
|
90 |
var prod_portalSubscr = prod_db.portalSubscribers.findOne({ "pid" : portalPid });
|
|
91 |
if(prod_portalSubscr) {
|
|
92 |
prod_portalSubscribers = prod_portalSubscr.subscribers;
|
|
93 |
prod_portalSubscribersEmails = prod_portalSubscr.subscribers.map(function(subscriber){return subscriber.email});
|
|
91 | 94 |
} |
92 | 95 |
|
93 |
beta_communitySubscribers.forEach(function(beta_communitySubscriber) {
|
|
94 |
var addInCommunity = true;
|
|
96 |
beta_portalSubscribers.forEach(function(beta_portalSubscriber) {
|
|
97 |
var addInPortal = true;
|
|
95 | 98 |
|
96 |
// if user exists in community subscribers already, do nothing
|
|
97 |
if(prod_communitySubscribersEmails.indexOf(beta_communitySubscriber.email) >= 0) {
|
|
98 |
addInCommunity = false;
|
|
99 |
// if user exists in portal subscribers already, do nothing
|
|
100 |
if(prod_portalSubscribersEmails.indexOf(beta_portalSubscriber.email) >= 0) {
|
|
101 |
addInPortal = false;
|
|
99 | 102 |
} else { |
100 |
var prod_subscriber = prod_db.subscriber.findOne({email: beta_communitySubscriber.email});
|
|
103 |
var prod_subscriber = prod_db.subscriber.findOne({email: beta_portalSubscriber.email});
|
|
101 | 104 |
|
102 | 105 |
// if subscriber of beta does not exist in production, add him |
103 | 106 |
if(!prod_subscriber) { |
104 |
prod_subscriber = {email: beta_communitySubscriber.email};
|
|
107 |
prod_subscriber = {email: beta_portalSubscriber.email};
|
|
105 | 108 |
prod_db.subscriber.save(prod_subscriber); |
106 |
print("subscriber "+beta_communitySubscriber.email+" added in production DB");
|
|
109 |
print("subscriber "+beta_portalSubscriber.email+" added in production DB");
|
|
107 | 110 |
} |
108 | 111 |
} |
109 | 112 |
|
110 |
if(addInCommunity) {
|
|
111 |
var prod_communitySubscriber = prod_db.subscriber.findOne({email: beta_communitySubscriber.email});
|
|
112 |
prod_communitySubscribers.push(prod_communitySubscriber);
|
|
113 |
if(addInPortal) {
|
|
114 |
var prod_portalSubscriber = prod_db.subscriber.findOne({email: beta_portalSubscriber.email});
|
|
115 |
prod_portalSubscribers.push(prod_portalSubscriber);
|
|
113 | 116 |
} |
114 | 117 |
}) |
115 |
if(prod_communitySubscr) {
|
|
116 |
prod_db.communitySubscribers.update({ "pid" : communityPid } , {$set: {subscribers : prod_communitySubscribers}});
|
|
118 |
if(prod_portalSubscr) {
|
|
119 |
prod_db.portalSubscribers.update({ "pid" : portalPid } , {$set: {subscribers : prod_portalSubscribers}});
|
|
117 | 120 |
} else { |
118 |
prod_db.communitySubscribers.insert({ "pid" : communityPid, "subscribers" : prod_communitySubscribers });
|
|
121 |
prod_db.portalSubscribers.insert({ "pid" : portalPid, "subscribers" : prod_portalSubscribers });
|
|
119 | 122 |
} |
120 | 123 |
|
121 |
print("subscribers of community were migrated");
|
|
124 |
print("subscribers of portal were migrated");
|
|
122 | 125 |
} else { |
123 |
print("\nERROR: Subscribers not migrated. No CommunitySubscribers for this community on beta db: "+beta_db_name+"\n");
|
|
126 |
print("\nERROR: Subscribers not migrated. No PortalSubscribers for this portal on beta db: "+beta_db_name+"\n");
|
|
124 | 127 |
} |
125 | 128 |
|
126 |
// migrate divHelpContents of community
|
|
127 |
var beta_communityId = beta_community._id.str;//beta_db.community.findOne( { pid: communityPid } )._id.str;
|
|
128 |
var prod_communityId = prod_db.community.findOne( { pid: communityPid })._id.str;
|
|
129 |
// migrate divHelpContents of portal
|
|
130 |
var beta_portalId = beta_portal._id.str;//beta_db.portal.findOne( { pid: portalPid } )._id.str;
|
|
131 |
var prod_portalId = prod_db.portal.findOne( { pid: portalPid })._id.str;
|
|
129 | 132 |
|
130 |
if(prod_communityOldId) {
|
|
131 |
prod_db.divHelpContent.remove({"community": prod_communityOldId});
|
|
133 |
if(prod_portalOldId) {
|
|
134 |
prod_db.divHelpContent.remove({"portal": prod_portalOldId});
|
|
132 | 135 |
} |
133 | 136 |
//var beta_divHelpContents = |
134 |
beta_db.divHelpContent.find( { community: beta_communityId } ).forEach(function(beta_divHelpContent){//.map( function(divHelpContent) { return divHelpContent; } );
|
|
137 |
beta_db.divHelpContent.find( { portal: beta_portalId } ).forEach(function(beta_divHelpContent){//.map( function(divHelpContent) { return divHelpContent; } );
|
|
135 | 138 |
//for (var beta_divHelpContent in beta_divHelpContents) { |
136 | 139 |
var beta_beta_divHelpContent_divId_ObjectId = ObjectId(beta_divHelpContent.divId); |
137 | 140 |
var beta_divId = beta_db.divId.findOne( { _id: beta_beta_divHelpContent_divId_ObjectId } );//.name;; |
138 | 141 |
if(beta_divId) { |
139 |
var prod_divId = prod_db.divId.findOne( { name: beta_divId.name } );//._id.str; |
|
142 |
var prod_divId = prod_db.divId.findOne( { name: beta_divId.name, portalType: beta_divId.portalType } );//._id.str;
|
|
140 | 143 |
if(prod_divId) { |
141 |
prod_db.divHelpContent.save({ "divId" : prod_divId._id.str, "community" : prod_communityId, "content" : beta_divHelpContent.content, "isActive" : beta_divHelpContent.isActive });
|
|
144 |
prod_db.divHelpContent.save({ "divId" : prod_divId._id.str, "portal" : prod_portalId, "content" : beta_divHelpContent.content, "isActive" : beta_divHelpContent.isActive });
|
|
142 | 145 |
} else { |
143 | 146 |
print("\nERROR: DivId with name: "+beta_divId.name+" does not exist on production db: "+prod_db_name+"\n"); |
144 | 147 |
} |
... | ... | |
148 | 151 |
//} |
149 | 152 |
}); |
150 | 153 |
|
151 |
print("divHelpContents of community were migrated");
|
|
154 |
print("divHelpContents of portal were migrated");
|
|
152 | 155 |
|
153 |
// migrate pageHelpContents of community
|
|
154 |
if(prod_communityOldId) {
|
|
155 |
prod_db.pageHelpContent.remove({"community": prod_communityOldId});
|
|
156 |
// migrate pageHelpContents of portal
|
|
157 |
if(prod_portalOldId) {
|
|
158 |
prod_db.pageHelpContent.remove({"portal": prod_portalOldId});
|
|
156 | 159 |
} |
157 |
beta_db.pageHelpContent.find( { community: beta_communityId } ).forEach(function(beta_pageHelpContent){
|
|
160 |
beta_db.pageHelpContent.find( { portal: beta_portalId } ).forEach(function(beta_pageHelpContent){
|
|
158 | 161 |
var beta_beta_pageHelpContent_page_ObjectId = ObjectId(beta_pageHelpContent.page); |
159 | 162 |
var beta_page = beta_db.page.findOne( { _id: beta_beta_pageHelpContent_page_ObjectId } );//.route; |
160 | 163 |
if(beta_page) { |
161 |
var prod_page = prod_db.page.findOne( { route: beta_page.route } );//._id.str; |
|
164 |
var prod_page = prod_db.page.findOne( { route: beta_page.route, portalType: beta_page.portalType } );//._id.str;
|
|
162 | 165 |
if(prod_page) { |
163 |
prod_db.pageHelpContent.save({ "page" : prod_page._id.str, "community" : prod_communityId, "content" : beta_pageHelpContent.content, "placement": beta_pageHelpContent.placement, "order": beta_pageHelpContent.order, "isActive": beta_pageHelpContent.isActive, "isPriorTo": beta_pageHelpContent.isPriorTo });
|
|
166 |
prod_db.pageHelpContent.save({ "page" : prod_page._id.str, "portal" : prod_portalId, "content" : beta_pageHelpContent.content, "placement": beta_pageHelpContent.placement, "order": beta_pageHelpContent.order, "isActive": beta_pageHelpContent.isActive, "isPriorTo": beta_pageHelpContent.isPriorTo });
|
|
164 | 167 |
} else { |
165 | 168 |
print("\nERROR: Page with route: "+beta_page.route+" does not exist on production db: "+prod_db_name+"\n"); |
166 | 169 |
} |
... | ... | |
169 | 172 |
} |
170 | 173 |
}); |
171 | 174 |
|
172 |
print("pageHelpContents of community were migrated");
|
|
175 |
print("pageHelpContents of portal were migrated");
|
|
173 | 176 |
|
174 |
// migrate htmlPageContents of community |
|
175 |
if(prod_communityOldId) { |
|
176 |
prod_db.htmlPageContent.remove({"community": prod_communityOldId}); |
|
177 |
} |
|
178 |
beta_db.htmlPageContent.find( { community: beta_communityId } ).forEach(function(beta_htmlPageContent){ |
|
179 |
var beta_beta_htmlPageContent_page_ObjectId = ObjectId(beta_htmlPageContent.page); |
|
180 |
var beta_htmlPage = beta_db.page.findOne( { _id: beta_beta_htmlPageContent_page_ObjectId } );//.route; |
|
181 |
if(beta_htmlPage) { |
|
182 |
var prod_htmlPage = prod_db.page.findOne( { route: beta_htmlPage.route } );//._id.str; |
|
183 |
if(prod_htmlPage) { |
|
184 |
prod_db.htmlPageContent.save({ "page" : prod_htmlPage._id.str, "community" : prod_communityId, "content" : beta_htmlPageContent.content }); |
|
185 |
} else { |
|
186 |
print("\nERROR: Page with route: "+beta_htmlPage.route+" does not exist on production db: "+prod_db_name+"\n"); |
|
187 |
} |
|
188 |
} else { |
|
189 |
print("\nERROR: Page with id: "+beta_htmlPageContent.page+" (of htmlPageContent: "+beta_htmlPageContent._id.str+") does not exist on beta db: "+beta_db_name+"\n"); |
|
190 |
} |
|
191 |
}); |
|
192 |
|
|
193 |
print("htmlPageContents of community were migrated"); |
|
194 |
|
|
195 |
// migrate notifications preferences of community |
|
196 |
var prod_notifications = prod_db.notifications.find({ communityPid: communityPid } ); |
|
177 |
// migrate notifications preferences of portal |
|
178 |
var prod_notifications = prod_db.notifications.find({ portalPid: portalPid } ); |
|
197 | 179 |
var prod_notifications_emails = []; |
198 | 180 |
if(prod_notifications) { |
199 | 181 |
prod_notifications_emails = prod_notifications.map(function(preference){return preference.managerEmail;}) |
200 | 182 |
} |
201 | 183 |
|
202 |
beta_db.notifications.find({ communityPid: communityPid } ).forEach(function(preference){
|
|
184 |
beta_db.notifications.find({ portalPid: portalPid } ).forEach(function(preference){
|
|
203 | 185 |
if(prod_notifications_emails.indexOf(preference.managerEmail) >= 0) { |
204 |
prod_db.notifications.update({ managerEmail: preference.managerEmail }, {$set: {notifyForNewManagers: preference.notifyForNewManagers, notifyForNewSubscribers: preference.notifyForNewSubscribers}}) |
|
186 |
prod_db.notifications.update({ managerEmail: preference.managerEmail, portalPid: portalPid }, {$set: {notifyForNewManagers: preference.notifyForNewManagers, notifyForNewSubscribers: preference.notifyForNewSubscribers}})
|
|
205 | 187 |
} else { |
206 | 188 |
prod_db.notifications.insert(preference); |
207 | 189 |
} |
208 | 190 |
}); |
209 | 191 |
|
210 |
print("Notification preferences of community were migrated");
|
|
192 |
print("Notification preferences of portal were migrated");
|
|
211 | 193 |
|
212 | 194 |
} |
213 | 195 |
|
214 |
migrate_community("egi", 'openaire_admin_copy', 'openaire_admin_prod');
|
|
196 |
migrate_portal("egi", 'admin_beta_test_migration', 'admin_prod_test_migration');
|
|
215 | 197 |
|
Also available in: Unified diff
[Trunk | Admin Tools]: Merging branch 'use-UoaAdminToolsLibrary' into trunk for revisions 58365:59468