Skip to content

Commit

Permalink
Fix various bugs with console arguments.
Browse files Browse the repository at this point in the history
Allow hyphens in environment names again.
  • Loading branch information
samoli committed May 30, 2012
1 parent c56ef67 commit cdd6d9b
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 125 deletions.
10 changes: 9 additions & 1 deletion railties/lib/rails/commands.rb
Expand Up @@ -36,9 +36,17 @@

when 'console'
require 'rails/commands/console'
options = Rails::Console.parse_arguments(ARGV)

# RAILS_ENV needs to be set before config/application is required
ENV['RAILS_ENV'] = options[:environment] if options[:environment]

# shift ARGV so IRB doesn't freak
ARGV.shift if ARGV.first && ARGV.first[0] != '-'

require APP_PATH
Rails.application.require_environment!
Rails::Console.start(Rails.application)
Rails::Console.start(Rails.application, options)

when 'server'
# Change to the application's path if there is no config.ru file in current dir.
Expand Down
48 changes: 25 additions & 23 deletions railties/lib/rails/commands/console.rb
Expand Up @@ -4,21 +4,12 @@

module Rails
class Console
attr_reader :options, :app, :console, :arguments

def self.start(*args)
new(*args).start
end

def initialize(app, arguments = ARGV)
@app = app
@arguments = arguments
app.load_console
@console = app.config.console || IRB
end
class << self
def start(*args)
new(*args).start
end

def options
@options ||= begin
def parse_arguments(arguments)
options = {}

OptionParser.new do |opt|
Expand All @@ -31,20 +22,38 @@ def options
opt.parse!(arguments)
end

if arguments.first && arguments.first[0] != '-'
env = arguments.first
options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
end

options
end
end

attr_reader :options, :app, :console

def initialize(app, options={})
@app = app
@options = options
app.load_console
@console = app.config.console || IRB
end

def sandbox?
options[:sandbox]
end

def environment
options[:environment] ||= ENV['RAILS_ENV'] || 'development'
end

def environment?
options[:environment]
environment
end

def set_environment!
Rails.env = options[:environment]
Rails.env = environment
end

def debugger?
Expand All @@ -53,9 +62,7 @@ def debugger?

def start
app.sandbox = sandbox?

require_debugger if debugger?

set_environment! if environment?

if sandbox?
Expand All @@ -82,8 +89,3 @@ def require_debugger
end
end
end

# Has to set the RAILS_ENV before config/application is required
if ARGV.first && !ARGV.first.index("-") && env = ARGV.shift # has to shift the env ARGV so IRB doesn't freak
ENV['RAILS_ENV'] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
end
137 changes: 75 additions & 62 deletions railties/lib/rails/commands/dbconsole.rb
Expand Up @@ -5,66 +5,19 @@

module Rails
class DBConsole
attr_reader :arguments, :config

attr_reader :config, :arguments
def self.start
new(config).start
new.start
end

def self.config
config = begin
YAML.load(ERB.new(IO.read("config/database.yml")).result)
rescue SyntaxError, StandardError
require APP_PATH
Rails.application.config.database_configuration
end

unless config[env]
abort "No database is configured for the environment '#{env}'"
end

config[env]
end

def self.env
if Rails.respond_to?(:env)
Rails.env
else
ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
end
end

def initialize(config, arguments = ARGV)
@config, @arguments = config, arguments
def initialize(arguments = ARGV)
@arguments = arguments
end

def start
include_password = false
options = {}
OptionParser.new do |opt|
opt.banner = "Usage: rails dbconsole [environment] [options]"
opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
include_password = true
end

opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
"Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
options['mode'] = mode
end

opt.on("--header") do |h|
options['header'] = h
end

opt.on("-h", "--help", "Show this help message.") do
puts opt
exit
end

opt.parse!(arguments)
abort opt.to_s unless (0..1).include?(arguments.size)
end

options = parse_arguments(arguments)
ENV['RAILS_ENV'] = options[:environment] || environment

case config["adapter"]
when /^mysql/
Expand All @@ -76,7 +29,7 @@ def start
'encoding' => '--default-character-set'
}.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact

if config['password'] && include_password
if config['password'] && options['include_password']
args << "--password=#{config['password']}"
elsif config['password'] && !config['password'].to_s.empty?
args << "-p"
Expand All @@ -90,7 +43,7 @@ def start
ENV['PGUSER'] = config["username"] if config["username"]
ENV['PGHOST'] = config["host"] if config["host"]
ENV['PGPORT'] = config["port"].to_s if config["port"]
ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password
ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && options['include_password']
find_cmd_and_exec('psql', config["database"])

when "sqlite"
Expand All @@ -110,7 +63,7 @@ def start

if config['username']
logon = config['username']
logon << "/#{config['password']}" if config['password'] && include_password
logon << "/#{config['password']}" if config['password'] && options['include_password']
logon << "@#{config['database']}" if config['database']
end

Expand All @@ -121,8 +74,73 @@ def start
end
end

def config
@config ||= begin
cfg = begin
cfg = YAML.load(ERB.new(IO.read("config/database.yml")).result)
rescue SyntaxError, StandardError
require APP_PATH
Rails.application.config.database_configuration
end

unless cfg[environment]
abort "No database is configured for the environment '#{environment}'"
end

cfg[environment]
end
end

def environment
if Rails.respond_to?(:env)
Rails.env
else
ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
end
end

protected

def parse_arguments(arguments)
options = {}

OptionParser.new do |opt|
opt.banner = "Usage: rails dbconsole [environment] [options]"
opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
options['include_password'] = true
end

opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
"Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
options['mode'] = mode
end

opt.on("--header") do |h|
options['header'] = h
end

opt.on("-h", "--help", "Show this help message.") do
puts opt
exit
end

opt.on("-e", "--environment=name", String,
"Specifies the environment to run this console under (test/development/production).",
"Default: development"
) { |v| options[:environment] = v.strip }

opt.parse!(arguments)
abort opt.to_s unless (0..1).include?(arguments.size)
end

if arguments.first && arguments.first[0] != '-'
env = arguments.first
options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
end

options
end

def find_cmd_and_exec(commands, *args)
commands = Array(commands)

Expand All @@ -145,8 +163,3 @@ def find_cmd_and_exec(commands, *args)
end
end
end

# Has to set the RAILS_ENV before config/application is required
if ARGV.first && !ARGV.first.index("-") && env = ARGV.first
ENV['RAILS_ENV'] = %w(production development test).detect {|e| e =~ /^#{env}/} || env
end

0 comments on commit cdd6d9b

Please sign in to comment.