Skip to content

Commit

Permalink
Finishes implementation of new sails-disk error, prevents prompting i…
Browse files Browse the repository at this point in the history
…n production (showing a warning instead), and includes an example of how to temporarily skip the 'migrate' prompt using a CLI option.
  • Loading branch information
mikermcneil committed Mar 15, 2016
1 parent 9a0d46e commit 3161c34
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions lib/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,41 +219,54 @@ module.exports = function initialize(hook, sails, done){
// If NODE_ENV is "production", check if any models are using
// a datastore running on `sails-disk`. If so, show a warning.
_productionCheck: ['_normalizeModelDefs', function (next) {
try {

// We use `process.env.NODE_ENV` instead of `sails.config.environment`
// to allow for the environment to be set to e.g. "staging" while the
// NODE_ENV is set to "production".
if (process.env.NODE_ENV === 'production') {
// > **Remember:**
// > In a production environment, regardless of your logical `environment`
// > config, the NODE_ENV environment variable should be set. Setting
// > `sails.config.environment` to production does this automatically.

// e.g. ['localDiskDb', 'foobar']
var datastoresUsingSailsDisk = _.reduce(sails.config.connections, function(memo, datastoreConf, identity){
if (datastoreConf.adapter === 'sails-disk') {
memo.push(identity);
}
return memo;
}, []);
// We use `process.env.NODE_ENV` instead of `sails.config.environment`
// to allow for the environment to be set to e.g. "staging" while the
// NODE_ENV is set to "production".
if (process.env.NODE_ENV === 'production') {
// > **Remember:**
// > In a production environment, regardless of your logical `environment`
// > config, the NODE_ENV environment variable should be set. Setting
// > `sails.config.environment` to production does this automatically.

// e.g. ['localDiskDb', 'foobar']
var datastoresUsingSailsDisk = _.reduce(sails.config.connections, function(memo, datastoreConf, identity){
if (datastoreConf.adapter === 'sails-disk') {
memo.push(identity);
}
return memo;
}, []);
console.log('datastoresUsingSailsDisk',datastoresUsingSailsDisk);

// e.g. ['user', 'product']
var modelsUsingSailsDisk = _.reduce(hook.models, function(memo, normalizedModelDef, identity){
if (_.contains(datastoresUsingSailsDisk, normalizedModelDef.connection)) {
memo.push(identity);
// e.g. ['user', 'product']
var modelsUsingSailsDisk = _.reduce(hook.models, function(memo, normalizedModelDef, identity){

// Look up the referenced datastore for this model, and then check to see if
// it matches any of the datastores using the sails-disk adapter.
var referencedDatastore = normalizedModelDef.connection[0];
if (_.contains(datastoresUsingSailsDisk, referencedDatastore)) {
memo.push(identity);
}
return memo;
}, []);
console.log('modelsUsingSailsDisk',modelsUsingSailsDisk);

if (modelsUsingSailsDisk.length > 0) {
sails.log.warn('The default `sails-disk` adapter is not designed for use as a production database;');
sails.log.warn('(it stores the entire contents of your database in memory)');
sails.log.warn('Instead, please use another adapter; e.g. sails-postgresql or sails-mongo.');
sails.log.warn('For more info, see: http://sailsjs.org/documentation/concepts/deployment');
sails.log.warn('To hide this warning message, enable `sails.config.orm.skipProductionWarnings`.');
}
return memo;
}, []);

if (modelsUsingSailsDisk.length > 0) {
sails.log.warn('The default `sails-disk` adapter is not designed for use as a production database;');
sails.log.warn('(it stores the entire contents of your database in memory)');
sails.log.warn('Instead, please use another adapter; e.g. sails-postgresql or sails-mongo.');
sails.log.warn('For more info, see: http://sailsjs.org/documentation/concepts/deployment');
sails.log.warn('To hide this warning message, enable `sails.config.orm.skipProductionWarnings`.');
}
}
// Just in case.
catch (e) {
return next(e);
}

// Otherwise it worked!
return next();
}],

Expand All @@ -262,8 +275,7 @@ module.exports = function initialize(hook, sails, done){
// to make a decision if no migrate configuration is present.
//
// Note that, if this is a production environment, the `migrate`
// setting has already been forced to "safe" when the model
// definitions were validated/normalized.
// setting will always be forced to "safe" in Waterline.
_doubleCheckMigration: ['_productionCheck', function (next) {

// If there are no models, we're good.
Expand All @@ -276,6 +288,15 @@ module.exports = function initialize(hook, sails, done){
return next();
}

// If this is a production NODE_ENV, show a slightly different message and skip the prompt.
if (process.env.NODE_ENV === 'production') {
console.log('');
sails.log.info('A project-wide `sails.config.models.migrate` setting has not been configured for this app.');
sails.log.info('Since the NODE_ENV env variable is set to "production", auto-migration will be disabled automatically.');
sails.log.info('(i.e. `migrate: \'safe\'`)');
return next();
}

// Otherwise show a prompt
console.log('-----------------------------------------------------------------');
console.log();
Expand All @@ -302,7 +323,8 @@ module.exports = function initialize(hook, sails, done){
console.log('What would you like Sails to do?');
console.log();
sails.log.info('To skip this prompt in the future, set `sails.config.models.migrate`.');
sails.log.info('(conventionally, this is done in `config/models.js`)');
sails.log.info('Usually this is done in a config file (e.g. `config/models.js`),');
sails.log.info('or as an override (e.g. `sails lift --models.migrate=\'alter\').');
console.log();
sails.log.warn('** DO NOT CHOOSE "2" or "3" IF YOU ARE WORKING WITH PRODUCTION DATA **');
console.log();
Expand Down

0 comments on commit 3161c34

Please sign in to comment.