Project

General

Profile

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

    
4
function migrate_community(communityPid, 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_community: "+communityPid+" - both dbs are here");
9

    
10
	var beta_community = beta_db.community.findOne({ pid: communityPid } );
11

    
12
	if(!beta_community) {
13
		print("ERROR: Community with pid: "+communityPid+" does not exist in (beta) database: "+ beta_db_name);
14
		return;
15
	}
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;
21
	}
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);
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) {
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() });
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 } );
37
			if(prod_page) {
38
				prod_community_pages[prod_page._id.str] = beta_community_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("Community pages are ready to be migrated");
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) {
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_community_entities[prod_entity._id.str] = beta_community_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("Community entities are ready to be migrated");
67

    
68
 	prod_db.community.save({"name" : beta_community.name, "pid" : communityPid, "pages" : prod_community_pages, "entities" : prod_community_entities});
69

    
70
	print("Community is migrated");
71

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

    
79
	print("Statistics for community were migrated");
80

    
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});
91
		}
92

    
93
		beta_communitySubscribers.forEach(function(beta_communitySubscriber) {
94
			var addInCommunity = true;
95

    
96
			// if user exists in community subscribers already, do nothing
97
			if(prod_communitySubscribersEmails.indexOf(beta_communitySubscriber.email) >= 0) {
98
				addInCommunity = false;
99
			} else {
100
				var prod_subscriber = prod_db.subscriber.findOne({email: beta_communitySubscriber.email});
101

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

    
110
			if(addInCommunity) {
111
				var prod_communitySubscriber = prod_db.subscriber.findOne({email: beta_communitySubscriber.email});
112
				prod_communitySubscribers.push(prod_communitySubscriber);
113
			}
114
		})
115
		if(prod_communitySubscr) {
116
			prod_db.communitySubscribers.update({ "pid" : communityPid } , {$set: {subscribers : prod_communitySubscribers}});
117
		} else {
118
			prod_db.communitySubscribers.insert({ "pid" : communityPid, "subscribers" : prod_communitySubscribers });
119
		}
120

    
121
		print("subscribers of community were migrated");
122
	} else {
123
		print("\nERROR: Subscribers not migrated. No CommunitySubscribers for this community on beta db: "+beta_db_name+"\n");
124
	}
125

    
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

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

    
151
	print("divHelpContents of community were migrated");
152

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

    
172
	print("pageHelpContents of community were migrated");
173

    
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 } );
197
	var prod_notifications_emails = [];
198
	if(prod_notifications) {
199
		prod_notifications_emails = prod_notifications.map(function(preference){return preference.managerEmail;})
200
	}
201

    
202
	beta_db.notifications.find({ communityPid: communityPid } ).forEach(function(preference){
203
		if(prod_notifications_emails.indexOf(preference.managerEmail) >= 0) {
204
			prod_db.notifications.update({ managerEmail: preference.managerEmail }, {$set: {notifyForNewManagers: preference.notifyForNewManagers, notifyForNewSubscribers: preference.notifyForNewSubscribers}})
205
		} else {
206
			prod_db.notifications.insert(preference);
207
		}
208
	});
209

    
210
	print("Notification preferences of community were migrated");
211

    
212
}
213

    
214
migrate_community("egi", 'openaire_admin_copy', 'openaire_admin_prod');
215

    
(3-3/6)