Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if ( action == 'up' )
{
executeUp();
}
else
{
executeDown();
}
break;
case 'db':
loadConfig();
if( folder.length < 1 )
{
log.info( 'Please enter a valid command, i.e. db:create|db:drop' );
}
else
{
global.mode = folder[ 1 ];
executeDB();
}
break;
default:
log.error( 'Invalid Action: Must be [up|down|create].' );
optimist.showHelp();
process.exit( 1 );
break;
}
}
if ( argv.help || argv._.length === 0 )
{
optimist.showHelp();
process.exit( 1 );
}
global.migrationTable = argv.table;
global.dbm = dbm;
global.matching = '';
global.mode;
global.verbose = argv.verbose;
global.dryRun = argv[ 'dry-run' ];
if ( global.dryRun )
{
log.info( 'dry run' );
}
function connect( config, callback )
{
if ( argv.template !== true )
config.template = argv.template || config.template;
template.connect( config, function ( err, tmp )
{
if( err )
{
callback( err );
return;
}
driver.connect( config, function ( err, db )
function executeDown( callback )
{
if ( !argv.count )
{
log.info( 'Defaulting to running 1 down migration.' );
argv.count = 1;
}
index.connect( config.getCurrent().settings, function ( err, migrator )
{
assert.ifError( err );
migrator.migrationsDir = path.resolve( argv[ 'migrations-dir' ] );
migrator.driver.createMigrationsTable( function ( err )
{
assert.ifError( err );
if ( typeof ( callback ) === 'function' )
callback( null, onComplete.bind( this, migrator ), function ( callback )
{
migrator.down( argv, callback );
} );
else
migrator.down( argv, onComplete.bind( this, migrator ) );
db.createDatabase( argv.dbname, { ifNotExists: true }, function()
{
if( err )
{
log.info( 'Error: Failed to create database!' );
}
else
{
log.info( 'Created database "' + argv.dbname + '"' );
}
db.close();
} );
}
function executeDB() {
if( argv._.length > 0 )
{
argv.dbname = argv._.shift().toString();
}
else
{
log.info( 'Error: You must enter a database name!' );
return;
}
index.driver( config.getCurrent().settings, function( err, db )
{
if( global.mode === 'create' )
{
db.createDatabase( argv.dbname, { ifNotExists: true }, function()
{
if( err )
{
log.info( 'Error: Failed to create database!' );
}
else
{
log.info( 'Created database "' + argv.dbname + '"' );
config.loadUrl( process.env.DATABASE_URL, argv.env );
}
else
{
config.load( argv.config, argv.env );
}
if ( verbose )
{
var current = config.getCurrent();
var s = JSON.parse( JSON.stringify( current.settings ) );
if ( s.password )
s.password = '******';
log.info( 'Using', current.env, 'settings:', s );
}
}
db.dropDatabase( argv.dbname, { ifExists: true }, function()
{
if( err )
{
log.info( 'Error: Failed to drop database!' );
}
else
{
log.info( 'Deleted database "' + argv.dbname + '"' );
}
db.close();
} );
}