Skip to content

Commit

Permalink
Merge pull request rails#51916 from fatkodima/migrations-primary-with…
Browse files Browse the repository at this point in the history
…out-db-tasks

Fix running migrations on other databases when `database_tasks: false` on primary
  • Loading branch information
eileencodes committed Jun 8, 2024
1 parent 8e97d84 commit 0b18f0b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ db_namespace = namespace :db do
task migrate: :load_config do
db_configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)

if db_configs.size == 1
if db_configs.size == 1 && db_configs.first.primary?
ActiveRecord::Tasks::DatabaseTasks.migrate
else
mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions
Expand Down
95 changes: 89 additions & 6 deletions railties/test/application/rake/multi_dbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1261,24 +1261,107 @@ class TwoMigration < ActiveRecord::Migration::Current
YAML

Dir.chdir(app_path) do
animals_db_exists = lambda { rails("runner", "puts !!(AnimalsBase.lease_connection rescue false)").strip }
generate_models_for_animals

assert_not File.exist?("storage/development_animals.sqlite3")
assert_not File.exist?("db/animals_schema.rb")

error = assert_raises do
rails "db:migrate:animals" ### Task not defined
end
assert_includes error.message, "Unrecognized command"

rails "db:migrate"
assert File.exist?("storage/default.sqlite3")
assert_not File.exist?("storage/development_animals.sqlite3")
assert File.exist?("db/schema.rb")
assert_not File.exist?("db/animals_schema.rb")

rails "db:drop"
assert_not File.exist?("storage/default.sqlite3")
assert_not File.exist?("storage/development_animals.sqlite3")
end
end

test "when database_tasks is false on 'primary', then run the database tasks on other dbs" do
require "#{app_path}/config/environment"
app_file "config/database.yml", <<-YAML
development:
primary:
database: storage/development.sqlite3
adapter: sqlite3
database_tasks: false
animals:
database: storage/development_animals.sqlite3
adapter: sqlite3
migrations_paths: db/animals_migrate
YAML

Dir.chdir(app_path) do
generate_models_for_animals

assert_equal "true", animals_db_exists.call
assert_not File.exist?("storage/development.sqlite3")
assert_not File.exist?("storage/development_animals.sqlite3")

assert_not File.exist?("db/animals_schema.yml")
assert_not File.exist?("db/schema.rb")
assert_not File.exist?("db/animals_schema.rb")

error = assert_raises do
rails "db:migrate:animals" ### Task not defined
end
assert_includes error.message, "Unrecognized command"

rails "db:schema:dump"
assert_not File.exist?("db/animals_schema.yml")
rails "db:migrate"
assert_not File.exist?("storage/development.sqlite3")
assert File.exist?("storage/development_animals.sqlite3")
assert_not File.exist?("db/schema.rb")
assert File.exist?("db/animals_schema.rb")

rails "db:drop"
assert_equal "true", animals_db_exists.call

assert_not File.exist?("storage/development.sqlite3")
assert_not File.exist?("storage/development_animals.sqlite3")
end
end

test "when database_tasks is false on the implicit primary database, then run the database tasks on other dbs" do
require "#{app_path}/config/environment"
app_file "config/database.yml", <<-YAML
development:
main:
database: storage/development.sqlite3
adapter: sqlite3
database_tasks: false
animals:
database: storage/development_animals.sqlite3
adapter: sqlite3
migrations_paths: db/animals_migrate
YAML

Dir.chdir(app_path) do
generate_models_for_animals

assert_not File.exist?("storage/development.sqlite3")
assert_not File.exist?("storage/development_animals.sqlite3")

assert_not File.exist?("db/schema.rb")
assert_not File.exist?("db/animals_schema.rb")

error = assert_raises do
rails "db:migrate:animals" ### Task not defined
end
assert_includes error.message, "Unrecognized command"

rails "db:migrate"
assert_not File.exist?("storage/development.sqlite3")
assert File.exist?("storage/development_animals.sqlite3")
assert_not File.exist?("db/schema.rb")
assert File.exist?("db/animals_schema.rb")

rails "db:drop"

assert_not File.exist?("storage/development.sqlite3")
assert_not File.exist?("storage/development_animals.sqlite3")
end
end

Expand Down

0 comments on commit 0b18f0b

Please sign in to comment.