diff options
author | Eileen Uchitelle <eileencodes@gmail.com> | 2018-08-16 15:49:18 -0400 |
---|---|---|
committer | Eileen Uchitelle <eileencodes@gmail.com> | 2018-08-30 10:06:45 -0400 |
commit | fdf3f0b9306ba8145e6e3acb84a50e5d23dfe48c (patch) | |
tree | 2ee1b54cfd2368f0ba582f05ed2182299bbdc9d8 /activerecord/test | |
parent | 3d2caab7dc92a13d4dd369678d5b4ce659df8e52 (diff) | |
download | rails-fdf3f0b9306ba8145e6e3acb84a50e5d23dfe48c.tar.gz rails-fdf3f0b9306ba8145e6e3acb84a50e5d23dfe48c.tar.bz2 rails-fdf3f0b9306ba8145e6e3acb84a50e5d23dfe48c.zip |
Refactors Active Record connection management
While the three-tier config makes it easier to define databases for
multiple database applications, it quickly became clear to offer full
support for multiple databases we need to change the way the connections
hash was handled.
A three-tier config means that when Rails needed to choose a default
configuration (in the case a user doesn't ask for a specific
configuration) it wasn't clear to Rails which the default was. I
[bandaid fixed this so the rake tasks could work](#32271) but that fix
wasn't correct because it actually doubled up the configuration hashes.
Instead of attemping to manipulate the hashes @tenderlove and I decided
that it made more sense if we converted the hashes to objects so we can
easily ask those object questions. In a three tier config like this:
```
development:
primary:
database: "my_primary_db"
animals:
database; "my_animals_db"
```
We end up with an object like this:
```
@configurations=[
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10
@env_name="development",@spec_name="primary",
@config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90
@env_name="development",@spec_name="animals",
@config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>
]>
```
The configurations setter takes the database configuration set by your
application and turns them into an
`ActiveRecord::DatabaseConfigurations` object that has one getter -
`@configurations` which is an array of all the database objects.
The configurations getter returns this object by default since it acts
like a hash in most of the cases we need. For example if you need to
access the default `development` database we can simply request it as we
did before:
```
ActiveRecord::Base.configurations["development"]
```
This will return primary development database configuration hash:
```
{ "database" => "my_primary_db" }
```
Internally all of Active Record has been converted to use the new
objects. I've built this to be backwards compatible but allow for
accessing the hash if needed for a deprecation period. To get the
original hash instead of the object you can either add `to_h` on the
configurations call or pass `legacy: true` to `configurations.
```
ActiveRecord::Base.configurations.to_h
=> { "development => { "database" => "my_primary_db" } }
ActiveRecord::Base.configurations(legacy: true)
=> { "development => { "database" => "my_primary_db" } }
```
The new configurations object allows us to iterate over the Active
Record configurations without losing the known environment or
specification name for that configuration. You can also select all the
configs for an env or env and spec. With this we can always ask
any object what environment it belongs to:
```
db_configs = ActiveRecord::Base.configurations.configurations_for("development")
=> #<ActiveRecord::DatabaseConfigurations:0x00007fd1acbdf800
@configurations=[
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10
@env_name="development",@spec_name="primary",
@config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>,
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbdea90
@env_name="development",@spec_name="animals",
@config={"adapter"=>"sqlite3", "database"=>"db/development.sqlite3"}>
]>
db_config.env_name
=> "development"
db_config.spec_name
=> "primary"
db_config.config
=> { "adapter"=>"sqlite3", "database"=>"db/development.sqlite3" }
```
The configurations object is more flexible than the configurations hash
and will allow us to build on top of the connection management in order
to add support for primary/replica connections, sharding, and
constructing queries for associations that live in multiple databases.
Diffstat (limited to 'activerecord/test')
6 files changed, 731 insertions, 74 deletions
diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index b8e623f17b..8c204a2692 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -28,13 +28,16 @@ module ActiveRecord end def test_establish_connection_uses_spec_name + old_config = ActiveRecord::Base.configurations config = { "readonly" => { "adapter" => "sqlite3" } } - resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(config) + ActiveRecord::Base.configurations = config + resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations) spec = resolver.spec(:readonly) @handler.establish_connection(spec.to_hash) assert_not_nil @handler.retrieve_connection_pool("readonly") ensure + ActiveRecord::Base.configurations = old_config @handler.remove_connection("readonly") end diff --git a/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb b/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb index 1b64324cc4..06c1c51724 100644 --- a/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +++ b/activerecord/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb @@ -18,11 +18,14 @@ module ActiveRecord end def resolve_config(config) - ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig.new(config).resolve + configs = ActiveRecord::DatabaseConfigurations.new(config) + configs.to_h end def resolve_spec(spec, config) - ConnectionSpecification::Resolver.new(resolve_config(config)).resolve(spec) + configs = ActiveRecord::DatabaseConfigurations.new(config) + resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(configs) + resolver.resolve(spec, spec) end def test_resolver_with_database_uri_and_current_env_symbol_key diff --git a/activerecord/test/cases/connection_specification/resolver_test.rb b/activerecord/test/cases/connection_specification/resolver_test.rb index 5b80f16a44..72be14f507 100644 --- a/activerecord/test/cases/connection_specification/resolver_test.rb +++ b/activerecord/test/cases/connection_specification/resolver_test.rb @@ -7,11 +7,15 @@ module ActiveRecord class ConnectionSpecification class ResolverTest < ActiveRecord::TestCase def resolve(spec, config = {}) - Resolver.new(config).resolve(spec) + configs = ActiveRecord::DatabaseConfigurations.new(config) + resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(configs) + resolver.resolve(spec, spec) end def spec(spec, config = {}) - Resolver.new(config).spec(spec) + configs = ActiveRecord::DatabaseConfigurations.new(config) + resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new(configs) + resolver.spec(spec) end def test_url_invalid_adapter diff --git a/activerecord/test/cases/legacy_configurations_test.rb b/activerecord/test/cases/legacy_configurations_test.rb new file mode 100644 index 0000000000..c36feb5116 --- /dev/null +++ b/activerecord/test/cases/legacy_configurations_test.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require "cases/helper" + +module ActiveRecord + class LegacyConfigurationsTest < ActiveRecord::TestCase + def test_can_turn_configurations_into_a_hash + assert ActiveRecord::Base.configurations.to_h.is_a?(Hash), "expected to be a hash but was not." + assert_equal ["arunit", "arunit2", "arunit_without_prepared_statements"].sort, ActiveRecord::Base.configurations.to_h.keys.sort + end + + def test_each_is_deprecated + assert_deprecated do + ActiveRecord::Base.configurations.each do |db_config| + assert_equal "primary", db_config.spec_name + end + end + end + + def test_first_is_deprecated + assert_deprecated do + db_config = ActiveRecord::Base.configurations.first + assert_equal "arunit", db_config.env_name + assert_equal "primary", db_config.spec_name + end + end + + def test_fetch_is_deprecated + assert_deprecated do + db_config = ActiveRecord::Base.configurations.fetch("arunit").first + assert_equal "arunit", db_config.env_name + assert_equal "primary", db_config.spec_name + end + end + + def test_values_are_deprecated + config_hashes = ActiveRecord::Base.configurations.configurations.map(&:config) + assert_deprecated do + assert_equal config_hashes, ActiveRecord::Base.configurations.values + end + end + end +end diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index ee53d576a1..a39a7371fe 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -156,20 +156,24 @@ module ActiveRecord class DatabaseTasksCreateAllTest < ActiveRecord::TestCase def setup + @old_configurations = ActiveRecord::Base.configurations @configurations = { "development" => { "database" => "my-db" } } $stdout, @original_stdout = StringIO.new, $stdout $stderr, @original_stderr = StringIO.new, $stderr + + ActiveRecord::Base.configurations = @configurations end def teardown $stdout, $stderr = @original_stdout, @original_stderr + ActiveRecord::Base.configurations = @old_configurations end def test_ignores_configurations_without_databases @configurations["development"]["database"] = nil - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do ActiveRecord::Tasks::DatabaseTasks.create_all end @@ -179,7 +183,7 @@ module ActiveRecord def test_ignores_remote_databases @configurations["development"]["host"] = "my.server.tld" - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do ActiveRecord::Tasks::DatabaseTasks.create_all end @@ -189,7 +193,7 @@ module ActiveRecord def test_warning_for_remote_databases @configurations["development"]["host"] = "my.server.tld" - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do ActiveRecord::Tasks::DatabaseTasks.create_all assert_match "This task only modifies local databases. my-db is on a remote host.", @@ -200,7 +204,7 @@ module ActiveRecord def test_creates_configurations_with_local_ip @configurations["development"]["host"] = "127.0.0.1" - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do ActiveRecord::Tasks::DatabaseTasks.create_all end @@ -210,7 +214,7 @@ module ActiveRecord def test_creates_configurations_with_local_host @configurations["development"]["host"] = "localhost" - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do ActiveRecord::Tasks::DatabaseTasks.create_all end @@ -220,40 +224,33 @@ module ActiveRecord def test_creates_configurations_with_blank_hosts @configurations["development"]["host"] = nil - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do ActiveRecord::Tasks::DatabaseTasks.create_all end end end - - private - - def with_stubbed_configurations_establish_connection - ActiveRecord::Base.stub(:configurations, @configurations) do - # To refrain from connecting to a newly created empty DB in - # sqlite3_mem tests - ActiveRecord::Base.connection_handler.stub( - :establish_connection, - nil - ) do - yield - end - end - end end class DatabaseTasksCreateCurrentTest < ActiveRecord::TestCase def setup + @old_configurations = ActiveRecord::Base.configurations + @configurations = { "development" => { "database" => "dev-db" }, "test" => { "database" => "test-db" }, - "production" => { "url" => "prod-db-url" } + "production" => { "url" => "abstract://prod-db-url" } } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations end def test_creates_current_environment_database - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -267,7 +264,7 @@ module ActiveRecord end def test_creates_current_environment_database_with_url - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -281,7 +278,7 @@ module ActiveRecord end def test_creates_test_and_development_databases_when_env_was_not_specified - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -301,7 +298,7 @@ module ActiveRecord old_env = ENV["RAILS_ENV"] ENV["RAILS_ENV"] = "development" - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -328,29 +325,27 @@ module ActiveRecord end end end - - private - - def with_stubbed_configurations_establish_connection - ActiveRecord::Base.stub(:configurations, @configurations) do - ActiveRecord::Base.stub(:establish_connection, nil) do - yield - end - end - end end class DatabaseTasksCreateCurrentThreeTierTest < ActiveRecord::TestCase def setup + @old_configurations = ActiveRecord::Base.configurations + @configurations = { "development" => { "primary" => { "database" => "dev-db" }, "secondary" => { "database" => "secondary-dev-db" } }, "test" => { "primary" => { "database" => "test-db" }, "secondary" => { "database" => "secondary-test-db" } }, - "production" => { "primary" => { "url" => "prod-db-url" }, "secondary" => { "url" => "secondary-prod-db-url" } } + "production" => { "primary" => { "url" => "abstract://prod-db-url" }, "secondary" => { "url" => "abstract://secondary-prod-db-url" } } } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations end def test_creates_current_environment_database - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -367,7 +362,7 @@ module ActiveRecord end def test_creates_current_environment_database_with_url - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -384,7 +379,7 @@ module ActiveRecord end def test_creates_test_and_development_databases_when_env_was_not_specified - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -406,7 +401,7 @@ module ActiveRecord old_env = ENV["RAILS_ENV"] ENV["RAILS_ENV"] = "development" - with_stubbed_configurations_establish_connection do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :create, @@ -439,16 +434,6 @@ module ActiveRecord end end end - - private - - def with_stubbed_configurations_establish_connection - ActiveRecord::Base.stub(:configurations, @configurations) do - ActiveRecord::Base.stub(:establish_connection, nil) do - yield - end - end - end end class DatabaseTasksDropTest < ActiveRecord::TestCase @@ -467,20 +452,24 @@ module ActiveRecord class DatabaseTasksDropAllTest < ActiveRecord::TestCase def setup + @old_configurations = ActiveRecord::Base.configurations @configurations = { development: { "database" => "my-db" } } $stdout, @original_stdout = StringIO.new, $stdout $stderr, @original_stderr = StringIO.new, $stderr + + ActiveRecord::Base.configurations = @configurations end def teardown $stdout, $stderr = @original_stdout, @original_stderr + ActiveRecord::Base.configurations = @old_configurations end def test_ignores_configurations_without_databases @configurations[:development]["database"] = nil - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do ActiveRecord::Tasks::DatabaseTasks.drop_all end @@ -490,7 +479,7 @@ module ActiveRecord def test_ignores_remote_databases @configurations[:development]["host"] = "my.server.tld" - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do ActiveRecord::Tasks::DatabaseTasks.drop_all end @@ -500,7 +489,7 @@ module ActiveRecord def test_warning_for_remote_databases @configurations[:development]["host"] = "my.server.tld" - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do ActiveRecord::Tasks::DatabaseTasks.drop_all assert_match "This task only modifies local databases. my-db is on a remote host.", @@ -511,7 +500,7 @@ module ActiveRecord def test_drops_configurations_with_local_ip @configurations[:development]["host"] = "127.0.0.1" - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do ActiveRecord::Tasks::DatabaseTasks.drop_all end @@ -521,7 +510,7 @@ module ActiveRecord def test_drops_configurations_with_local_host @configurations[:development]["host"] = "localhost" - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do ActiveRecord::Tasks::DatabaseTasks.drop_all end @@ -531,7 +520,7 @@ module ActiveRecord def test_drops_configurations_with_blank_hosts @configurations[:development]["host"] = nil - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do ActiveRecord::Tasks::DatabaseTasks.drop_all end @@ -541,15 +530,22 @@ module ActiveRecord class DatabaseTasksDropCurrentTest < ActiveRecord::TestCase def setup + @old_configurations = ActiveRecord::Base.configurations @configurations = { "development" => { "database" => "dev-db" }, "test" => { "database" => "test-db" }, - "production" => { "url" => "prod-db-url" } + "production" => { "url" => "abstract://prod-db-url" } } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations end def test_drops_current_environment_database - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with(ActiveRecord::Tasks::DatabaseTasks, :drop, ["database" => "test-db"]) do ActiveRecord::Tasks::DatabaseTasks.drop_current( @@ -560,7 +556,7 @@ module ActiveRecord end def test_drops_current_environment_database_with_url - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with(ActiveRecord::Tasks::DatabaseTasks, :drop, ["url" => "prod-db-url"]) do ActiveRecord::Tasks::DatabaseTasks.drop_current( @@ -571,7 +567,7 @@ module ActiveRecord end def test_drops_test_and_development_databases_when_env_was_not_specified - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :drop, @@ -591,7 +587,7 @@ module ActiveRecord old_env = ENV["RAILS_ENV"] ENV["RAILS_ENV"] = "development" - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :drop, @@ -612,15 +608,22 @@ module ActiveRecord class DatabaseTasksDropCurrentThreeTierTest < ActiveRecord::TestCase def setup + @old_configurations = ActiveRecord::Base.configurations @configurations = { "development" => { "primary" => { "database" => "dev-db" }, "secondary" => { "database" => "secondary-dev-db" } }, "test" => { "primary" => { "database" => "test-db" }, "secondary" => { "database" => "secondary-test-db" } }, - "production" => { "primary" => { "url" => "prod-db-url" }, "secondary" => { "url" => "secondary-prod-db-url" } } + "production" => { "primary" => { "url" => "abstract://prod-db-url" }, "secondary" => { "url" => "abstract://secondary-prod-db-url" } } } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations end def test_drops_current_environment_database - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :drop, @@ -637,7 +640,7 @@ module ActiveRecord end def test_drops_current_environment_database_with_url - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :drop, @@ -654,7 +657,7 @@ module ActiveRecord end def test_drops_test_and_development_databases_when_env_was_not_specified - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :drop, @@ -676,7 +679,7 @@ module ActiveRecord old_env = ENV["RAILS_ENV"] ENV["RAILS_ENV"] = "development" - ActiveRecord::Base.stub(:configurations, @configurations) do + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :drop, @@ -848,12 +851,16 @@ module ActiveRecord class DatabaseTasksPurgeCurrentTest < ActiveRecord::TestCase def test_purges_current_environment_database + old_configurations = ActiveRecord::Base.configurations configurations = { "development" => { "database" => "dev-db" }, "test" => { "database" => "test-db" }, "production" => { "database" => "prod-db" } } - ActiveRecord::Base.stub(:configurations, configurations) do + + ActiveRecord::Base.configurations = configurations + + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :purge, @@ -864,13 +871,17 @@ module ActiveRecord end end end + ensure + ActiveRecord::Base.configurations = old_configurations end end class DatabaseTasksPurgeAllTest < ActiveRecord::TestCase def test_purge_all_local_configurations + old_configurations = ActiveRecord::Base.configurations configurations = { development: { "database" => "my-db" } } - ActiveRecord::Base.stub(:configurations, configurations) do + ActiveRecord::Base.configurations = configurations + ActiveRecord::Base.configurations do assert_called_with( ActiveRecord::Tasks::DatabaseTasks, :purge, @@ -879,6 +890,8 @@ module ActiveRecord ActiveRecord::Tasks::DatabaseTasks.purge_all end end + ensure + ActiveRecord::Base.configurations = old_configurations end end diff --git a/activerecord/test/cases/tasks/legacy_database_tasks_test.rb b/activerecord/test/cases/tasks/legacy_database_tasks_test.rb new file mode 100644 index 0000000000..ffa55be878 --- /dev/null +++ b/activerecord/test/cases/tasks/legacy_database_tasks_test.rb @@ -0,0 +1,591 @@ +# frozen_string_literal: true + +require "cases/helper" +require "active_record/tasks/database_tasks" + +module ActiveRecord + class LegacyDatabaseTasksCreateAllTest < ActiveRecord::TestCase + def setup + @old_configurations = ActiveRecord::Base.configurations.to_h + + @configurations = { "development" => { "database" => "my-db" } } + + $stdout, @original_stdout = StringIO.new, $stdout + $stderr, @original_stderr = StringIO.new, $stderr + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + $stdout, $stderr = @original_stdout, @original_stderr + ActiveRecord::Base.configurations = @old_configurations + end + + def test_ignores_configurations_without_databases + @configurations["development"]["database"] = nil + + ActiveRecord::Base.configurations.to_h do + assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do + ActiveRecord::Tasks::DatabaseTasks.create_all + end + end + end + + def test_ignores_remote_databases + @configurations["development"]["host"] = "my.server.tld" + + ActiveRecord::Base.configurations.to_h do + assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :create) do + ActiveRecord::Tasks::DatabaseTasks.create_all + end + end + end + + def test_warning_for_remote_databases + @configurations["development"]["host"] = "my.server.tld" + + ActiveRecord::Base.configurations.to_h do + ActiveRecord::Tasks::DatabaseTasks.create_all + + assert_match "This task only modifies local databases. my-db is on a remote host.", + $stderr.string + end + end + + def test_creates_configurations_with_local_ip + @configurations["development"]["host"] = "127.0.0.1" + + ActiveRecord::Base.configurations.to_h do + assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do + ActiveRecord::Tasks::DatabaseTasks.create_all + end + end + end + + def test_creates_configurations_with_local_host + @configurations["development"]["host"] = "localhost" + + ActiveRecord::Base.configurations.to_h do + assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do + ActiveRecord::Tasks::DatabaseTasks.create_all + end + end + end + + def test_creates_configurations_with_blank_hosts + @configurations["development"]["host"] = nil + + ActiveRecord::Base.configurations.to_h do + assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do + ActiveRecord::Tasks::DatabaseTasks.create_all + end + end + end + end + + class LegacyDatabaseTasksCreateCurrentTest < ActiveRecord::TestCase + def setup + @old_configurations = ActiveRecord::Base.configurations.to_h + + @configurations = { + "development" => { "database" => "dev-db" }, + "test" => { "database" => "test-db" }, + "production" => { "url" => "abstract://prod-db-url" } + } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations + end + + def test_creates_current_environment_database + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + ["database" => "test-db"], + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("test") + ) + end + end + end + + def test_creates_current_environment_database_with_url + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + ["url" => "prod-db-url"], + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("production") + ) + end + end + end + + def test_creates_test_and_development_databases_when_env_was_not_specified + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + [ + ["database" => "dev-db"], + ["database" => "test-db"] + ], + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + end + + def test_creates_test_and_development_databases_when_rails_env_is_development + old_env = ENV["RAILS_ENV"] + ENV["RAILS_ENV"] = "development" + + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + [ + ["database" => "dev-db"], + ["database" => "test-db"] + ], + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + ensure + ENV["RAILS_ENV"] = old_env + end + + def test_establishes_connection_for_the_given_environments + ActiveRecord::Tasks::DatabaseTasks.stub(:create, nil) do + assert_called_with(ActiveRecord::Base, :establish_connection, [:development]) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + end + end + + class LegacyDatabaseTasksCreateCurrentThreeTierTest < ActiveRecord::TestCase + def setup + @old_configurations = ActiveRecord::Base.configurations.to_h + + @configurations = { + "development" => { "primary" => { "database" => "dev-db" }, "secondary" => { "database" => "secondary-dev-db" } }, + "test" => { "primary" => { "database" => "test-db" }, "secondary" => { "database" => "secondary-test-db" } }, + "production" => { "primary" => { "url" => "abstract://prod-db-url" }, "secondary" => { "url" => "abstract://secondary-prod-db-url" } } + } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations + end + + def test_creates_current_environment_database + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + [ + ["database" => "test-db"], + ["database" => "secondary-test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("test") + ) + end + end + end + + def test_creates_current_environment_database_with_url + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + [ + ["url" => "prod-db-url"], + ["url" => "secondary-prod-db-url"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("production") + ) + end + end + end + + def test_creates_test_and_development_databases_when_env_was_not_specified + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + [ + ["database" => "dev-db"], + ["database" => "secondary-dev-db"], + ["database" => "test-db"], + ["database" => "secondary-test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + end + + def test_creates_test_and_development_databases_when_rails_env_is_development + old_env = ENV["RAILS_ENV"] + ENV["RAILS_ENV"] = "development" + + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :create, + [ + ["database" => "dev-db"], + ["database" => "secondary-dev-db"], + ["database" => "test-db"], + ["database" => "secondary-test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + ensure + ENV["RAILS_ENV"] = old_env + end + + def test_establishes_connection_for_the_given_environments_config + ActiveRecord::Tasks::DatabaseTasks.stub(:create, nil) do + assert_called_with( + ActiveRecord::Base, + :establish_connection, + [:development] + ) do + ActiveRecord::Tasks::DatabaseTasks.create_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + end + end + + class LegacyDatabaseTasksDropAllTest < ActiveRecord::TestCase + def setup + @old_configurations = ActiveRecord::Base.configurations.to_h + + @configurations = { development: { "database" => "my-db" } } + + $stdout, @original_stdout = StringIO.new, $stdout + $stderr, @original_stderr = StringIO.new, $stderr + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + $stdout, $stderr = @original_stdout, @original_stderr + ActiveRecord::Base.configurations = @old_configurations + end + + def test_ignores_configurations_without_databases + @configurations[:development]["database"] = nil + + ActiveRecord::Base.configurations.to_h do + assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do + ActiveRecord::Tasks::DatabaseTasks.drop_all + end + end + end + + def test_ignores_remote_databases + @configurations[:development]["host"] = "my.server.tld" + + ActiveRecord::Base.configurations.to_h do + assert_not_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do + ActiveRecord::Tasks::DatabaseTasks.drop_all + end + end + end + + def test_warning_for_remote_databases + @configurations[:development]["host"] = "my.server.tld" + + ActiveRecord::Base.configurations.to_h do + ActiveRecord::Tasks::DatabaseTasks.drop_all + + assert_match "This task only modifies local databases. my-db is on a remote host.", + $stderr.string + end + end + + def test_drops_configurations_with_local_ip + @configurations[:development]["host"] = "127.0.0.1" + + ActiveRecord::Base.configurations.to_h do + assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do + ActiveRecord::Tasks::DatabaseTasks.drop_all + end + end + end + + def test_drops_configurations_with_local_host + @configurations[:development]["host"] = "localhost" + + ActiveRecord::Base.configurations.to_h do + assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do + ActiveRecord::Tasks::DatabaseTasks.drop_all + end + end + end + + def test_drops_configurations_with_blank_hosts + @configurations[:development]["host"] = nil + + ActiveRecord::Base.configurations.to_h do + assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do + ActiveRecord::Tasks::DatabaseTasks.drop_all + end + end + end + end + + class LegacyDatabaseTasksDropCurrentTest < ActiveRecord::TestCase + def setup + @old_configurations = ActiveRecord::Base.configurations.to_h + + @configurations = { + "development" => { "database" => "dev-db" }, + "test" => { "database" => "test-db" }, + "production" => { "url" => "abstract://prod-db-url" } + } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations + end + + def test_drops_current_environment_database + ActiveRecord::Base.configurations.to_h do + assert_called_with(ActiveRecord::Tasks::DatabaseTasks, :drop, + ["database" => "test-db"]) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("test") + ) + end + end + end + + def test_drops_current_environment_database_with_url + ActiveRecord::Base.configurations.to_h do + assert_called_with(ActiveRecord::Tasks::DatabaseTasks, :drop, + ["url" => "prod-db-url"]) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("production") + ) + end + end + end + + def test_drops_test_and_development_databases_when_env_was_not_specified + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :drop, + [ + ["database" => "dev-db"], + ["database" => "test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + end + + def test_drops_testand_development_databases_when_rails_env_is_development + old_env = ENV["RAILS_ENV"] + ENV["RAILS_ENV"] = "development" + + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :drop, + [ + ["database" => "dev-db"], + ["database" => "test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + ensure + ENV["RAILS_ENV"] = old_env + end + end + + class LegacyDatabaseTasksDropCurrentThreeTierTest < ActiveRecord::TestCase + def setup + @old_configurations = ActiveRecord::Base.configurations.to_h + + @configurations = { + "development" => { "primary" => { "database" => "dev-db" }, "secondary" => { "database" => "secondary-dev-db" } }, + "test" => { "primary" => { "database" => "test-db" }, "secondary" => { "database" => "secondary-test-db" } }, + "production" => { "primary" => { "url" => "abstract://prod-db-url" }, "secondary" => { "url" => "abstract://secondary-prod-db-url" } } + } + + ActiveRecord::Base.configurations = @configurations + end + + def teardown + ActiveRecord::Base.configurations = @old_configurations + end + + def test_drops_current_environment_database + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :drop, + [ + ["database" => "test-db"], + ["database" => "secondary-test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("test") + ) + end + end + end + + def test_drops_current_environment_database_with_url + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :drop, + [ + ["url" => "prod-db-url"], + ["url" => "secondary-prod-db-url"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("production") + ) + end + end + end + + def test_drops_test_and_development_databases_when_env_was_not_specified + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :drop, + [ + ["database" => "dev-db"], + ["database" => "secondary-dev-db"], + ["database" => "test-db"], + ["database" => "secondary-test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + end + + def test_drops_testand_development_databases_when_rails_env_is_development + old_env = ENV["RAILS_ENV"] + ENV["RAILS_ENV"] = "development" + + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :drop, + [ + ["database" => "dev-db"], + ["database" => "secondary-dev-db"], + ["database" => "test-db"], + ["database" => "secondary-test-db"] + ] + ) do + ActiveRecord::Tasks::DatabaseTasks.drop_current( + ActiveSupport::StringInquirer.new("development") + ) + end + end + ensure + ENV["RAILS_ENV"] = old_env + end + end + + class LegacyDatabaseTasksPurgeCurrentTest < ActiveRecord::TestCase + def test_purges_current_environment_database + @old_configurations = ActiveRecord::Base.configurations.to_h + + configurations = { + "development" => { "database" => "dev-db" }, + "test" => { "database" => "test-db" }, + "production" => { "database" => "prod-db" } + } + + ActiveRecord::Base.configurations = configurations + + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :purge, + ["database" => "prod-db"] + ) do + assert_called_with(ActiveRecord::Base, :establish_connection, [:production]) do + ActiveRecord::Tasks::DatabaseTasks.purge_current("production") + end + end + end + ensure + ActiveRecord::Base.configurations = @old_configurations + end + end + + class LegacyDatabaseTasksPurgeAllTest < ActiveRecord::TestCase + def test_purge_all_local_configurations + @old_configurations = ActiveRecord::Base.configurations.to_h + + configurations = { development: { "database" => "my-db" } } + ActiveRecord::Base.configurations = configurations + + ActiveRecord::Base.configurations.to_h do + assert_called_with( + ActiveRecord::Tasks::DatabaseTasks, + :purge, + ["database" => "my-db"] + ) do + ActiveRecord::Tasks::DatabaseTasks.purge_all + end + end + ensure + ActiveRecord::Base.configurations = @old_configurations + end + end +end |