2016-05-01 14:27:04 +00:00
// Migrate unsubscriptions collection to new schema
// The console-stamp module must be installed (not included in package.json)
// It requires two environment variables: MONGODB_OLD and MONGODB_NEW
// Due to some big user profiles it needs more RAM than is allowed by default by v8 (arounf 1.7GB).
// Run the script with --max-old-space-size=4096 to allow up to 4GB of RAM
console . log ( 'Starting migrations/api_v3/unsubscriptions.js.' ) ;
require ( 'babel-register' ) ;
var Q = require ( 'q' ) ;
var MongoDB = require ( 'mongodb' ) ;
var nconf = require ( 'nconf' ) ;
var mongoose = require ( 'mongoose' ) ;
var _ = require ( 'lodash' ) ;
var uuid = require ( 'uuid' ) ;
var consoleStamp = require ( 'console-stamp' ) ;
// Add timestamps to console messages
consoleStamp ( console ) ;
// Initialize configuration
2016-05-13 13:35:12 +00:00
require ( '../../website/server/libs/api-v3/setupNconf' ) ( ) ;
2016-05-01 14:27:04 +00:00
var MONGODB _OLD = nconf . get ( 'MONGODB_OLD' ) ;
var MONGODB _NEW = nconf . get ( 'MONGODB_NEW' ) ;
var MongoClient = MongoDB . MongoClient ;
2016-05-11 13:59:18 +00:00
mongoose . Promise = Bluebird ; // otherwise mongoose models won't work
2016-05-01 14:27:04 +00:00
// Load new models
2016-05-13 13:35:12 +00:00
var EmailUnsubscription = require ( '../../website/server/models/emailUnsubscription' ) . model ;
2016-05-01 14:27:04 +00:00
// To be defined later when MongoClient connects
var mongoDbOldInstance ;
var oldUnsubscriptionCollection ;
var mongoDbNewInstance ;
var newUnsubscriptionCollection ;
var BATCH _SIZE = 1000 ;
var processedUnsubscriptions = 0 ;
// Only process unsubscriptions that fall in a interval ie -> up to 0000-4000-0000-0000
var AFTER _UNSUBSCRIPTION _ID = nconf . get ( 'AFTER_UNSUBSCRIPTION_ID' ) ;
var BEFORE _UNSUBSCRIPTION _ID = nconf . get ( 'BEFORE_UNSUBSCRIPTION_ID' ) ;
function processUnsubscriptions ( afterId ) {
var processedTasks = 0 ;
var lastUnsubscription = null ;
var oldUnsubscriptions ;
var query = { } ;
if ( BEFORE _UNSUBSCRIPTION _ID ) {
query . _id = { $lte : BEFORE _UNSUBSCRIPTION _ID } ;
}
if ( ( afterId || AFTER _UNSUBSCRIPTION _ID ) && ! query . _id ) {
query . _id = { } ;
}
if ( afterId ) {
query . _id . $gt = afterId ;
} else if ( AFTER _UNSUBSCRIPTION _ID ) {
query . _id . $gt = AFTER _UNSUBSCRIPTION _ID ;
}
var batchInsertUnsubscriptions = newUnsubscriptionCollection . initializeUnorderedBulkOp ( ) ;
console . log ( ` Executing unsubscriptions query. \n Matching unsubscriptions after ${ afterId ? afterId : AFTER _UNSUBSCRIPTION _ID } and before ${ BEFORE _UNSUBSCRIPTION _ID } (included). ` ) ;
return oldUnsubscriptionCollection
. find ( query )
. sort ( { _id : 1 } )
. limit ( BATCH _SIZE )
. toArray ( )
. then ( function ( oldUnsubscriptionsR ) {
oldUnsubscriptions = oldUnsubscriptionsR ;
console . log ( ` Processing ${ oldUnsubscriptions . length } unsubscriptions. Already processed ${ processedUnsubscriptions } unsubscriptions. ` ) ;
if ( oldUnsubscriptions . length === BATCH _SIZE ) {
lastUnsubscription = oldUnsubscriptions [ oldUnsubscriptions . length - 1 ] . _id ;
}
oldUnsubscriptions . forEach ( function ( oldUnsubscription ) {
oldUnsubscription . email = oldUnsubscription . email . toLowerCase ( ) ;
var newUnsubscription = new EmailUnsubscription ( oldUnsubscription ) ;
batchInsertUnsubscriptions . insert ( newUnsubscription . toObject ( ) ) ;
} ) ;
console . log ( ` Saving ${ oldUnsubscriptions . length } unsubscriptions. ` ) ;
return batchInsertUnsubscriptions . execute ( ) ;
} )
. then ( function ( ) {
processedUnsubscriptions += oldUnsubscriptions . length ;
console . log ( ` Saved ${ oldUnsubscriptions . length } unsubscriptions. ` ) ;
if ( lastUnsubscription ) {
return processUnsubscriptions ( lastUnsubscription ) ;
} else {
return console . log ( 'Done!' ) ;
}
} ) ;
}
// Connect to the databases
2016-05-11 12:34:01 +00:00
Bluebird . all ( [
2016-05-01 14:27:04 +00:00
MongoClient . connect ( MONGODB _OLD ) ,
MongoClient . connect ( MONGODB _NEW ) ,
] )
. then ( function ( result ) {
var oldInstance = result [ 0 ] ;
var newInstance = result [ 1 ] ;
mongoDbOldInstance = oldInstance ;
oldUnsubscriptionCollection = mongoDbOldInstance . collection ( 'emailunsubscriptions' ) ;
mongoDbNewInstance = newInstance ;
newUnsubscriptionCollection = mongoDbNewInstance . collection ( 'emailunsubscriptions' ) ;
console . log ( ` Connected with MongoClient to ${ MONGODB _OLD } and ${ MONGODB _NEW } . ` ) ;
return processUnsubscriptions ( ) ;
} )
. catch ( function ( err ) {
console . error ( err . stack || err ) ;
} ) ;