Last active
November 17, 2016 07:47
-
-
Save xavxyz/cd97d4bc6a7bdfbd2bcbe27eb28b8eb3 to your computer and use it in GitHub Desktop.
Migration script for Nova Users to run server-side: from user.telescope.settingX to user.__settingX (without removing user.telescope.settingX for backward compatibility)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Explanation: | |
// This is a migration script for your users to be compatible with the Apollo version of Nova | |
// We do not support anymore users properties on the `telescope` namespace. They will be duplicated onto __setting: | |
// Example: `user.telescope.karma` becomes `user.__karma` | |
// Note: we do not remove the `user.telescope` object, you'll still be able to go back to your previous full Meteor build if you feel that Apollo doesn't fit your needs | |
// How to use: | |
// Copy/paste the script below at the end of the nova:users /packages/nova-users/lib/server.js (package's server entry-point) | |
Meteor.startup(function runMigrationToApollo() { | |
// find users with telescope fields (full-Meteor Nova) and an arbitrary Apollo user setting | |
const usersCursor = Users.find({telescope: {$exists: true}, __karma: {$exists: false}}); | |
const usersCount = usersCursor.count(); | |
const users = usersCursor.fetch(); | |
console.log('// --------------------------------------------------------'); | |
console.log('// Nova Users migration to Apollo data'); | |
// zero user to migrate \o/ | |
if (!usersCount) { | |
console.log('// Status: All good! 👍 We didn\'t find any user to migrate! '); | |
console.log('// ====> You should remove this script from your application! 🔭'); | |
console.log('// --------------------------------------------------------'); | |
return; | |
} | |
console.log(`// Status: Starting the migration, ${usersCount} users to migrate... (it may take some time)`); | |
// duplicate telescope namespace to the root of the user object | |
const apolloUsers = users.map(user => { | |
// telescopeNamespace.bio = "Hey, I'm a Telescope Nova user!" | |
// telescopeNamespace.email = "xxx@xxx.com" | |
// telescopeNamespace.displayName = "John Doe" | |
// telescopeNamespace.x = y | |
const telescopeNamespace = user.telescope; | |
// "bio" => {__bio: "Hey, I'm a Telescope Nova user!"} | |
// "email" => {__email: "xxx@xxx.com"} | |
// "displayName" => {__displayName: "John Doe"} | |
// "x" => {__x: y} | |
const arrayOfSettings = Object.keys(telescopeNamespace).map(key => { | |
return {[`__${key}`]: telescopeNamespace[key]}; | |
}); | |
// { | |
// __bio: "Hey, ...", | |
// __email: "xxx@xxx.com", | |
// __displayName: "John Doe", | |
// __x: "y" | |
// } | |
const newSettings = arrayOfSettings.reduce((result, item) => { | |
return { | |
...result, | |
...item | |
}; | |
}, {}); | |
// { | |
// _id: "yvZfYTWC8HZggSEa5", | |
// __bio "Hey, ...", | |
// __email: "xxx@xxx.com", | |
// __displayName: "John Doe", | |
// __x: "y" | |
// } | |
return { | |
_id: user._id, | |
...newSettings | |
}; | |
}); | |
// migrate the users with the new settings | |
const migratedUsers = apolloUsers.map(apolloUserSettings => { | |
const userId = apolloUserSettings._id; | |
delete apolloUserSettings._id; // maybe not needed ? this is to avoid overwritting the _id, even if it's the same | |
return Users.update({_id: userId}, {$set: apolloUserSettings}); | |
}); | |
// maybe do something on migratedUsers to check if there any false value (update failure) and log it? could this happen? Mongo connection lost? | |
console.log('// Status: Migration of Users to Apollo completed! 👍') | |
console.log('// ====> You should remove this script from your application! 🔭'); | |
console.log('// --------------------------------------------------------'); | |
}); |
What about also logging the migration to the Migrations
collection? The previous migrations script would insert the following properties:
name
startedAt
completed (true/false)
itemsAffected
finishedAt
Another thing that might be useful is set a special __hasMigrated
flag on a user profile once it's been migrated. This way if the user has to interrupt and re-run the migration for some reason they don't need to re-migrate everything.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feedbacks and contributions on this snippet are more than welcomed! 👍