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
	prod_db = db.getSiblingDB(prod_db_name);
7

    
8
	print("migrate_portal: "+portalPid+" - both dbs are here");
9

    
10
	var beta_portal = beta_db.portal.findOne({ pid: portalPid } );
11

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

    
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
	}
22

    
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

    
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
		var beta_pageObjectId = ObjectId(beta_pageId);
33
		//var prod_pageId = prod_db.portal.find( { route: beta_db.page.find( { _id: beta_pageObjectId }).map( function(page) { return page.route; } ).toString() });
34
		var beta_page = beta_db.page.findOne( { _id: beta_pageObjectId } );
35
		if(beta_page) {
36
			var prod_page = prod_db.page.findOne( { route: beta_page.route, portalType: beta_page.portalType } );
37
			if(prod_page) {
38
				prod_portal_pages[prod_page._id.str] = beta_portal_pages[beta_pageId];
39
			} else {
40
				print("\nERROR: Page with route: "+beta_page.route+" does not exist on production db: "+prod_db_name+"\n");
41
			}
42
		} else {
43
			print("\nERROR: Page with id: "+beta_pageId+" does not exist on beta db: "+beta_db_name+"\n");
44
		}
45
	}
46
	print("Portal pages are ready to be migrated");
47

    
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
		var beta_entityObjectId = ObjectId(beta_entityId);
53
		var beta_entity = beta_db.entity.findOne( { _id: beta_entityObjectId } );
54
		if(beta_entity) {
55
			var prod_entity = prod_db.entity.findOne( { pid: beta_entity.pid } );
56
			if(prod_entity) {
57
				prod_portal_entities[prod_entity._id.str] = beta_portal_entities[beta_entityId];
58
			} else {
59
				print("\nERROR: Entity with pid: "+beta_entity.pid+" does not exist on production db: "+prod_db_name+"\n");
60
			}
61
		} else {
62
			print("\nERROR: Entity with id: "+beta_entityId+" does not exist on beta db: "+beta_db_name+"\n");
63
		}
64
	}
65

    
66
	print("Portal entities are ready to be migrated");
67

    
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
 	});
72

    
73
	print("Portal is migrated");
74

    
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 } );
80
	prod_db.statistics.save(beta_statistics);
81

    
82
	print("Statistics for portal were migrated");
83

    
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});
94
		}
95

    
96
		beta_portalSubscribers.forEach(function(beta_portalSubscriber) {
97
			var addInPortal = true;
98

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

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

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

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

    
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;
132

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

    
154
	print("divHelpContents of portal were migrated");
155

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

    
175
	print("pageHelpContents of portal were migrated");
176

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

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

    
192
	print("Notification preferences of portal were migrated");
193

    
194
}
195

    
196
migrate_portal("egi", 'admin_beta_test_migration', 'admin_prod_test_migration');
197

    
(4-4/7)