216 lines
9.8 KiB
JavaScript
216 lines
9.8 KiB
JavaScript
|
//version compatibility: 1.1.1-SNAPSHOT
|
||
|
print("migrateCommunityIn_db script running...");
|
||
|
|
||
|
function migrate_community(communityPid, beta_db_name, prod_db_name) {
|
||
|
beta_db = db.getSiblingDB(beta_db_name);
|
||
|
prod_db = db.getSiblingDB(prod_db_name);
|
||
|
|
||
|
print("migrate_community: "+communityPid+" - both dbs are here");
|
||
|
|
||
|
var beta_community = beta_db.community.findOne({ pid: communityPid } );
|
||
|
|
||
|
if(!beta_community) {
|
||
|
print("ERROR: Community with pid: "+communityPid+" does not exist in (beta) database: "+ beta_db_name);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var prod_communityOld = prod_db.community.findOne( { pid: communityPid } );
|
||
|
var prod_communityOldId;
|
||
|
if(prod_communityOld) {
|
||
|
prod_communityOldId = prod_communityOld._id.str;
|
||
|
}
|
||
|
|
||
|
// delete community from production db
|
||
|
prod_db.community.remove({"pid" : communityPid});
|
||
|
print("Community with pid: "+communityPid+" is deleted (if existed) from production db: "+prod_db_name);
|
||
|
|
||
|
// migrate community pages
|
||
|
//beta_community_pages = beta_db.community.findOne({ pid: communityPid } ).map( function(community) { return community.pages } );
|
||
|
var beta_community_pages = beta_community.pages;
|
||
|
var prod_community_pages = {}
|
||
|
for (var beta_pageId in beta_community_pages) {
|
||
|
var beta_pageObjectId = ObjectId(beta_pageId);
|
||
|
//var prod_pageId = prod_db.community.find( { route: beta_db.page.find( { _id: beta_pageObjectId }).map( function(page) { return page.route; } ).toString() });
|
||
|
var beta_page = beta_db.page.findOne( { _id: beta_pageObjectId } );
|
||
|
if(beta_page) {
|
||
|
var prod_page = prod_db.page.findOne( { route: beta_page.route } );
|
||
|
if(prod_page) {
|
||
|
prod_community_pages[prod_page._id.str] = beta_community_pages[beta_pageId];
|
||
|
} else {
|
||
|
print("\nERROR: Page with route: "+beta_page.route+" does not exist on production db: "+prod_db_name+"\n");
|
||
|
}
|
||
|
} else {
|
||
|
print("\nERROR: Page with id: "+beta_pageId+" does not exist on beta db: "+beta_db_name+"\n");
|
||
|
}
|
||
|
}
|
||
|
print("Community pages are ready to be migrated");
|
||
|
|
||
|
// migrate community entities
|
||
|
var beta_community_entities = beta_community.entities;
|
||
|
var prod_community_entities = {}
|
||
|
for (var beta_entityId in beta_community_entities) {
|
||
|
var beta_entityObjectId = ObjectId(beta_entityId);
|
||
|
var beta_entity = beta_db.entity.findOne( { _id: beta_entityObjectId } );
|
||
|
if(beta_entity) {
|
||
|
var prod_entity = prod_db.entity.findOne( { pid: beta_entity.pid } );
|
||
|
if(prod_entity) {
|
||
|
prod_community_entities[prod_entity._id.str] = beta_community_entities[beta_entityId];
|
||
|
} else {
|
||
|
print("\nERROR: Entity with pid: "+beta_entity.pid+" does not exist on production db: "+prod_db_name+"\n");
|
||
|
}
|
||
|
} else {
|
||
|
print("\nERROR: Entity with id: "+beta_entityId+" does not exist on beta db: "+beta_db_name+"\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
print("Community entities are ready to be migrated");
|
||
|
|
||
|
prod_db.community.save({"name" : beta_community.name, "pid" : communityPid, "pages" : prod_community_pages, "entities" : prod_community_entities});
|
||
|
|
||
|
print("Community is migrated");
|
||
|
|
||
|
// delete community statistics from production db
|
||
|
prod_db.statistics.remove({"pid" : communityPid});
|
||
|
print("Statistics for community with pid: "+communityPid+" are deleted (if existed) from production db: "+prod_db_name);
|
||
|
// migrate statistics for community
|
||
|
var beta_statistics = beta_db.statistics.findOne({ pid: communityPid } );
|
||
|
prod_db.statistics.save(beta_statistics);
|
||
|
|
||
|
print("Statistics for community were migrated");
|
||
|
|
||
|
// migrate subscribers of community
|
||
|
var beta_communitySubscr = beta_db.communitySubscribers.findOne({ pid: communityPid } );
|
||
|
if(beta_communitySubscr) {
|
||
|
var beta_communitySubscribers = beta_communitySubscr.subscribers;
|
||
|
var prod_communitySubscribers = [];
|
||
|
var prod_communitySubscribersEmails = [];
|
||
|
var prod_communitySubscr = prod_db.communitySubscribers.findOne({ "pid" : communityPid });
|
||
|
if(prod_communitySubscr) {
|
||
|
prod_communitySubscribers = prod_communitySubscr.subscribers;
|
||
|
prod_communitySubscribersEmails = prod_communitySubscr.subscribers.map(function(subscriber){return subscriber.email});
|
||
|
}
|
||
|
|
||
|
beta_communitySubscribers.forEach(function(beta_communitySubscriber) {
|
||
|
var addInCommunity = true;
|
||
|
|
||
|
// if user exists in community subscribers already, do nothing
|
||
|
if(prod_communitySubscribersEmails.indexOf(beta_communitySubscriber.email) >= 0) {
|
||
|
addInCommunity = false;
|
||
|
} else {
|
||
|
var prod_subscriber = prod_db.subscriber.findOne({email: beta_communitySubscriber.email});
|
||
|
|
||
|
// if subscriber of beta does not exist in production, add him
|
||
|
if(!prod_subscriber) {
|
||
|
prod_subscriber = {email: beta_communitySubscriber.email};
|
||
|
prod_db.subscriber.save(prod_subscriber);
|
||
|
print("subscriber "+beta_communitySubscriber.email+" added in production DB");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(addInCommunity) {
|
||
|
var prod_communitySubscriber = prod_db.subscriber.findOne({email: beta_communitySubscriber.email});
|
||
|
prod_communitySubscribers.push(prod_communitySubscriber);
|
||
|
}
|
||
|
})
|
||
|
if(prod_communitySubscr) {
|
||
|
prod_db.communitySubscribers.update({ "pid" : communityPid } , {$set: {subscribers : prod_communitySubscribers}});
|
||
|
} else {
|
||
|
prod_db.communitySubscribers.insert({ "pid" : communityPid, "subscribers" : prod_communitySubscribers });
|
||
|
}
|
||
|
|
||
|
print("subscribers of community were migrated");
|
||
|
} else {
|
||
|
print("\nERROR: Subscribers not migrated. No CommunitySubscribers for this community on beta db: "+beta_db_name+"\n");
|
||
|
}
|
||
|
|
||
|
// migrate divHelpContents of community
|
||
|
var beta_communityId = beta_community._id.str;//beta_db.community.findOne( { pid: communityPid } )._id.str;
|
||
|
var prod_communityId = prod_db.community.findOne( { pid: communityPid })._id.str;
|
||
|
|
||
|
if(prod_communityOldId) {
|
||
|
prod_db.divHelpContent.remove({"community": prod_communityOldId});
|
||
|
}
|
||
|
//var beta_divHelpContents =
|
||
|
beta_db.divHelpContent.find( { community: beta_communityId } ).forEach(function(beta_divHelpContent){//.map( function(divHelpContent) { return divHelpContent; } );
|
||
|
//for (var beta_divHelpContent in beta_divHelpContents) {
|
||
|
var beta_beta_divHelpContent_divId_ObjectId = ObjectId(beta_divHelpContent.divId);
|
||
|
var beta_divId = beta_db.divId.findOne( { _id: beta_beta_divHelpContent_divId_ObjectId } );//.name;;
|
||
|
if(beta_divId) {
|
||
|
var prod_divId = prod_db.divId.findOne( { name: beta_divId.name } );//._id.str;
|
||
|
if(prod_divId) {
|
||
|
prod_db.divHelpContent.save({ "divId" : prod_divId._id.str, "community" : prod_communityId, "content" : beta_divHelpContent.content, "isActive" : beta_divHelpContent.isActive });
|
||
|
} else {
|
||
|
print("\nERROR: DivId with name: "+beta_divId.name+" does not exist on production db: "+prod_db_name+"\n");
|
||
|
}
|
||
|
} else {
|
||
|
print("\nERROR: DivId with id: "+beta_divHelpContent.divId+" (of divHelpContent: "+beta_divHelpContent._id.str+") does not exist on beta db: "+beta_db_name+"\n");
|
||
|
}
|
||
|
//}
|
||
|
});
|
||
|
|
||
|
print("divHelpContents of community were migrated");
|
||
|
|
||
|
// migrate pageHelpContents of community
|
||
|
if(prod_communityOldId) {
|
||
|
prod_db.pageHelpContent.remove({"community": prod_communityOldId});
|
||
|
}
|
||
|
beta_db.pageHelpContent.find( { community: beta_communityId } ).forEach(function(beta_pageHelpContent){
|
||
|
var beta_beta_pageHelpContent_page_ObjectId = ObjectId(beta_pageHelpContent.page);
|
||
|
var beta_page = beta_db.page.findOne( { _id: beta_beta_pageHelpContent_page_ObjectId } );//.route;
|
||
|
if(beta_page) {
|
||
|
var prod_page = prod_db.page.findOne( { route: beta_page.route } );//._id.str;
|
||
|
if(prod_page) {
|
||
|
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 });
|
||
|
} else {
|
||
|
print("\nERROR: Page with route: "+beta_page.route+" does not exist on production db: "+prod_db_name+"\n");
|
||
|
}
|
||
|
} else {
|
||
|
print("\nERROR: Page with id: "+beta_pageHelpContent.page+" (of pageHelpContent: "+beta_pageHelpContent._id.str+") does not exist on beta db: "+beta_db_name+"\n");
|
||
|
}
|
||
|
});
|
||
|
|
||
|
print("pageHelpContents of community were migrated");
|
||
|
|
||
|
// migrate htmlPageContents of community
|
||
|
if(prod_communityOldId) {
|
||
|
prod_db.htmlPageContent.remove({"community": prod_communityOldId});
|
||
|
}
|
||
|
beta_db.htmlPageContent.find( { community: beta_communityId } ).forEach(function(beta_htmlPageContent){
|
||
|
var beta_beta_htmlPageContent_page_ObjectId = ObjectId(beta_htmlPageContent.page);
|
||
|
var beta_htmlPage = beta_db.page.findOne( { _id: beta_beta_htmlPageContent_page_ObjectId } );//.route;
|
||
|
if(beta_htmlPage) {
|
||
|
var prod_htmlPage = prod_db.page.findOne( { route: beta_htmlPage.route } );//._id.str;
|
||
|
if(prod_htmlPage) {
|
||
|
prod_db.htmlPageContent.save({ "page" : prod_htmlPage._id.str, "community" : prod_communityId, "content" : beta_htmlPageContent.content });
|
||
|
} else {
|
||
|
print("\nERROR: Page with route: "+beta_htmlPage.route+" does not exist on production db: "+prod_db_name+"\n");
|
||
|
}
|
||
|
} else {
|
||
|
print("\nERROR: Page with id: "+beta_htmlPageContent.page+" (of htmlPageContent: "+beta_htmlPageContent._id.str+") does not exist on beta db: "+beta_db_name+"\n");
|
||
|
}
|
||
|
});
|
||
|
|
||
|
print("htmlPageContents of community were migrated");
|
||
|
|
||
|
// migrate notifications preferences of community
|
||
|
var prod_notifications = prod_db.notifications.find({ communityPid: communityPid } );
|
||
|
var prod_notifications_emails = [];
|
||
|
if(prod_notifications) {
|
||
|
prod_notifications_emails = prod_notifications.map(function(preference){return preference.managerEmail;})
|
||
|
}
|
||
|
|
||
|
beta_db.notifications.find({ communityPid: communityPid } ).forEach(function(preference){
|
||
|
if(prod_notifications_emails.indexOf(preference.managerEmail) >= 0) {
|
||
|
prod_db.notifications.update({ managerEmail: preference.managerEmail }, {$set: {notifyForNewManagers: preference.notifyForNewManagers, notifyForNewSubscribers: preference.notifyForNewSubscribers}})
|
||
|
} else {
|
||
|
prod_db.notifications.insert(preference);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
print("Notification preferences of community were migrated");
|
||
|
|
||
|
}
|
||
|
|
||
|
migrate_community("egi", 'openaire_admin_copy', 'openaire_admin_prod');
|
||
|
|