2012-04-27 03:28:53 -04:00
|
|
|
require 'abstract_unit'
|
2014-07-15 22:36:45 -04:00
|
|
|
require 'minitest/mock'
|
2012-04-27 03:28:53 -04:00
|
|
|
require 'rails/commands/dbconsole'
|
|
|
|
|
|
|
|
class Rails::DBConsoleTest < ActiveSupport::TestCase
|
|
|
|
|
2012-05-04 10:40:32 -04:00
|
|
|
|
2013-12-15 01:07:34 -05:00
|
|
|
def setup
|
|
|
|
Rails::DBConsole.const_set('APP_PATH', 'rails/all')
|
|
|
|
end
|
2012-05-04 10:40:32 -04:00
|
|
|
|
2013-12-15 01:07:34 -05:00
|
|
|
def teardown
|
|
|
|
Rails::DBConsole.send(:remove_const, 'APP_PATH')
|
|
|
|
%w[PGUSER PGHOST PGPORT PGPASSWORD DATABASE_URL].each{|key| ENV.delete(key)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_with_db_config_only
|
|
|
|
config_sample = {
|
|
|
|
"test"=> {
|
|
|
|
"adapter"=> "sqlite3",
|
|
|
|
"host"=> "localhost",
|
|
|
|
"port"=> "9000",
|
|
|
|
"database"=> "foo_test",
|
|
|
|
"user"=> "foo",
|
|
|
|
"password"=> "bar",
|
|
|
|
"pool"=> "5",
|
|
|
|
"timeout"=> "3000"
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 22:36:45 -04:00
|
|
|
app_db_config(config_sample) do
|
2014-08-26 11:53:19 -04:00
|
|
|
assert_equal config_sample["test"], Rails::DBConsole.new.config
|
2014-07-15 22:36:45 -04:00
|
|
|
end
|
2013-12-15 01:07:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_with_no_db_config
|
2014-07-15 22:36:45 -04:00
|
|
|
app_db_config(nil) do
|
|
|
|
assert_raise(ActiveRecord::AdapterNotSpecified) {
|
|
|
|
Rails::DBConsole.new.config
|
|
|
|
}
|
|
|
|
end
|
2013-12-15 01:07:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_with_database_url_only
|
2013-12-21 23:09:01 -05:00
|
|
|
ENV['DATABASE_URL'] = 'postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000'
|
|
|
|
expected = {
|
|
|
|
"adapter" => "postgresql",
|
|
|
|
"host" => "localhost",
|
|
|
|
"port" => 9000,
|
|
|
|
"database" => "foo_test",
|
|
|
|
"username" => "foo",
|
|
|
|
"password" => "bar",
|
|
|
|
"pool" => "5",
|
|
|
|
"timeout" => "3000"
|
2013-12-15 01:07:34 -05:00
|
|
|
}.sort
|
2014-07-15 22:36:45 -04:00
|
|
|
|
|
|
|
app_db_config(nil) do
|
|
|
|
assert_equal expected, Rails::DBConsole.new.config.sort
|
|
|
|
end
|
2013-12-15 01:07:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_config_choose_database_url_if_exists
|
2013-12-21 23:09:01 -05:00
|
|
|
host = "database-url-host.com"
|
|
|
|
ENV['DATABASE_URL'] = "postgresql://foo:bar@#{host}:9000/foo_test?pool=5&timeout=3000"
|
2013-12-15 01:07:34 -05:00
|
|
|
sample_config = {
|
|
|
|
"test" => {
|
2013-12-21 23:09:01 -05:00
|
|
|
"adapter" => "postgresql",
|
|
|
|
"host" => "not-the-#{host}",
|
|
|
|
"port" => 9000,
|
|
|
|
"database" => "foo_test",
|
|
|
|
"username" => "foo",
|
|
|
|
"password" => "bar",
|
|
|
|
"pool" => "5",
|
|
|
|
"timeout" => "3000"
|
2013-12-15 01:07:34 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-15 22:36:45 -04:00
|
|
|
app_db_config(sample_config) do
|
|
|
|
assert_equal host, Rails::DBConsole.new.config["host"]
|
|
|
|
end
|
2012-05-04 10:40:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_env
|
2014-08-26 11:53:19 -04:00
|
|
|
assert_equal "test", Rails::DBConsole.new.environment
|
2012-05-11 17:19:58 -04:00
|
|
|
|
|
|
|
ENV['RAILS_ENV'] = nil
|
2015-03-20 11:14:11 -04:00
|
|
|
ENV['RACK_ENV'] = nil
|
2012-05-04 10:40:32 -04:00
|
|
|
|
2014-07-15 22:36:45 -04:00
|
|
|
Rails.stub(:respond_to?, false) do
|
2014-08-26 11:53:19 -04:00
|
|
|
assert_equal "development", Rails::DBConsole.new.environment
|
2012-05-04 10:40:32 -04:00
|
|
|
|
2015-03-20 11:14:11 -04:00
|
|
|
ENV['RACK_ENV'] = "rack_env"
|
|
|
|
assert_equal "rack_env", Rails::DBConsole.new.environment
|
|
|
|
|
2014-07-15 22:36:45 -04:00
|
|
|
ENV['RAILS_ENV'] = "rails_env"
|
2014-08-26 11:53:19 -04:00
|
|
|
assert_equal "rails_env", Rails::DBConsole.new.environment
|
2014-07-15 22:36:45 -04:00
|
|
|
end
|
2012-05-07 00:38:34 -04:00
|
|
|
ensure
|
|
|
|
ENV['RAILS_ENV'] = "test"
|
2015-03-20 11:14:11 -04:00
|
|
|
ENV['RACK_ENV'] = nil
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
2012-12-27 10:04:54 -05:00
|
|
|
def test_rails_env_is_development_when_argument_is_dev
|
2014-07-15 22:36:45 -04:00
|
|
|
dbconsole = Rails::DBConsole.new
|
|
|
|
|
|
|
|
dbconsole.stub(:available_environments, ['development', 'test']) do
|
|
|
|
options = dbconsole.send(:parse_arguments, ['dev'])
|
|
|
|
assert_match('development', options[:environment])
|
|
|
|
end
|
2012-12-27 10:04:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
|
2014-07-15 22:36:45 -04:00
|
|
|
dbconsole = Rails::DBConsole.new
|
|
|
|
|
|
|
|
dbconsole.stub(:available_environments, ['dev']) do
|
|
|
|
options = dbconsole.send(:parse_arguments, ['dev'])
|
|
|
|
assert_match('dev', options[:environment])
|
|
|
|
end
|
2012-12-27 10:04:54 -05:00
|
|
|
end
|
|
|
|
|
2012-04-27 03:28:53 -04:00
|
|
|
def test_mysql
|
2012-05-04 10:40:32 -04:00
|
|
|
start(adapter: 'mysql', database: 'db')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal [%w[mysql mysql5], 'db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mysql_full
|
2012-05-04 10:40:32 -04:00
|
|
|
start(adapter: 'mysql', database: 'db', host: 'locahost', port: 1234, socket: 'socket', username: 'user', password: 'qwerty', encoding: 'UTF-8')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal [%w[mysql mysql5], '--host=locahost', '--port=1234', '--socket=socket', '--user=user', '--default-character-set=UTF-8', '-p', 'db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mysql_include_password
|
2012-05-04 10:40:32 -04:00
|
|
|
start({adapter: 'mysql', database: 'db', username: 'user', password: 'qwerty'}, ['-p'])
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal [%w[mysql mysql5], '--user=user', '--password=qwerty', 'db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_postgresql
|
2012-05-04 10:40:32 -04:00
|
|
|
start(adapter: 'postgresql', database: 'db')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['psql', 'db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_postgresql_full
|
2012-05-04 10:40:32 -04:00
|
|
|
start(adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3', host: 'host', port: 5432)
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['psql', 'db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
assert_equal 'user', ENV['PGUSER']
|
|
|
|
assert_equal 'host', ENV['PGHOST']
|
|
|
|
assert_equal '5432', ENV['PGPORT']
|
|
|
|
assert_not_equal 'q1w2e3', ENV['PGPASSWORD']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_postgresql_include_password
|
2012-05-04 10:40:32 -04:00
|
|
|
start({adapter: 'postgresql', database: 'db', username: 'user', password: 'q1w2e3'}, ['-p'])
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['psql', 'db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
assert_equal 'user', ENV['PGUSER']
|
|
|
|
assert_equal 'q1w2e3', ENV['PGPASSWORD']
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sqlite
|
2012-05-04 10:40:32 -04:00
|
|
|
start(adapter: 'sqlite', database: 'db')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['sqlite', 'db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sqlite3
|
2012-05-04 04:54:20 -04:00
|
|
|
start(adapter: 'sqlite3', database: 'db.sqlite3')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['sqlite3', Rails.root.join('db.sqlite3').to_s], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sqlite3_mode
|
2012-05-04 04:54:20 -04:00
|
|
|
start({adapter: 'sqlite3', database: 'db.sqlite3'}, ['--mode', 'html'])
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['sqlite3', '-html', Rails.root.join('db.sqlite3').to_s], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sqlite3_header
|
2012-05-04 04:54:20 -04:00
|
|
|
start({adapter: 'sqlite3', database: 'db.sqlite3'}, ['--header'])
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['sqlite3', '-header', Rails.root.join('db.sqlite3').to_s], dbconsole.find_cmd_and_exec_args
|
2012-05-04 04:54:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sqlite3_db_absolute_path
|
|
|
|
start(adapter: 'sqlite3', database: '/tmp/db.sqlite3')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['sqlite3', '/tmp/db.sqlite3'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
Fix rails db command with sqlite3 database
When using sqlite3 it was attempting to find the database file based on
Rails.root, the problem is that Rails.root is not always present because
we try to first manually load "config/database.yml" instead of loading
the entire app, to make "rails db" faster.
This means that when we're in the root path of the app, calling "rails db"
won't allow us to use Rails.root, making the command fail for sqlite3
with the error:
./rails/commands/dbconsole.rb:62:in `start':
undefined method `root' for Rails:Module (NoMethodError)
The fix is to simply not pass any dir string to File.expand_path, which
will make it use the current directory of the process as base, or the
root path of the app, which is what we want.
When we are in any other subdirectory, calling "rails db" should work
just fine, because "config/database.yml" won't be found, thus "rails db"
will fallback to loading the app, making Rails.root available.
Closes #8257.
2012-11-18 20:46:33 -05:00
|
|
|
def test_sqlite3_db_without_defined_rails_root
|
2014-07-15 22:36:45 -04:00
|
|
|
Rails.stub(:respond_to?, false) do
|
|
|
|
start(adapter: 'sqlite3', database: 'config/db.sqlite3')
|
|
|
|
assert !aborted
|
|
|
|
assert_equal ['sqlite3', Rails.root.join('../config/db.sqlite3').to_s], dbconsole.find_cmd_and_exec_args
|
|
|
|
end
|
Fix rails db command with sqlite3 database
When using sqlite3 it was attempting to find the database file based on
Rails.root, the problem is that Rails.root is not always present because
we try to first manually load "config/database.yml" instead of loading
the entire app, to make "rails db" faster.
This means that when we're in the root path of the app, calling "rails db"
won't allow us to use Rails.root, making the command fail for sqlite3
with the error:
./rails/commands/dbconsole.rb:62:in `start':
undefined method `root' for Rails:Module (NoMethodError)
The fix is to simply not pass any dir string to File.expand_path, which
will make it use the current directory of the process as base, or the
root path of the app, which is what we want.
When we are in any other subdirectory, calling "rails db" should work
just fine, because "config/database.yml" won't be found, thus "rails db"
will fallback to loading the app, making Rails.root available.
Closes #8257.
2012-11-18 20:46:33 -05:00
|
|
|
end
|
|
|
|
|
2012-04-27 03:28:53 -04:00
|
|
|
def test_oracle
|
2012-05-04 10:40:32 -04:00
|
|
|
start(adapter: 'oracle', database: 'db', username: 'user', password: 'secret')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['sqlplus', 'user@db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_oracle_include_password
|
2012-05-04 10:40:32 -04:00
|
|
|
start({adapter: 'oracle', database: 'db', username: 'user', password: 'secret'}, ['-p'])
|
2012-04-27 03:28:53 -04:00
|
|
|
assert !aborted
|
2014-07-15 22:36:45 -04:00
|
|
|
assert_equal ['sqlplus', 'user/secret@db'], dbconsole.find_cmd_and_exec_args
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_unknown_command_line_client
|
2012-05-04 10:40:32 -04:00
|
|
|
start(adapter: 'unknown', database: 'db')
|
2012-04-27 03:28:53 -04:00
|
|
|
assert aborted
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/Unknown command-line client for db/, output)
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
2012-05-04 14:04:53 -04:00
|
|
|
def test_print_help_short
|
|
|
|
stdout = capture(:stdout) do
|
|
|
|
start({}, ['-h'])
|
|
|
|
end
|
|
|
|
assert aborted
|
|
|
|
assert_equal '', output
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/Usage:.*dbconsole/, stdout)
|
2012-05-04 14:04:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_print_help_long
|
|
|
|
stdout = capture(:stdout) do
|
|
|
|
start({}, ['--help'])
|
|
|
|
end
|
|
|
|
assert aborted
|
|
|
|
assert_equal '', output
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/Usage:.*dbconsole/, stdout)
|
2012-05-04 14:04:53 -04:00
|
|
|
end
|
|
|
|
|
2012-04-27 03:28:53 -04:00
|
|
|
attr_reader :aborted, :output
|
2013-04-04 13:53:46 -04:00
|
|
|
private :aborted, :output
|
|
|
|
|
|
|
|
private
|
2012-04-27 03:28:53 -04:00
|
|
|
|
2013-12-15 01:07:34 -05:00
|
|
|
def app_db_config(results)
|
2014-07-15 22:36:45 -04:00
|
|
|
Rails.application.config.stub(:database_configuration, results || {}) do
|
|
|
|
yield
|
|
|
|
end
|
2013-12-15 01:07:34 -05:00
|
|
|
end
|
|
|
|
|
2012-04-27 03:28:53 -04:00
|
|
|
def dbconsole
|
2014-07-15 22:36:45 -04:00
|
|
|
@dbconsole ||= Class.new(Rails::DBConsole) do
|
|
|
|
attr_reader :find_cmd_and_exec_args
|
|
|
|
|
|
|
|
def find_cmd_and_exec(*args)
|
|
|
|
@find_cmd_and_exec_args = args
|
|
|
|
end
|
|
|
|
end.new(nil)
|
2012-04-27 03:28:53 -04:00
|
|
|
end
|
|
|
|
|
2012-05-04 10:40:32 -04:00
|
|
|
def start(config = {}, argv = [])
|
2014-07-15 22:36:45 -04:00
|
|
|
dbconsole.stub(:config, config.stringify_keys) do
|
|
|
|
dbconsole.stub(:arguments, argv) do
|
|
|
|
capture_abort { dbconsole.start }
|
|
|
|
end
|
|
|
|
end
|
2012-05-04 10:40:32 -04:00
|
|
|
end
|
2012-04-27 03:28:53 -04:00
|
|
|
|
2012-05-04 10:40:32 -04:00
|
|
|
def capture_abort
|
2012-04-27 03:28:53 -04:00
|
|
|
@aborted = false
|
2012-05-07 00:38:34 -04:00
|
|
|
@output = capture(:stderr) do
|
2012-04-27 03:28:53 -04:00
|
|
|
begin
|
2012-05-04 10:40:32 -04:00
|
|
|
yield
|
2012-04-27 03:28:53 -04:00
|
|
|
rescue SystemExit
|
|
|
|
@aborted = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|