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 portal
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
  // db.auth("your_username", "your_password");
23
	prod_db = db.getSiblingDB(production_db_name);
24
  // db.auth("your_username", "your_password");
25

    
26
	print("keep_Beta_Production_DBs_synchronized - both dbs ("+beta_db_name+", "+production_db_name+") are here");
27

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

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

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

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

    
57
	print("Entities are synchronized\n");
58

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

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

    
83
			prod_db.page.save({
84
        "name": beta_page.name,
85
        "route": beta_page.route,
86
        "type": beta_page.type,
87
        "portalType": beta_page.portalType,
88
        "entities" : prod_page_entities,
89
        "top": beta_page.top, "bottom": beta_page.bottom, "left": beta_page.left, "right": beta_page.right
90
			});
91

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

    
94
			// synchronize this page and its contents for all available communities
95
			//prod_pageId = prod_db.page.find( { route: beta_page.route }).map( function(prod_page) { return prod_page._id.str; } ).toString();
96
			var prod_pageId = prod_db.page.findOne( { route: beta_page.route, portalType: beta_page.portalType })._id.str;
97
			beta_db.portal.find({type: beta_page.portalType}).forEach(function(beta_portal){
98
				var prod_portal = prod_db.portal.findOne( { pid: beta_portal.pid } );
99
				if(prod_portal) {
100
					var prod_portal_pages = prod_portal.pages;
101
					prod_portal_pages[prod_pageId] = beta_portal.pages[beta_page._id.str];
102
					prod_db.portal.update({ pid: prod_portal.pid }, {$set: {pages: prod_portal_pages}})
103
					
104
					print("Page was created for portal: "+prod_portal.name+" that exists on production");
105

    
106
					beta_db.pageHelpContent.find({portal: beta_portal._id.str, page: beta_page._id.str}).forEach(function(beta_pageHelpContent) {
107
						prod_db.pageHelpContent.save({ "page" : prod_pageId, "portal" : prod_portal._id.str, "content" : beta_pageHelpContent.content, "placement": beta_pageHelpContent.placement, "order": beta_pageHelpContent.order, "isActive": beta_pageHelpContent.isActive, "isPriorTo": beta_pageHelpContent.isPriorTo });
108
					});
109
					print("PageHelpContents were created for portal: "+prod_portal.name+" that exists on production");
110
				}
111
				print(""); 
112
			});
113
			print("");
114
		}
115
	});
116

    
117
	print("Pages are synchronized\n");
118

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

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

    
161
	print("DivIds are synchronized");
162
	
163
}
164

    
165
keep_Beta_Production_DBs_synchronized("openaireconnect_new", "openaireconnect");
166

    
(6-6/7)