Skip to content

Instantly share code, notes, and snippets.

@pratik60
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save pratik60/b283b53d874e0e7dabb1 to your computer and use it in GitHub Desktop.

Select an option

Save pratik60/b283b53d874e0e7dabb1 to your computer and use it in GitHub Desktop.
Migration
/*
From mean 4 directory
cp -rv ../carpe-diem/public/profile-pictures/* cactus-intranet/files/Images/usersPictures
cp -rv ../carpe-diem/public/coverPictures/* cactus-intranet/files/Images/coverPictures/
cp -rv ../carpe-diem/public/groupPictures/* cactus-intranet/files/Images/groupsPictures/
cp -rv ../carpe-diem/public/bannerPictures/* cactus-intranet/files/Images/bannerImages/
cp -rv ../carpe-diem/public/articlesPictures/* cactus-intranet/files/Images/articlesPictures/
cp -rv ../carpe-diem/public/img/albumPictures/* cactus-intranet/files/Images/albumsPictures/
cp -rv ../carpe-diem/public/img/profile-pictures/* cactus-intranet/files/Images/usersPictures/
*/
var mongojs = require('mongojs');
var _ = require('lodash');
var S = require('string');
var async = require('async');
var collections = ['posts', 'users', 'groups', 'arigatos', 'likes', 'comments', 'polls', 'awards', 'variables'];
var ocp = require('mongojs').connect('mongodb://localhost/old-cactus-prod', collections);
var ncp = require('mongojs').connect('mongodb://localhost/new-cactus-prod', collections);
var ObjectId = mongojs.ObjectId;
var fs = require('fs');
async.series([
function(callback) {
ocp.users.find().forEach(function(err, user) {
if (user) {
preprocessUsers(user, saveUsers);
} else {
ocp.awards.update({
award: {
$in: ['Excellence', 'Excellene']
}
}, {
$set: {
award: 'Excellence'
}
}, {
multi: true
});
ocp.awards.update({
award: {
$in: ['Communication', 'communication']
}
}, {
$set: {
award: 'Communication'
}
}, {
multi: true
});
console.log('voila users');
callback(null, 'myusers');
}
});
},
function(callback) {
ocp.awards.find().forEach(function(err, oldAward) {
if (oldAward) {
ncp.variables.findOne({
'name': 'award',
'value': oldAward.award
}, function(err, awardVariable) {
ncp.users.update({
"_id": ObjectId(oldAward.user)
}, {
$push: {
awards: {
award: ObjectId(awardVariable._id),
date: oldAward.created
}
}
});
});
} else {
console.log('voila awards');
callback(null, 'awards');
}
});
},
function(callback) {
ocp.arigatos.find().forEach(function(err, arigato) {
if (arigato) {
ncp.arigatos.save({
"_id": arigato._id,
"arigatoFor": arigato.arigatoFor,
"arigatoText": arigato.arigatoText,
"arigatoBy": arigato.arigatoBy,
"created": arigato.created,
"changed": arigato.changed
});
} else {
console.log('voila arigatos');
callback(null, 'myarigatos');
}
});
},
function(callback) {
ocp.polls.find().forEach(function(err, poll) {
if (poll) {
ncp.polls.save({
"_id": poll._id,
"choices": poll.choices,
"created": poll.created,
"open": poll.open,
"question": poll.question,
"user": ObjectId('536a98650cf13dd3d6a21286')
});
} else {
console.log('voila polls');
callback(null, 'mypolls');
}
});
},
function(callback) {
ocp.groups.find().forEach(function(err, group) {
if (group) {
ncp.groups.save({
"_id": group._id,
"created": group.created,
"name": group.name,
"groupCoverImage": 'files/Images/groupsPictures/' + S(group.groupCoverImage).chompLeft('groupPictures/').s,
"groupPreviewImage": 'files/Images/groupsPictures/' + S(group.groupImage).chompLeft('groupPictures/').s,
"about": group.about,
"members": group.members,
"moderators": group.moderators,
"admin": group.admin,
"wallUser": group.wallUser,
"grouptype": group.grouptype
});
} else {
console.log('voila groups');
callback(null, 'mygroups');
}
});
},
function(callback) {
ocp.posts.find().forEach(function(err, post) {
if (post) {
preprocessPosts(post, savePosts);
} else {
console.log('voila posts');
callback(null, 'myposts');
}
});
},
function(callback) {
ocp.comments.find().forEach(function(err, comment) {
if (comment) {
ncp.comments.save({
"_id": comment._id,
"created": comment.created,
"changed": comment.changed,
"body": comment.body,
"user": comment.user,
"parent": ObjectId(comment.parent)
});
} else {
console.log('voila comments');
callback(null, 'mycomments');
}
});
},
function(callback) {
ocp.likes.find().forEach(function(err, like) {
if (like && like.referenceType == 'post') {
ncp.posts.update({
"_id": ObjectId(like.referenceId)
}, {
$addToSet: {
likes: like.user
}
});
} else if (like && like.referenceType == 'comment') {
ncp.comments.update({
"_id": ObjectId(like.referenceId)
}, {
$addToSet: {
likes: like.user
}
});
} else {
console.log('voila likes');
callback(null, 'my likes');
}
});
},
function(callback) {
ncp.groups.find().forEach(function(err, group) {
if (group && group.members) {
_.forEach(group.members, function(groupuser) {
ncp.users.update({
'_id': ObjectId(groupuser)
}, {
$addToSet: {
groups: group._id
}
});
});
} else {
console.log('group users added');
callback(null, 'user groups');
}
});
},
function(callback) {
ncp.users.update({
designation: null
}, {
$unset: {
designation: ""
}
}, {
multi: true
});
ncp.users.update({
bio: null
}, {
$unset: {
bio: ""
}
}, {
multi: true
});
ncp.users.update({
team: null
}, {
$unset: {
team: ""
}
}, {
multi: true
});
ncp.users.update({
mobile: null
}, {
$unset: {
mobile: ""
}
}, {
multi: true
});
ncp.users.update({
location: null
}, {
$unset: {
location: ""
}
}, {
multi: true
});
ncp.users.update({
joiningDate: null
}, {
$unset: {
joiningDate: ""
}
}, {
multi: true
});
ncp.users.update({
roles: {
$size: 0
}
}, {
$addToSet: {
roles: "authenticated"
}
}, {
multi: true
});
ncp.posts.update({
wallUser: null
}, {
$unset: {
wallUser: ""
}
}, {
multi: true
});
ncp.posts.update({
scrapeUrl: null
}, {
$unset: {
scrapeUrl: ""
}
}, {
multi: true
});
ncp.posts.update({
group: null
}, {
$unset: {
group: ""
}
}, {
multi: true
});
ncp.posts.update({
albumImages: null
}, {
$unset: {
albumImages: ""
}
}, {
multi: true
});
ncp.posts.update({
filePath: null
}, {
$unset: {
filePath: ""
}
}, {
multi: true
});
ncp.posts.update({
pinPost: null
}, {
$unset: {
pinPost: ""
}
}, {
multi: true
});
callback(null, 'cleanup time');
}
], function(err, results) {
console.log(results);
});
function preprocessUsers(user, callback) {
if (user.picture == '/img/default/default_profile_small.jpg') {
user.picture = '/users/assets/img/default-grav.jpg';
} else if (S(user.picture).contains('/img/profile-pictures/')) {
if (fs.existsSync('/home/intranetnew/carpe-diem/public' + user.picture)) {
console.log(user.picture + 'exists');
user.picture = 'files/Images/usersPictures/' + S(user.picture).chompLeft('/img/profile-pictures/').s;
} else {
console.log(user.picture + 'does not exist');
user.picture = '/users/assets/img/default-grav.jpg';
}
} else if (S(user.picture).contains('profile-pictures/')) {
user.picture = 'files/Images/usersPictures/' + S(user.picture).chompLeft('profile-pictures/').s;
}
if (user.coverPicture == '/img/default/fb-cover.jpg') {
user.coverPicture = "/users/assets/img/default-cover.jpg";
} else
user.coverPicture = 'files/Images/coverPictures/' + S(user.coverPicture).chompLeft('coverPictures/').s;
callback(user);
}
function saveUsers(user) {
ncp.users.save({
"_id": user._id,
"name": S(user.name).unescapeHTML().s,
"email": user.email,
"username": S(user.username).unescapeHTML().s,
"created": user.created,
"changed": new Date(),
"picture": user.picture,
"coverPicture": user.coverPicture,
"roles": user.roles,
"status": user.status,
"designation": user.designation,
"followers": user.followers,
"birthdate": user.birthdate,
"bio": user.bio,
"team": user.team,
"extensionNumber": user.extension_number,
"mobile": user.mobile,
"location": user.location,
"joiningDate": user.joining_date,
"provider": user.provider,
"birthdateDay": user.birthdateDay,
"birthdateMonth": user.birthdateMonth
});
}
function preprocessPosts(post, callback) {
post.newTags = [];
if (post.previewImage !== undefined && post.previewImage) {
post.filePath = [];
post.filePath.push(post.previewImage)
}
if (post.tags) {
_.forEach(post.tags, function(tag) {
post.newTags.push(tag.name);
});
} else post.newTags = ['General'];
/*Post Type*/
if (post.postType == 'article') {
post.type = 'article';
if (post.attachFile) {
post.filePath = [];
post.filePath.push('files/Documents/' + S(post.attachFile).chompLeft('articleFiles/').s);
}
} else if (post.postType == 'text')
post.type = 'shortMessage';
else if (post.postType == 'albums') {
post.type = 'album';
post.albumImages = [];
_.forEach(post.albumPreviewImage, function(image) {
post.albumImages.push({
"preview": 'files/Images/albumsPictures/' + S(image).chompLeft('img/albumPictures/').s,
"thumbnail": 'files/Images/albumsPictures/thumbnail_' + S(image).chompLeft('img/albumPictures/preview_').s,
"large": 'files/Images/albumsPictures/large_' + S(image).chompLeft('img/albumPictures/preview_').s
});
});
} else if (post.postType == 'image') {
post.type = 'file';
post.filePath = [];
post.filePath.push('files/Documents/' + S(post.postImage).chompLeft('img/postImages/').s);
}
callback(post);
}
function savePosts(post) {
ncp.posts.save({
"_id": post._id,
"created": post.created,
"changed": post.changed,
"body": post.message,
"title": post.title,
"scrapeUrl": post.scrapeUrl,
"abstract": post.abstract,
"pinPost": post.pinPost,
"type": post.type,
"wallUser": post.wallUser,
"group": post.group,
"user": post.user,
"tags": post.newTags,
"albumImages": post.albumImages,
"filePath": post.filePath
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment