More faster rails dbconsole

This commit is contained in:
Dmitry Vorotilin 2012-05-04 18:40:32 +04:00
parent ed1703bcb2
commit 346bb01849
3 changed files with 86 additions and 40 deletions

View File

@ -57,8 +57,7 @@ when 'server'
when 'dbconsole' when 'dbconsole'
require 'rails/commands/dbconsole' require 'rails/commands/dbconsole'
require APP_PATH Rails::DBConsole.start
Rails::DBConsole.start(Rails.application)
when 'application', 'runner' when 'application', 'runner'
require "rails/commands/#{command}" require "rails/commands/#{command}"

View File

@ -5,15 +5,37 @@ require 'rbconfig'
module Rails module Rails
class DBConsole class DBConsole
attr_reader :arguments attr_reader :arguments, :config
def self.start(app) def self.start
new(app).start new(config).start
end end
def initialize(app, arguments = ARGV) def self.config
@app = app config = begin
@arguments = arguments 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
end end
def start def start
@ -38,10 +60,6 @@ module Rails
abort opt.to_s unless (0..1).include?(arguments.size) abort opt.to_s unless (0..1).include?(arguments.size)
end end
unless config = @app.config.database_configuration[Rails.env]
abort "No database is configured for the environment '#{Rails.env}'"
end
case config["adapter"] case config["adapter"]
when /^mysql/ when /^mysql/

View File

