不要怂,就是干,撸起袖子干!

Commit d068479f by Simon Townsend

url argument support (as opposed to a config file) for migrations

1 parent c0b75438
Showing with 35 additions and 2 deletions
...@@ -6,6 +6,7 @@ var path = require("path") ...@@ -6,6 +6,7 @@ var path = require("path")
, Sequelize = require(__dirname + '/../index') , Sequelize = require(__dirname + '/../index')
, moment = require("moment") , moment = require("moment")
, _ = Sequelize.Utils._ , _ = Sequelize.Utils._
, parseDatabaseUrl = require('parse-database-url')
var configuration = { var configuration = {
configFile: process.cwd() + '/config/config.json', configFile: process.cwd() + '/config/config.json',
...@@ -86,19 +87,50 @@ var createMigrationsFolder = function(force) { ...@@ -86,19 +87,50 @@ var createMigrationsFolder = function(force) {
} }
} }
var parseDbUrl = function(url) {
var parsed,
config;
try {
parsed = parseDatabaseUrl(url);
//var dialect = parsed.driver == 'postgrest' ? 'pg' : parsed.driver;
config = {
username : parsed.user || null,
password : parsed.password || null,
database : parsed.database || null,
host : parsed.host || null,
port : parsed.port || null,
dialect : parsed.driver || null
}
} catch (e) {
throw new Error('Error parsing url: ' + url);
}
return config
};
var readConfig = function() { var readConfig = function() {
var config var config
if (program.url) {
config = parseDbUrl(program.url);
} else {
try { try {
config = require(configuration.configFile); config = require(configuration.configFile);
} catch(e) { } catch(e) {
throw new Error('Error reading "' + relativeConfigFile() + '".') throw new Error('Error reading "' + relativeConfigFile() + '".')
} }
}
if(typeof config != 'object') { if(typeof config != 'object') {
throw new Error('Config must be an object: ' + relativeConfigFile()); throw new Error('Config must be an object: ' + relativeConfigFile());
} }
console.log('Loaded configuration file "' + relativeConfigFile() + '".') if (program.url) {
console.log('Parsed url ' + program.url);
} else {
console.log('Loaded configuration file "' + relativeConfigFile() + '".');
}
if (config[configuration.environment]) { if (config[configuration.environment]) {
console.log('Using environment "' + configuration.environment + '".') console.log('Using environment "' + configuration.environment + '".')
config = config[configuration.environment] config = config[configuration.environment]
...@@ -114,6 +146,7 @@ program ...@@ -114,6 +146,7 @@ program
.option('-u, --undo', 'Undo the last migration.') .option('-u, --undo', 'Undo the last migration.')
.option('-f, --force', 'Forces the action to be done.') .option('-f, --force', 'Forces the action to be done.')
.option('-c, --create-migration [migration-name]', 'Creates a new migration.') .option('-c, --create-migration [migration-name]', 'Creates a new migration.')
.option('-U, --url <url>', 'Database url. An alternative to a config file')
.option('--config <config_file>', 'Specifies alternate config file.') .option('--config <config_file>', 'Specifies alternate config file.')
.parse(process.argv) .parse(process.argv)
...@@ -130,7 +163,7 @@ if(typeof program.env === 'string') { ...@@ -130,7 +163,7 @@ if(typeof program.env === 'string') {
} }
if (program.migrate) { if (program.migrate) {
if (configFileExists()) { if (configFileExists() || program.url) {
var config var config
, options = {} , options = {}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!