Skip to content

Commit

Permalink
Merge pull request #6150 from avakhov/dbconsole-fixes
Browse files Browse the repository at this point in the history
Rails db console improvements
  • Loading branch information
drogus committed May 22, 2012
2 parents 1447aca + 1fed1f1 commit d22859e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion railties/lib/rails/commands/console.rb
Expand Up @@ -22,7 +22,7 @@ def options
options = {}

OptionParser.new do |opt|
opt.banner = "Usage: console [environment] [options]"
opt.banner = "Usage: rails console [environment] [options]"
opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v }
opt.on("-e", "--environment=name", String,
"Specifies the environment to run this console under (test/development/production).",
Expand Down
9 changes: 7 additions & 2 deletions railties/lib/rails/commands/dbconsole.rb
Expand Up @@ -42,7 +42,7 @@ def start
include_password = false
options = {}
OptionParser.new do |opt|
opt.banner = "Usage: dbconsole [environment] [options]"
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
Expand All @@ -56,6 +56,11 @@ def start
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
Expand Down Expand Up @@ -96,7 +101,7 @@ def start

args << "-#{options['mode']}" if options['mode']
args << "-header" if options['header']
args << config['database']
args << File.expand_path(config['database'], Rails.root)

find_cmd_and_exec('sqlite3', *args)

Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/commands/runner.rb
Expand Up @@ -9,7 +9,7 @@
end

ARGV.clone.options do |opts|
opts.banner = "Usage: runner [options] ('Some.ruby(code)' or a filename)"
opts.banner = "Usage: rails runner [options] ('Some.ruby(code)' or a filename)"

opts.separator ""

Expand Down
35 changes: 29 additions & 6 deletions railties/test/commands/dbconsole_test.rb
Expand Up @@ -92,20 +92,25 @@ def test_sqlite
end

def test_sqlite3
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', 'db')
start(adapter: 'sqlite3', database: 'db')
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', Rails.root.join('db.sqlite3').to_s)
start(adapter: 'sqlite3', database: 'db.sqlite3')
assert !aborted
end

def test_sqlite3_mode
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-html', 'db')
start({adapter: 'sqlite3', database: 'db'}, ['--mode', 'html'])
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-html', Rails.root.join('db.sqlite3').to_s)
start({adapter: 'sqlite3', database: 'db.sqlite3'}, ['--mode', 'html'])
assert !aborted
end

def test_sqlite3_header
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-header', 'db')
start({adapter: 'sqlite3', database: 'db'}, ['--header'])
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-header', Rails.root.join('db.sqlite3').to_s)
start({adapter: 'sqlite3', database: 'db.sqlite3'}, ['--header'])
end

def test_sqlite3_db_absolute_path
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '/tmp/db.sqlite3')
start(adapter: 'sqlite3', database: '/tmp/db.sqlite3')
assert !aborted
end

Expand All @@ -127,6 +132,24 @@ def test_unknown_command_line_client
assert_match /Unknown command-line client for db/, output
end

def test_print_help_short
stdout = capture(:stdout) do
start({}, ['-h'])
end
assert aborted
assert_equal '', output
assert_match /Usage:.*dbconsole/, stdout
end

def test_print_help_long
stdout = capture(:stdout) do
start({}, ['--help'])
end
assert aborted
assert_equal '', output
assert_match /Usage:.*dbconsole/, stdout
end

private
attr_reader :aborted, :output

Expand Down

0 comments on commit d22859e

Please sign in to comment.