2020-03-31 14:02:29 +02:00
//version compatibility: 1.1.1-SNAPSHOT
print ( "migrateCommunityIn_db script running..." ) ;
2020-09-24 14:02:39 +02:00
function migrate _portal ( portalPid , beta _db _name , prod _db _name ) {
2020-03-31 14:02:29 +02:00
beta _db = db . getSiblingDB ( beta _db _name ) ;
2020-12-04 10:41:00 +01:00
// db.auth("your_username", "your_password");
2020-03-31 14:02:29 +02:00
prod _db = db . getSiblingDB ( prod _db _name ) ;
2020-12-04 10:41:00 +01:00
// db.auth("your_username", "your_password");
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
print ( "migrate_portal: " + portalPid + " - both dbs are here" ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
var beta _portal = beta _db . portal . findOne ( { pid : portalPid } ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
if ( ! beta _portal ) {
print ( "ERROR: Portal with pid: " + portalPid + " does not exist in (beta) database: " + beta _db _name ) ;
2020-03-31 14:02:29 +02:00
return ;
}
2020-09-24 14:02:39 +02:00
var prod _portalOld = prod _db . portal . findOne ( { pid : portalPid } ) ;
var prod _portalOldId ;
if ( prod _portalOld ) {
prod _portalOldId = prod _portalOld . _id . str ;
2020-03-31 14:02:29 +02:00
}
2020-09-24 14:02:39 +02:00
// delete portal from production db
prod _db . portal . remove ( { "pid" : portalPid } ) ;
print ( "Portal with pid: " + portalPid + " is deleted (if existed) from production db: " + prod _db _name ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
// migrate portal pages
//beta_portal_pages = beta_db.portal.findOne({ pid: portalPid } ).map( function(portal) { return portal.pages } );
var beta _portal _pages = beta _portal . pages ;
var prod _portal _pages = { }
for ( var beta _pageId in beta _portal _pages ) {
2020-03-31 14:02:29 +02:00
var beta _pageObjectId = ObjectId ( beta _pageId ) ;
2020-09-24 14:02:39 +02:00
//var prod_pageId = prod_db.portal.find( { route: beta_db.page.find( { _id: beta_pageObjectId }).map( function(page) { return page.route; } ).toString() });
2020-03-31 14:02:29 +02:00
var beta _page = beta _db . page . findOne ( { _id : beta _pageObjectId } ) ;
if ( beta _page ) {
2020-09-24 14:02:39 +02:00
var prod _page = prod _db . page . findOne ( { route : beta _page . route , portalType : beta _page . portalType } ) ;
2020-03-31 14:02:29 +02:00
if ( prod _page ) {
2020-09-24 14:02:39 +02:00
prod _portal _pages [ prod _page . _id . str ] = beta _portal _pages [ beta _pageId ] ;
2020-03-31 14:02:29 +02:00
} 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" ) ;
}
}
2020-09-24 14:02:39 +02:00
print ( "Portal pages are ready to be migrated" ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
// migrate portal entities
var beta _portal _entities = beta _portal . entities ;
var prod _portal _entities = { }
for ( var beta _entityId in beta _portal _entities ) {
2020-03-31 14:02:29 +02:00
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 ) {
2020-09-24 14:02:39 +02:00
prod _portal _entities [ prod _entity . _id . str ] = beta _portal _entities [ beta _entityId ] ;
2020-03-31 14:02:29 +02:00
} 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" ) ;
}
}
2020-09-24 14:02:39 +02:00
print ( "Portal entities are ready to be migrated" ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
prod _db . portal . save ( {
"name" : beta _portal . name , "pid" : portalPid , "type" : beta _portal . type ,
"pages" : prod _portal _pages , "entities" : prod _portal _entities
} ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
print ( "Portal is migrated" ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
// delete portal statistics from production db
prod _db . statistics . remove ( { "pid" : portalPid } ) ;
print ( "Statistics for portal with pid: " + portalPid + " are deleted (if existed) from production db: " + prod _db _name ) ;
// migrate statistics for portal
var beta _statistics = beta _db . statistics . findOne ( { pid : portalPid } ) ;
2020-03-31 14:02:29 +02:00
prod _db . statistics . save ( beta _statistics ) ;
2020-09-24 14:02:39 +02:00
print ( "Statistics for portal were migrated" ) ;
// migrate subscribers of portal
var beta _portalSubscr = beta _db . portalSubscribers . findOne ( { pid : portalPid } ) ;
if ( beta _portalSubscr ) {
var beta _portalSubscribers = beta _portalSubscr . subscribers ;
var prod _portalSubscribers = [ ] ;
var prod _portalSubscribersEmails = [ ] ;
var prod _portalSubscr = prod _db . portalSubscribers . findOne ( { "pid" : portalPid } ) ;
if ( prod _portalSubscr ) {
prod _portalSubscribers = prod _portalSubscr . subscribers ;
prod _portalSubscribersEmails = prod _portalSubscr . subscribers . map ( function ( subscriber ) { return subscriber . email } ) ;
2020-03-31 14:02:29 +02:00
}
2020-09-24 14:02:39 +02:00
beta _portalSubscribers . forEach ( function ( beta _portalSubscriber ) {
var addInPortal = true ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
// if user exists in portal subscribers already, do nothing
if ( prod _portalSubscribersEmails . indexOf ( beta _portalSubscriber . email ) >= 0 ) {
addInPortal = false ;
2020-03-31 14:02:29 +02:00
} else {
2020-09-24 14:02:39 +02:00
var prod _subscriber = prod _db . subscriber . findOne ( { email : beta _portalSubscriber . email } ) ;
2020-03-31 14:02:29 +02:00
// if subscriber of beta does not exist in production, add him
if ( ! prod _subscriber ) {
2020-09-24 14:02:39 +02:00
prod _subscriber = { email : beta _portalSubscriber . email } ;
2020-03-31 14:02:29 +02:00
prod _db . subscriber . save ( prod _subscriber ) ;
2020-09-24 14:02:39 +02:00
print ( "subscriber " + beta _portalSubscriber . email + " added in production DB" ) ;
2020-03-31 14:02:29 +02:00
}
}
2020-09-24 14:02:39 +02:00
if ( addInPortal ) {
var prod _portalSubscriber = prod _db . subscriber . findOne ( { email : beta _portalSubscriber . email } ) ;
prod _portalSubscribers . push ( prod _portalSubscriber ) ;
2020-03-31 14:02:29 +02:00
}
} )
2020-09-24 14:02:39 +02:00
if ( prod _portalSubscr ) {
prod _db . portalSubscribers . update ( { "pid" : portalPid } , { $set : { subscribers : prod _portalSubscribers } } ) ;
2020-03-31 14:02:29 +02:00
} else {
2020-09-24 14:02:39 +02:00
prod _db . portalSubscribers . insert ( { "pid" : portalPid , "subscribers" : prod _portalSubscribers } ) ;
2020-03-31 14:02:29 +02:00
}
2020-09-24 14:02:39 +02:00
print ( "subscribers of portal were migrated" ) ;
2020-03-31 14:02:29 +02:00
} else {
2020-09-24 14:02:39 +02:00
print ( "\nERROR: Subscribers not migrated. No PortalSubscribers for this portal on beta db: " + beta _db _name + "\n" ) ;
2020-03-31 14:02:29 +02:00
}
2020-09-24 14:02:39 +02:00
// migrate divHelpContents of portal
var beta _portalId = beta _portal . _id . str ; //beta_db.portal.findOne( { pid: portalPid } )._id.str;
var prod _portalId = prod _db . portal . findOne ( { pid : portalPid } ) . _id . str ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
if ( prod _portalOldId ) {
prod _db . divHelpContent . remove ( { "portal" : prod _portalOldId } ) ;
2020-03-31 14:02:29 +02:00
}
//var beta_divHelpContents =
2020-09-24 14:02:39 +02:00
beta _db . divHelpContent . find ( { portal : beta _portalId } ) . forEach ( function ( beta _divHelpContent ) { //.map( function(divHelpContent) { return divHelpContent; } );
2020-03-31 14:02:29 +02:00
//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 ) {
2020-09-24 14:02:39 +02:00
var prod _divId = prod _db . divId . findOne ( { name : beta _divId . name , portalType : beta _divId . portalType } ) ; //._id.str;
2020-03-31 14:02:29 +02:00
if ( prod _divId ) {
2020-09-24 14:02:39 +02:00
prod _db . divHelpContent . save ( { "divId" : prod _divId . _id . str , "portal" : prod _portalId , "content" : beta _divHelpContent . content , "isActive" : beta _divHelpContent . isActive } ) ;
2020-03-31 14:02:29 +02:00
} 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" ) ;
}
//}
} ) ;
2020-09-24 14:02:39 +02:00
print ( "divHelpContents of portal were migrated" ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
// migrate pageHelpContents of portal
if ( prod _portalOldId ) {
prod _db . pageHelpContent . remove ( { "portal" : prod _portalOldId } ) ;
2020-03-31 14:02:29 +02:00
}
2020-09-24 14:02:39 +02:00
beta _db . pageHelpContent . find ( { portal : beta _portalId } ) . forEach ( function ( beta _pageHelpContent ) {
2020-03-31 14:02:29 +02:00
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 ) {
2020-09-24 14:02:39 +02:00
var prod _page = prod _db . page . findOne ( { route : beta _page . route , portalType : beta _page . portalType } ) ; //._id.str;
2020-03-31 14:02:29 +02:00
if ( prod _page ) {
2020-09-24 14:02:39 +02:00
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 } ) ;
2020-03-31 14:02:29 +02:00
} 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" ) ;
}
} ) ;
2020-09-24 14:02:39 +02:00
print ( "pageHelpContents of portal were migrated" ) ;
2020-03-31 14:02:29 +02:00
2020-09-24 14:02:39 +02:00
// migrate notifications preferences of portal
var prod _notifications = prod _db . notifications . find ( { portalPid : portalPid } ) ;
2020-03-31 14:02:29 +02:00
var prod _notifications _emails = [ ] ;
if ( prod _notifications ) {
prod _notifications _emails = prod _notifications . map ( function ( preference ) { return preference . managerEmail ; } )
}
2020-09-24 14:02:39 +02:00
beta _db . notifications . find ( { portalPid : portalPid } ) . forEach ( function ( preference ) {
2020-03-31 14:02:29 +02:00
if ( prod _notifications _emails . indexOf ( preference . managerEmail ) >= 0 ) {
2020-09-24 14:02:39 +02:00
prod _db . notifications . update ( { managerEmail : preference . managerEmail , portalPid : portalPid } , { $set : { notifyForNewManagers : preference . notifyForNewManagers , notifyForNewSubscribers : preference . notifyForNewSubscribers } } )
2020-03-31 14:02:29 +02:00
} else {
prod _db . notifications . insert ( preference ) ;
}
} ) ;
2020-09-24 14:02:39 +02:00
print ( "Notification preferences of portal were migrated" ) ;
2020-03-31 14:02:29 +02:00
}
2021-02-25 11:18:49 +01:00
function migrateCurators ( beta _db _name , prod _db _name ) {
beta _db = db . getSiblingDB ( beta _db _name ) ;
// beta_db.auth("your_username", "your_password");
prod _db = db . getSiblingDB ( prod _db _name ) ;
// prod_db.auth("your_username", "your_password");
print ( "migrateCurators: both dbs are here" ) ;
// migrate curators of portal
var beta _curators = beta _db . curator . find ( ) . forEach ( function ( beta _curator ) {
if ( beta _curator ) {
prod _db . curator . remove ( { "email" : beta _curator . email } ) ;
curator = { } ;
curator [ '_id' ] = beta _curator . _id ; // this is aaiId
if ( beta _curator . email ) {
curator [ 'email' ] = beta _curator . email ;
} else {
curator [ 'email' ] = "" ;
}
if ( beta _curator . name ) {
curator [ 'name' ] = beta _curator . name ;
} else {
curator [ 'name' ] = "" ;
}
if ( beta _curator . affiliations ) {
curator [ 'affiliations' ] = beta _curator . affiliations ;
} else {
curator [ 'affiliations' ] = [ ] ;
}
if ( beta _curator . photo ) {
curator [ 'photo' ] = beta _curator . photo ;
} else {
curator [ 'photo' ] = "" ;
}
if ( beta _curator . bio ) {
curator [ 'bio' ] = beta _curator . bio ;
} else {
curator [ 'bio' ] = "" ;
}
// prod_db.curator.insert({"email": beta_curator.email,"name": beta_curator.name, "affiliations": beta_curator.affiliations, "photo": beta_curator.photo, "bio": beta_curator.bio});
prod _db . curator . insert ( curator ) ;
print ( "curator " + beta _curator . name + " is migrated" ) ;
}
} ) ;
}
2020-03-31 14:02:29 +02:00
2021-02-25 11:18:49 +01:00
// migrate_portal("aginfra", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("rural-digital-europe", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("clarin", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("enermaps", 'openaireconnect_new', 'openaireconnect');
// // migrate_portal("ifremer", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("instruct", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("risis", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("rda", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("science-innovation-policy", 'openaireconnect_new', 'openaireconnect');
// // migrate_portal("sobigdata", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("sdsn-gr", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("elixir-gr", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("beopen", 'openaireconnect_new', 'openaireconnect');
// // migrate_portal("euromarine", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("oa-pg", 'openaireconnect_new', 'openaireconnect');
//
//
// migrate_portal("covid-19", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("dariah", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("dh-ch", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("egi", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("mes", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("epos", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("fam", 'openaireconnect_new', 'openaireconnect');
// migrate_portal("ni", 'openaireconnect_new', 'openaireconnect');
//
migrateCurators ( "openaireconnect_new" , "openaireconnect" ) ;