Skip to content

Commit

Permalink
Added prompt as the default behavior for an undefined project-wide mi…
Browse files Browse the repository at this point in the history
…grate setting. (i.e. 'migrate:ask')
  • Loading branch information
mikermcneil committed Aug 6, 2014
1 parent 76339cf commit 5edb52b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
90 changes: 89 additions & 1 deletion lib/hooks/orm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var _ = require('lodash');
var async = require('async');
var prompt = require('prompt');
var howto_loadAppModelsAndAdapters = require('./load-user-modules');
var howto_normalizeModelDef = require('./normalize-model');
var howto_buildORM = require('./build-orm');
Expand Down Expand Up @@ -93,11 +94,98 @@ module.exports = function(sails) {
next(null, sails.models);
}],

// Before continuing any further to actually start up the ORM,
// check the migrate settings for each model to (1) use migrate:safe
// in production and (2) prompt the user to make a decision if no migrate
// configuration is present.
_doubleCheckMigration: ['normalizedModelDefs', function (next) {

// If there are no models, we're good
if (!_.keys(sails.models).length) {
return next();
}

// If a project-wide migrate setting (sails.config.models.migrate) is defined, we're good.
if (typeof sails.config.models.migrate !== 'undefined') {
return next();
}

// Otherwise show a prompt
prompt.start();
console.log('',
'Excuse my interruption, but it looks like this app'+'\n',
'does not have a project-wide "migrate" setting configured yet.'+'\n',
'(perhaps this is the first time you\'re lifting it with models?)'+'\n',
'\n',
'In short, this setting controls whether/how Sails will attempt to automatically'+'\n',
'rebuild the tables/collections/sets/etc. in your database schema.\n',
'You can read more about the "migrate" setting here:'+'\n',
'http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html?q=migrate)\n'
// 'command(⌘)+click to open links in the terminal'
);
console.log();
console.log('',
'In a production environment (NODE_ENV==="production") Sails always uses'+'\n',
'migrate:"safe" to protect inadvertent deletion of your data.\n',
'However during development, you have a few other options for convenience:'+'\n\n',
'1. safe - never auto-migrate my database(s). I will do it myself (by hand)','\n',
'2. alter - auto-migrate, but attempt to keep my existing data (experimental)\n',
'3. drop - wipe/drop ALL my data and rebuild models every time I lift Sails\n'
);
console.log('What would you like Sails to do?');
console.log();
sails.log.warn('** DO NOT CHOOSE "2" or "3" IF YOU ARE WORKING WITH PRODUCTION DATA **');
console.log();
prompt.get(['?'], function(err, result) {
if (err) return next(err);
result = +result['?'];

switch (result) {
case 2:
sails.config.models.migrate = 'alter';
break;
case 3:
sails.config.models.migrate = 'drop';
break;
default:
sails.config.models.migrate = 'safe';
break;
}

console.log();
console.log(' Temporarily setting `sails.config.models.migrate` to "%s"...', sails.config.models.migrate);
console.log(' (press CTRL+C to cancel-- continuing lift automatically in 2 seconds...)');
console.log();
console.log('',
'To skip this prompt next time, add a project-wide "migrate" setting\n',
'to your app. This is conventionally configured in `config/models.js`.'
);
setTimeout(function (){
return next();
},2200);
});

// async.eachSeries(_.keys(sails.models), function (identity, nextModel) {
// // If migrate setting is defined, continue without doing anything else.
// if (typeof sails.models[identity].migrate !== 'undefined') {
// return nextModel();
// }

// // If migrate setting is not specififed on this model,
// // display a prompt and require the user to make a decision about
// // their migration strategy.
// var thisModel = sails.models[identity];

// }, next);


}],

// Once all user model and adapter definitions are loaded
// and normalized, go ahead and initialize the ORM, which
// creates instantiated model objects and stuffs them in
// sails.models.
instantiatedCollections: ['normalizedModelDefs', function (next, async_data) {
instantiatedCollections: ['_doubleCheckMigration', function (next, async_data) {
buildORM(async_data.normalizedModelDefs, next);
}]

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"anchor": "~0.10.0",
"mock-req": "0.1.0",
"mock-res": "0.1.0",
"semver": "~2.2.1"
"semver": "~2.2.1",
"prompt": "^0.2.13"
},
"devDependencies": {
"root-require": "~0.2.0",
Expand Down

0 comments on commit 5edb52b

Please sign in to comment.