Files
meanTorrent/scripts/upgrade-users-sparse-index.js
Michael Leanos 4906611ccc fix(users): GitHub strategy missing email (#1250)
Fixes an issue with an empty/missing/null Email coming from GitHub's
OAuth call response.

Also, introduces the `sparse` index option on the User model's Email
field. This will ensure that we can have multiple User documents without
the Email field.

Adds a server-side User model test for the sparse index setting on the
email field.

Confirms that User documents without the email field are not indexed,
illustrating the sparse option on the schema's email field works
properly.

Added the dropdb task to the Gulp test:client & test:server tasks, to
ensure we have a clean database & that any indexes are rebuilt; this
will ensure any Schema changes (in this case the email index is rebuilt using
the sparse index option) are reflected when the database is started again.

Added a UPGRADE.md for tracking important upgrade information for our
user's to be aware of, when we introduce potentially breaking changes.

Included an explanation of the Sparse index being added, and how to apply it
to an existing MEANJS application's database.

Adds a script for dropping the `email` field's index from the User
collection.

Related #1145
2016-04-29 14:13:46 -07:00

65 lines
1.7 KiB
JavaScript

'use strict';
// Set the Node ENV
process.env.NODE_ENV = 'development';
var chalk = require('chalk'),
mongoose = require('../config/lib/mongoose');
mongoose.loadModels();
var _indexToRemove = 'email_1',
errors = [],
processedCount = 0;
mongoose.connect(function (db) {
// get a reference to the User collection
var userCollection = db.connections[0].collections.users;
console.log();
console.log(chalk.yellow('Removing index "' +
_indexToRemove + '" from the User collection.'));
console.log();
// Remove the index
userCollection.dropIndex(_indexToRemove, function (err, result) {
var message = 'Successfully removed the index "' + _indexToRemove + '".';
if (err) {
errors.push(err);
message = 'An error occured while removing the index "' +
_indexToRemove + '".';
if (err.message.indexOf('index not found with name') !== -1) {
message = 'Index "' + _indexToRemove + '" could not be found.' +
'\r\nPlease double check the index name in your ' +
'mongodb User collection.';
}
reportAndExit(message);
} else {
reportAndExit(message);
}
});
});
function reportAndExit(message) {
if (errors.length) {
console.log(chalk.red(message));
console.log();
console.log(chalk.yellow('Errors:'));
for (var i = 0; i < errors.length; i++) {
console.log(chalk.red(errors[i]));
if (i === errors.length -1) {
process.exit(0);
}
}
} else {
console.log(chalk.green(message));
console.log(chalk.green('The next time your application starts, ' +
'Mongoose will rebuild the index "' + _indexToRemove + '".'));
process.exit(0);
}
}