Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/codeinvain/sequelize into…
Browse files Browse the repository at this point in the history
… codeinvain-master
  • Loading branch information
sdepold committed Apr 5, 2014
2 parents eb03433 + 8cc1fbf commit 121aefa
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
39 changes: 38 additions & 1 deletion bin/sequelize
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ var configuration = {
migrationsPath: process.cwd() + '/migrations'
}

var loadDefaultOptions = function(path){
try{
options = require(process.cwd()+'/'+path);
for (var key in options){
program[key] = options[key];
}
}catch(e){
console.log(e);
throw new Error('Error reading "' + path + '".')
}
}

var configFileExists = function() {
return fs.existsSync(configuration.configFile)
}
Expand Down Expand Up @@ -82,12 +94,26 @@ var createMigrationsFolder = function(force) {
}

try {
fs.mkdirSync(configuration.migrationsPath)
mkdirp(configuration.migrationsPath);
console.log('Successfully created migrations folder at "' + configuration.migrationsPath + '".')
} catch(e) {
}
}

var mkdirp = function (path, root) {

var dirs = path.split('/'), dir = dirs.shift(), root = (root||'')+dir+'/';

try { fs.mkdirSync(root); }
catch (e) {
//dir wasn't made, something went wrong
if(!fs.statSync(root).isDirectory()) throw new Error(e);
}

return !dirs.length||mkdirp(dirs.join('/'), root);
}


var parseDbUrl = function(urlString) {
var urlParts,
config = {};
Expand Down Expand Up @@ -155,11 +181,17 @@ program
.option('-u, --undo', 'Undo the last migration.')
.option('-f, --force', 'Forces the action to be done.')
.option('-c, --create-migration [migration-name]', 'Creates a new migration.')
.option('-p, --migrations-path <migration-path>', 'migration directory path.')
.option('-U, --url <url>', 'Database url. An alternative to a config file')
.option('-o,--cli-options <options_file>', 'Specifies lib options from file.')
.option('--config <config_file>', 'Specifies alternate config file.')
.option('--coffee', 'Consider coffee script files as migration source.')
.parse(process.argv)

if(typeof program.cliOptions === 'string') {
loadDefaultOptions(program.cliOptions);
}

if(typeof program.config === 'string') {
if (isRelativePath(program.config)) {
configuration.configFile = path.join(process.cwd(), program.config);
Expand All @@ -168,6 +200,10 @@ if(typeof program.config === 'string') {
}
}

if(typeof program.migrationsPath=== 'string') {
configuration.migrationsPath = program.migrationsPath;
}

if(typeof program.env === 'string') {
configuration.environment = program.env
}
Expand Down Expand Up @@ -307,3 +343,4 @@ if (program.migrate || program.undo) {
} else {
console.log('No action specified. Try "sequelize --help" for usage information.')
}

6 changes: 6 additions & 0 deletions config/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"configFile": "./config/database.json",
"migrationsPath": "./db/migrate"
}


32 changes: 32 additions & 0 deletions test/sequelize.executable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ if (os.type().toLowerCase().indexOf('windows') === -1) {
})
})(['config', 'migrations'])

it("creates a custom migrations folder", function(done) {
exec("rm -rf ./*", { cwd: __dirname + '/tmp' }, function() {
exec("../../bin/sequelize --init -p ./db/migrate", { cwd: __dirname + '/tmp' }, function() {
exec("ls -ila", { cwd: __dirname + '/tmp' }, function(err, stdout) {
expect(stdout).to.include('db')
done()
})
})
})
})

it("creates a config.json file", function(done) {
exec("rm -rf ./*", { cwd: __dirname + '/tmp' }, function() {
exec("../../bin/sequelize --init", { cwd: __dirname + '/tmp' }, function() {
Expand All @@ -93,6 +104,8 @@ if (os.type().toLowerCase().indexOf('windows') === -1) {
})
})
})


})
})
})(['--init', '-i'])
Expand Down Expand Up @@ -435,6 +448,25 @@ if (os.type().toLowerCase().indexOf('windows') === -1) {
})
})
})(['--url', '-U'])

;(function(flags) {
flags.forEach(function(flag) {
describe(flag, function() {
it("using options file instead of cli switches", function(done) {
exec("rm -rf ./*", { cwd: __dirname + '/tmp' }, function() {
exec( "../../bin/sequelize --init "+flag+" ../../config/options.json" , { cwd: __dirname + '/tmp' }, function() {
exec("ls -ila", { cwd: __dirname + '/tmp' }, function(err, stdout) {
expect(stdout).to.include('db')
done()
})
})
})
})
});

})
})(['--cli-options', '-o'])

})

}

0 comments on commit 121aefa

Please sign in to comment.