Project

General

Profile

1
//version compatibility: 1.1.1-SNAPSHOT
2
print("migrateCommunityIn_db script running...");
3

    
4
function migrate_portal(portalPid, beta_db_name, prod_db_name) {
5
	beta_db = db.getSiblingDB(beta_db_name);
6
  // db.auth("your_username", "your_password");
7
	prod_db = db.getSiblingDB(prod_db_name);
8
  // db.auth("your_username", "your_password");
9

    
10
	print("migrate_portal: "+portalPid+" - both dbs are here");
11

    
12
	var beta_portal = beta_db.portal.findOne({ pid: portalPid } );
13

    
14
	if(!beta_portal) {
15
		print("ERROR: Portal with pid: "+portalPid+" does not exist in (beta) database: "+ beta_db_name);
16
		return;
17
	}
18

    
19
	var prod_portalOld = prod_db.portal.findOne( { pid: portalPid } );
20
	var prod_portalOldId;
21
	if(prod_portalOld) {
22
		prod_portalOldId = prod_portalOld._id.str;
23
	}
24

    
25
	// delete portal from production db
26
	prod_db.portal.remove({"pid" : portalPid});
27
	print("Portal with pid: "+portalPid+" is deleted (if existed) from production db: "+prod_db_name);
28

    
29
	// migrate portal pages
30
	//beta_portal_pages = beta_db.portal.findOne({ pid: portalPid } ).map( function(portal) { return portal.pages } );
31
	var beta_portal_pages = beta_portal.pages;
32
	var prod_portal_pages = {}
33
	for (var beta_pageId in beta_portal_pages) {
34
		var beta_pageObjectId = ObjectId(beta_pageId);
35
		//var prod_pageId = prod_db.portal.find( { route: beta_db.page.find( { _id: beta_pageObjectId }).map( function(page) { return page.route; } ).toString() });
36
		var beta_page = beta_db.page.findOne( { _id: beta_pageObjectId } );
37
		if(beta_page) {
38
			var prod_page = prod_db.page.findOne( { route: beta_page.route, portalType: beta_page.portalType } );
39
			if(prod_page) {
40
				prod_portal_pages[prod_page._id.str] = beta_portal_pages[beta_pageId];
41
			} else {
42
				print("\nERROR: Page with route: "+beta_page.route+" does not exist on production db: "+prod_db_name+"\n");
43
			}
44
		} else {
45
			print("\nERROR: Page with id: "+beta_pageId+" does not exist on beta db: "+beta_db_name+"\n");
46
		}
47
	}
48
	print("Portal pages are ready to be migrated");
49

    
50
	// migrate portal entities
51
	var beta_portal_entities = beta_portal.entities;
52
	var prod_portal_entities = {}
53
	for (var beta_entityId in beta_portal_entities) {
54
		var beta_entityObjectId = ObjectId(beta_entityId);
55
		var beta_entity = beta_db.entity.findOne( { _id: beta_entityObjectId } );
56
		if(beta_entity) {
57
			var prod_entity = prod_db.entity.findOne( { pid: beta_entity.pid } );
58
			if(prod_entity) {
59
				prod_portal_entities[prod_entity._id.str] = beta_portal_entities[beta_entityId];
60
			} else {
61
				print("\nERROR: Entity with pid: "+beta_entity.pid+" does not exist on production db: "+prod_db_name+"\n");
62
			}
63
		} else {
64
			print("\nERROR: Entity with id: "+beta_entityId+" does not exist on beta db: "+beta_db_name+"\n");
65
		}
66
	}
67

    
68
	print("Portal entities are ready to be migrated");
69

    
70
 	prod_db.portal.save({
71
    "name" : beta_portal.name, "pid" : portalPid, "type": beta_portal.type,
72
    "pages" : prod_portal_pages, "entities" : prod_portal_entities
73
 	});
74

    
75
	print("Portal is migrated");
76

    
77
	// delete portal statistics from production db
78
	prod_db.statistics.remove({"pid" : portalPid});
79
	print("Statistics for portal with pid: "+portalPid+" are deleted (if existed) from production db: "+prod_db_name);
80
	// migrate statistics for portal
81
	var beta_statistics = beta_db.statistics.findOne({ pid: portalPid } );
82
	prod_db.statistics.save(beta_statistics);
83

    
84
	print("Statistics for portal were migrated");
85

    
86
	// migrate subscribers of portal
87
	var beta_portalSubscr = beta_db.portalSubscribers.findOne({ pid: portalPid } );
88
	if(beta_portalSubscr) {
89
		var beta_portalSubscribers = beta_portalSubscr.subscribers;
90
		var prod_portalSubscribers = [];
91
		var prod_portalSubscribersEmails = [];
92
		var prod_portalSubscr = prod_db.portalSubscribers.findOne({ "pid" : portalPid });
93
		if(prod_portalSubscr) {
94
			prod_portalSubscribers = prod_portalSubscr.subscribers;
95
			prod_portalSubscribersEmails = prod_portalSubscr.subscribers.map(function(subscriber){return subscriber.email});
96
		}
97

    
98
		beta_portalSubscribers.forEach(function(beta_portalSubscriber) {
99
			var addInPortal = true;
100

    
101
			// if user exists in portal subscribers already, do nothing
102
			if(prod_portalSubscribersEmails.indexOf(beta_portalSubscriber.email) >= 0) {
103
				addInPortal = false;
104
			} else {
105
				var prod_subscriber = prod_db.subscriber.findOne({email: beta_portalSubscriber.email});
106

    
107
				// if subscriber of beta does not exist in production, add him
108
				if(!prod_subscriber) {
109
					prod_subscriber = {email: beta_portalSubscriber.email};
110
					prod_db.subscriber.save(prod_subscriber);
111
					print("subscriber "+beta_portalSubscriber.email+" added in production DB");
112
				}
113
			}
114

    
115
			if(addInPortal) {
116
				var prod_portalSubscriber = prod_db.subscriber.findOne({email: beta_portalSubscriber.email});
117
				prod_portalSubscribers.push(prod_portalSubscriber);
118
			}
119
		})
120
		if(prod_portalSubscr) {
121
			prod_db.portalSubscribers.update({ "pid" : portalPid } , {$set: {subscribers : prod_portalSubscribers}});
122
		} else {
123
			prod_db.portalSubscribers.insert({ "pid" : portalPid, "subscribers" : prod_portalSubscribers });
124
		}
125

    
126
		print("subscribers of portal were migrated");
127
	} else {
128
		print("\nERROR: Subscribers not migrated. No PortalSubscribers for this portal on beta db: "+beta_db_name+"\n");
129
	}
130

    
131
	// migrate divHelpContents of portal
132
	var beta_portalId = beta_portal._id.str;//beta_db.portal.findOne( { pid: portalPid } )._id.str;
133
	var prod_portalId = prod_db.portal.findOne( { pid: portalPid })._id.str;
134

    
135
	if(prod_portalOldId) {
136
		prod_db.divHelpContent.remove({"portal": prod_portalOldId});
137
	}
138
	//var beta_divHelpContents = 
139
	beta_db.divHelpContent.find( { portal: beta_portalId } ).forEach(function(beta_divHelpContent){//.map( function(divHelpContent) { return divHelpContent; } );
140
	//for (var beta_divHelpContent in beta_divHelpContents) {
141
		var beta_beta_divHelpContent_divId_ObjectId = ObjectId(beta_divHelpContent.divId);
142
		var beta_divId = beta_db.divId.findOne( { _id: beta_beta_divHelpContent_divId_ObjectId } );//.name;;
143
		if(beta_divId) {
144
			var prod_divId = prod_db.divId.findOne( { name: beta_divId.name, portalType: beta_divId.portalType } );//._id.str;
145
			if(prod_divId) {
146
				prod_db.divHelpContent.save({ "divId" : prod_divId._id.str, "portal" : prod_portalId, "content" : beta_divHelpContent.content, "isActive" : beta_divHelpContent.isActive });
147
			} else {
148
				print("\nERROR: DivId with name: "+beta_divId.name+" does not exist on production db: "+prod_db_name+"\n");
149
			}
150
		} else {
151
			print("\nERROR: DivId with id: "+beta_divHelpContent.divId+" (of divHelpContent: "+beta_divHelpContent._id.str+") does not exist on beta db: "+beta_db_name+"\n");
152
		}
153
	//}
154
	});
155

    
156
	print("divHelpContents of portal were migrated");
157

    
158
	// migrate pageHelpContents of portal
159
	if(prod_portalOldId) {
160
		prod_db.pageHelpContent.remove({"portal": prod_portalOldId});
161
	}
162
	beta_db.pageHelpContent.find( { portal: beta_portalId } ).forEach(function(beta_pageHelpContent){
163
		var beta_beta_pageHelpContent_page_ObjectId = ObjectId(beta_pageHelpContent.page);
164
		var beta_page = beta_db.page.findOne( { _id: beta_beta_pageHelpContent_page_ObjectId } );//.route;
165
		if(beta_page) {
166
			var prod_page = prod_db.page.findOne( { route: beta_page.route, portalType: beta_page.portalType } );//._id.str;
167
			if(prod_page) {
168
				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 });
169
			} else {
170
				print("\nERROR: Page with route: "+beta_page.route+" does not exist on production db: "+prod_db_name+"\n");
171
			}
172
		} else {
173
			print("\nERROR: Page with id: "+beta_pageHelpContent.page+" (of pageHelpContent: "+beta_pageHelpContent._id.str+") does not exist on beta db: "+beta_db_name+"\n");
174
		}
175
	});