@ -3,42 +3,70 @@ require 'rails/commands/dbconsole'
class Rails::DBConsoleTest < ActiveSupport::TestCase class Rails::DBConsoleTest < ActiveSupport::TestCase
def teardown def teardown
%w[PGUSER PGHOST PGPORT PGPASSWORD'].each{|key| ENV.delete(key)} %w[PGUSER PGHOST PGPORT PGPASSWORD].each{|key| ENV.delete(key)}
end end
def test_no_database_configured def test_config
start [], false Rails::DBConsole.const_set(:APP_PATH, "erb")
app_config({})
capture_abort { Rails::DBConsole.config }
assert aborted assert aborted
assert_match /No database is configured for the environment '\w+'/, output assert_match /No database is configured for the environment '\w+'/, output
app_config(development: "with_init")
assert_equal Rails::DBConsole.config, "with_init"
app_db_file("development:\n without_init")
assert_equal Rails::DBConsole.config, "without_init"
app_db_file("development:\n <%= Rails.something_app_specific %>")
assert_equal Rails::DBConsole.config, "with_init"
app_db_file("development:\n\ninvalid")
assert_equal Rails::DBConsole.config, "with_init"
end
def test_env
assert_equal Rails::DBConsole.env, "development"
Rails.stubs(:respond_to?).with(:env).returns(false)
assert_equal Rails::DBConsole.env, "development"
ENV['RACK_ENV'] = "rack_env"
assert_equal Rails::DBConsole.env, "rack_env"
ENV['RAILS_ENV'] = "rails_env"
assert_equal Rails::DBConsole.env, "rails_env"
end end
def test_mysql def test_mysql
dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], 'db') dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], 'db')
start [], {adapter: 'mysql', database: 'db'} start(adapter: 'mysql', database: 'db')
assert !aborted assert !aborted
end end
def test_mysql_full def test_mysql_full
dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--host=locahost', '--port=1234', '--socket=socket', '--user=user', '--default-character-set=UTF-8', '-p', 'db') dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--host=locahost', '--port=1234', '--socket=socket', '--user=user', '--default-character-set=UTF-8', '-p', 'db')
start [], {adapter: 'mysql', database: 'db', host: 'locahost', port: 1234, socket: 'socket', username: 'user', password: 'qwerty', encoding: 'UTF-8'} start(adapter: 'mysql', database: 'db', host: 'locahost', port: 1234, socket: 'socket', username: 'user', password: 'qwerty', encoding: 'UTF-8')
assert !aborted assert !aborted
end end
def test_mysql_include_password def test_mysql_include_password
dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--user=user', '--password=qwerty', 'db') dbconsole.expects(:find_cmd_and_exec).with(%w[mysql mysql5], '--user=user', '--password=qwerty', 'db')
start ['-p'], {adapter: 'mysql', database: 'db', username: 'user', password: 'qwerty'} start({adapter: 'mysql', database: 'db', username: 'user', password: 'qwerty'}, ['-p'])
assert !aborted assert !aborted
end end
def test_postgresql def test_postgresql
dbconsole.expects(:find_cmd_and_exec).with('psql', 'db') dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
start [], {adapter: 'postgresql', database: 'db'} start(adapter: 'postgresql', database: 'db')
assert !aborted assert !aborted
end end
def test_postgresql_full def test_postgresql_full
dbconsole.expects(:find_cmd_and_exec).with('psql', 'db') dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
start [], {adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3', host: 'host', port: 5432} start(adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3', host: 'host', port: 5432)
assert !aborted assert !aborted
assert_equal 'user', ENV['PGUSER'] assert_equal 'user', ENV['PGUSER']
assert_equal 'host', ENV['PGHOST'] assert_equal 'host', ENV['PGHOST']
@ -48,7 +76,7 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
def test_postgresql_include_password def test_postgresql_include_password
dbconsole.expects(:find_cmd_and_exec).with('psql', 'db') dbconsole.expects(:find_cmd_and_exec).with('psql', 'db')
start ['-p'], {adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3'} start({adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3'}, ['-p'])
assert !aborted assert !aborted
assert_equal 'user', ENV['PGUSER'] assert_equal 'user', ENV['PGUSER']
assert_equal 'q1w2e3', ENV['PGPASSWORD'] assert_equal 'q1w2e3', ENV['PGPASSWORD']
@ -56,42 +84,42 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
def test_sqlite def test_sqlite
dbconsole.expects(:find_cmd_and_exec).with('sqlite', 'db') dbconsole.expects(:find_cmd_and_exec).with('sqlite', 'db')
start [], {adapter: 'sqlite', database: 'db'} start(adapter: 'sqlite', database: 'db')
assert !aborted assert !aborted
end end
def test_sqlite3 def test_sqlite3
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', 'db') dbconsole.expects(:find_cmd_and_exec).with('sqlite3', 'db')
start [], {adapter: 'sqlite3', database: 'db'} start(adapter: 'sqlite3', database: 'db')
assert !aborted assert !aborted
end end
def test_sqlite3_mode def test_sqlite3_mode
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-html', 'db') dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-html', 'db')
start ['--mode', 'html'], {adapter: 'sqlite3', database: 'db'} start({adapter: 'sqlite3', database: 'db'}, ['--mode', 'html'])
assert !aborted assert !aborted
end end
def test_sqlite3_header def test_sqlite3_header
dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-header', 'db') dbconsole.expects(:find_cmd_and_exec).with('sqlite3', '-header', 'db')
start ['--header'], {adapter: 'sqlite3', database: 'db'} start({adapter: 'sqlite3', database: 'db'}, ['--header'])
assert !aborted assert !aborted
end end
def test_oracle def test_oracle
dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user@db') dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user@db')
start [], {adapter: 'oracle', database: 'db', username: 'user', password: 'secret'} start(adapter: 'oracle', database: 'db', username: 'user', password: 'secret')
assert !aborted assert !aborted
end end
def test_oracle_include_password def test_oracle_include_password
dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user/secret@db') dbconsole.expects(:find_cmd_and_exec).with('sqlplus', 'user/secret@db')
start ['-p'], {adapter: 'oracle', database: 'db', username: 'user', password: 'secret'} start({adapter: 'oracle', database: 'db', username: 'user', password: 'secret'}, ['-p'])
assert !aborted assert !aborted
end end
def test_unknown_command_line_client def test_unknown_command_line_client
start [], {adapter: 'unknown', database: 'db'} start(adapter: 'unknown', database: 'db')
assert aborted assert aborted
assert_match /Unknown command-line client for db/, output assert_match /Unknown command-line client for db/, output
end end
@ -100,29 +128,30 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase
attr_reader :aborted, :output attr_reader :aborted, :output
def dbconsole def dbconsole
@dbconsole ||= Rails::DBConsole.new(app) @dbconsole ||= Rails::DBConsole.new(nil)
end end
def start(argv = [], database_configuration = {}) def start(config = {}, argv = [])
dbconsole.stubs(arguments: argv) dbconsole.stubs(config: config.stringify_keys, arguments: argv)
app.config.stubs(database_configuration: { capture_abort { dbconsole.start }
Rails.env => database_configuration ? database_configuration.stringify_keys : database_configuration end
})
def capture_abort
@aborted = false @aborted = false
@output = capture(:stderr) do @output = capture(:stderr) do
begin begin
dbconsole.start yield
rescue SystemExit rescue SystemExit
@aborted = true @aborted = true
end end
end end
end end
def app def app_db_file(result)
@app ||= begin IO.stubs(:read).with("config/database.yml").returns(result)
config = mock("config")
stub("app", config: config)
end end
def app_config(result)
Rails.application.config.stubs(:database_configuration).returns(result.stringify_keys)
end end
end end