Project

General

Profile

1
/*Authentication???
2

    
3
// 1. copy database with name "test" in folder "/home/konstantina/mongo_backups"
4
mongodump --db test --out /home/konstantina/mongo_backups
5

    
6
// 2. paste database with name "test", which is in folder "/home/konstantina/mongo_backups/test", into database with name "test2" | Drop collections if test2 already exists
7
mongorestore --db test2 --drop /home/konstantina/mongo_backups/test
8

    
9
// Be careful with function parameters: names of dbs, name of community
10
// 3. If needed run init script
11

    
12
// 4. Run synchronize_dbs.js
13

    
14
// 5. Run migrateCommunityIn_db.js
15
*/
16

    
17
//version compatibility: 1.1.1-SNAPSHOT
18
print("synchronization of DBs script running...");
19

    
20
function keep_Beta_Production_DBs_synchronized(beta_db_name, production_db_name) {
21
	beta_db = db.getSiblingDB(beta_db_name);
22
	prod_db = db.getSiblingDB(production_db_name);
23

    
24
	print("keep_Beta_Production_DBs_synchronized - both dbs ("+beta_db_name+", "+production_db_name+") are here");
25

    
26
	// synchronize entities
27
	beta_db.entity.find().forEach(function(beta_entity){
28
		//var prod_entityId = prod_db.entity.find( { name: beta_entity.name }).map( function(prod_entity) { return prod_entity._id.str; } ).toString();
29
		var prod_entity = prod_db.entity.findOne( { pid: beta_entity.pid });
30
		if(!prod_entity) {
31
			prod_db.entity.save({"pid": beta_entity.pid, "name": beta_entity.name});
32

    
33
			print("Entity with pid: "+beta_entity.pid+" is created in "+production_db_name+" database");
34

    
35
			// synchronize this entity for all available communities
36
			//prod_entityId = prod_db.entity.find( { pid: beta_entity.pid }).map( function(prod_entity) { return prod_entity._id.str; } ).toString();
37
			var prod_entity_id = prod_db.entity.findOne( { pid: beta_entity.pid })._id.str;
38

    
39
			beta_db.community.find().forEach(function(beta_community){
40
				var prod_community = prod_db.community.findOne( { pid: beta_community.pid } );
41
				if(prod_community) {
42
					var prod_community_entities = prod_community.entities;
43
					prod_community_entities[prod_entity_id] = beta_community.entities[beta_entity._id.str];
44
					prod_db.community.update({ pid: prod_community.pid }, {$set: {entities: prod_community_entities}})
45
				
46
					print("Entity was created for community with pid: "+prod_community.pid+" that exists on production db: "+production_db_name);
47
				} else {
48
					print("\nERROR: Community with pid: "+ beta_community.pid+" does not exist on production db: "+production_db_name+"\n");
49
				} 
50
			});
51
			print("");
52
		}
53
	});
54

    
55
	print("Entities are synchronized\n");
56

    
57
	// synchronize pages
58
	beta_db.page.find().forEach(function(beta_page){
59
		//var prod_pageId = prod_db.page.find( { route: beta_page.route }).map( function(prod_page) { return prod_page._id.str; } ).toString();
60
		var prod_page = prod_db.page.findOne( { route: beta_page.route });//._id.str;
61
		if(!prod_page) {
62

    
63
			var prod_page_entities = [];
64
			beta_page.entities.forEach(function(beta_page_entity) {
65
				var beta_page_entity_ObjectId = ObjectId(beta_page_entity);
66
				//var beta_pageEntity_pid = beta_db.entity.find( { _id: beta_page_entity_ObjectId } ).map(function(entity) { return entity.pid; }).toString();
67
				var beta_pageEntity = beta_db.entity.findOne( { _id: beta_page_entity_ObjectId });
68
				if(beta_pageEntity) {
69
					//var prod_pageEntity_id = prod_db.entity.find( { pid: beta_pageEntity_pid } ).map(function(entity) { return entity._id.str; }).toString();
70
					var prod_pageEntity = prod_db.entity.findOne( { pid: beta_pageEntity.pid });//._id.str;
71
					if(prod_pageEntity) {
72
						prod_page_entities.push(prod_pageEntity._id.str);
73
					} else {
74
						print("\nERROR: Entity with pid: "+beta_pageEntity.pid+" does not exist on production db: "+production_db_name+"\n");
75
					}
76
				} else {
77
					print("\nERROR: Entity with id: "+beta_page_entity+" (of page: "+beta_page.name+") does not exist on beta db: "+beta_db_name+"\n");
78
				}
79
			})
80

    
81
			prod_db.page.save({"name": beta_page.name, "route": beta_page.route, "type": beta_page.type, "connect": beta_page.connect, "openaire": beta_page.openaire, "entities" : prod_page_entities});
82

    
83
			print("Page: "+beta_page.name+" is created in "+production_db_name+" database");
84

    
85
			// synchronize this page and its contents for all available communities
86
			//prod_pageId = prod_db.page.find( { route: beta_page.route }).map( function(prod_page) { return prod_page._id.str; } ).toString();
87
			var prod_pageId = prod_db.page.findOne( { route: beta_page.route })._id.str;
88
			beta_db.community.find().forEach(function(beta_community){
89
				var prod_community = prod_db.community.findOne( { pid: beta_community.pid } );
90
				if(prod_community) {
91
					var prod_community_pages = prod_community.pages;
92
					prod_community_pages[prod_pageId] = beta_community.pages[beta_page._id.str];
93
					prod_db.community.update({ pid: prod_community.pid }, {$set: {pages: prod_community_pages}})
94
					
95
					print("Page was created for community: "+prod_community.name+" that exists on production");
96

    
97
					beta_db.pageHelpContent.find({community: beta_community._id.str, page: beta_page._id.str}).forEach(function(beta_pageHelpContent) {
98
						prod_db.pageHelpContent.save({ "page" : prod_pageId, "community" : prod_community._id.str, "content" : beta_pageHelpContent.content, "placement": beta_pageHelpContent.placement, "order": beta_pageHelpContent.order, "isActive": beta_pageHelpContent.isActive, "isPriorTo": beta_pageHelpContent.isPriorTo });
99
					});
100
					print("PageHelpContents were created for community: "+prod_community.name+" that exists on production");
101

    
102
					beta_db.htmlPageContent.find({community: beta_community._id.str, page: beta_page._id.str}).forEach(function(beta_htmlPageContent) {
103
						prod_db.htmlPageContent.save({ "page" : prod_pageId, "community" : prod_community._id.str, "content" : beta_htmlPageContent.content });
104
					});
105
					print("HtmlPageContents were created for community: "+prod_community.name+" that exists on production");
106
				}
107
				print(""); 
108
			});
109
			print("");
110
		}
111
	});
112

    
113
	print("Pages are synchronized\n");
114

    
115
	// synchronize divIds
116
	beta_db.divId.find().forEach(function(beta_divId){
117
		//var prod_divIdId = prod_db.divId.find( { name: beta_divId.name }).map( function(prod_divId) { return prod_divId._id.str; } ).toString();
118
		var prod_divId = prod_db.divId.findOne( { name: beta_divId.name });//._id.str;
119
		if(!prod_divId) {
120
			var prod_divId_pages = [];
121
			beta_divId.pages.forEach(function(beta_divId_page) {
122
				var beta_divId_page_ObjectId = ObjectId(beta_divId_page);
123
				//var beta_divIdPage_route = beta_db.page.find( { _id: beta_divId_page_ObjectId } ).map(function(page) { return page.route; }).toString();
124
				var beta_divIdPage = beta_db.page.findOne( { _id: beta_divId_page_ObjectId });//.route;
125
				if(beta_divIdPage) {
126
					//var prod_divIdPage_id = prod_db.page.find( { route: beta_divIdPage_route } ).map(function(page) { return page._id.str; }).toString();
127
					var prod_divIdPage = prod_db.page.findOne( { route: beta_divIdPage.route });//._id.str;
128
					if(prod_divIdPage) {
129
						prod_divId_pages.push(prod_divIdPage._id.str);
130
					} else {
131
						print("\nERROR: Page with route: "+beta_divIdPage.route+" does not exist on production db: "+production_db_name+"\n");
132
					}
133
				} else {
134
					print("\nERROR: Page with id: "+beta_divId_page+" (of divId: "+beta_divId.name+") does not exist on beta db: "+beta_db_name+"\n");
135
				}
136
			})
137
			prod_db.divId.save({"name": beta_divId.name, "pages" : prod_divId_pages});
138

    
139
			print("DivId: "+beta_divId.name+" is created in "+production_db_name+" database");
140
			
141
			// synchronize this divId's contents for all available communities
142
			//prod_divIdId = prod_db.divId.find( { name: beta_divId.name }).map( function(prod_divId) { return prod_divId._id.str; } ).toString();
143
			var prod_divIdId = prod_db.divId.findOne( { name: beta_divId.name })._id.str;
144
			beta_db.community.find().forEach(function(beta_community){
145
				var prod_community = prod_db.community.findOne( { pid: beta_community.pid } );
146
				if(prod_community) {
147
					beta_db.divHelpContent.find({community: beta_community._id.str, divId: beta_divId._id.str}).forEach(function(beta_divHelpContent) {
148
						prod_db.divHelpContent.save({ "divId" : prod_divIdId, "community" : prod_community._id.str, "content" : beta_divHelpContent.content, "isActive" : beta_divHelpContent.isActive });
149
					});
150
					print("DivHelpContents were created for community: "+prod_community.name+" that exists on production");
151
				}
152
			});
153
			print("");
154
		}
155
	});
156

    
157
	print("DivIds are synchronized");
158
	
159
}
160

    
161
keep_Beta_Production_DBs_synchronized("openaire_admin_copy", "openaire_admin_prod");
162

    
(6-6/7)