176

    
177
	print("pageHelpContents of portal were migrated");
178

    
179
	// migrate notifications preferences of portal
180
	var prod_notifications = prod_db.notifications.find({ portalPid: portalPid } );
181
	var prod_notifications_emails = [];
182
	if(prod_notifications) {
183
		prod_notifications_emails = prod_notifications.map(function(preference){return preference.managerEmail;})
184
	}
185

    
186
	beta_db.notifications.find({ portalPid: portalPid } ).forEach(function(preference){
187
		if(prod_notifications_emails.indexOf(preference.managerEmail) >= 0) {
188
			prod_db.notifications.update({ managerEmail: preference.managerEmail, portalPid: portalPid }, {$set: {notifyForNewManagers: preference.notifyForNewManagers, notifyForNewSubscribers: preference.notifyForNewSubscribers}})
189
		} else {
190
			prod_db.notifications.insert(preference);
191
		}
192
	});
193

    
194
	print("Notification preferences of portal were migrated");
195

    
196
}
197

    
198
function migrateCurators(beta_db_name, prod_db_name) {
199
  beta_db = db.getSiblingDB(beta_db_name);
200
  // beta_db.auth("your_username", "your_password");
201

    
202
  prod_db = db.getSiblingDB(prod_db_name);
203
  // prod_db.auth("your_username", "your_password");
204

    
205
  print("migrateCurators: both dbs are here");
206

    
207
  // migrate curators of portal
208
  var beta_curators = beta_db.curator.find().forEach(function(beta_curator) {
209
    if (beta_curator) {
210
      prod_db.curator.remove({"email": beta_curator.email});
211
      curator = {};
212
      curator['_id'] = beta_curator._id;  // this is aaiId
213

    
214
      if(beta_curator.email) {
215
        curator['email'] = beta_curator.email;
216
      } else {
217
        curator['email'] = "";
218
      }
219

    
220
      if(beta_curator.name) {
221
        curator['name'] = beta_curator.name;
222
      } else {
223
        curator['name'] = "";
224
      }
225

    
226
      if(beta_curator.affiliations) {
227
        curator['affiliations'] = beta_curator.affiliations;
228
      } else {
229
        curator['affiliations'] = [];
230
      }
231

    
232
      if(beta_curator.photo) {
233
        curator['photo'] = beta_curator.photo;
234
      } else {
235
        curator['photo'] = "";
236
      }
237

    
238
      if(beta_curator.bio) {
239
        curator['bio'] = beta_curator.bio;
240
      } else {
241
        curator['bio'] = "";
242
      }
243
      // prod_db.curator.insert({"email": beta_curator.email,"name": beta_curator.name, "affiliations": beta_curator.affiliations, "photo": beta_curator.photo, "bio": beta_curator.bio});
244
      prod_db.curator.insert(curator);
245

    
246
      print("curator "+beta_curator.name + " is migrated");
247
    }
248
  });
249
}
250

    
251
// migrate_portal("aginfra", 'openaireconnect_new', 'openaireconnect');
252
// migrate_portal("rural-digital-europe", 'openaireconnect_new', 'openaireconnect');
253
// migrate_portal("clarin", 'openaireconnect_new', 'openaireconnect');
254
// migrate_portal("enermaps", 'openaireconnect_new', 'openaireconnect');
255
// // migrate_portal("ifremer", 'openaireconnect_new', 'openaireconnect');
256
// migrate_portal("instruct", 'openaireconnect_new', 'openaireconnect');
257
// migrate_portal("risis", 'openaireconnect_new', 'openaireconnect');
258
// migrate_portal("rda", 'openaireconnect_new', 'openaireconnect');
259
// migrate_portal("science-innovation-policy", 'openaireconnect_new', 'openaireconnect');
260
// // migrate_portal("sobigdata", 'openaireconnect_new', 'openaireconnect');
261
// migrate_portal("sdsn-gr", 'openaireconnect_new', 'openaireconnect');
262
// migrate_portal("elixir-gr", 'openaireconnect_new', 'openaireconnect');
263
// migrate_portal("beopen", 'openaireconnect_new', 'openaireconnect');
264
// // migrate_portal("euromarine", 'openaireconnect_new', 'openaireconnect');
265
// migrate_portal("oa-pg", 'openaireconnect_new', 'openaireconnect');
266
//
267
//
268
// migrate_portal("covid-19", 'openaireconnect_new', 'openaireconnect');
269
// migrate_portal("dariah", 'openaireconnect_new', 'openaireconnect');
270
// migrate_portal("dh-ch", 'openaireconnect_new', 'openaireconnect');
271
// migrate_portal("egi", 'openaireconnect_new', 'openaireconnect');
272
// migrate_portal("mes", 'openaireconnect_new', 'openaireconnect');
273
// migrate_portal("epos", 'openaireconnect_new', 'openaireconnect');
274
// migrate_portal("fam", 'openaireconnect_new', 'openaireconnect');
275
// migrate_portal("ni", 'openaireconnect_new', 'openaireconnect');
276
//
277

    
278
migrateCurators("openaireconnect_new", "openaireconnect");
(4-4/7)