Skip to content

Commit

Permalink
Fix migration ordering for bin/rails db:prepare across databases
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Aug 5, 2024
1 parent e419653 commit c53ec4b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 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 @@ -92,7 +92,7 @@ db_namespace = namespace :db do
if db_configs.size == 1 && db_configs.first.primary?
ActiveRecord::Tasks::DatabaseTasks.migrate
else
mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions(db_configs)
mapped_versions = ActiveRecord::Tasks::DatabaseTasks.db_configs_with_versions

mapped_versions.sort.each do |version, db_configs|
db_configs.each do |db_config|
Expand Down
27 changes: 19 additions & 8 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,17 @@ def prepare_all

seed = true
end
end
end

migrate
dump_schema(db_config) if ActiveRecord.dump_schema_after_migration
each_current_environment(env) do |environment|
db_configs_with_versions(environment).sort.each do |version, db_configs|
db_configs.each do |db_config|
with_temporary_pool(db_config) do
migrate(version)
dump_schema(db_config) if ActiveRecord.dump_schema_after_migration
end
end
end
end

Expand Down Expand Up @@ -255,10 +263,10 @@ def migrate(version = nil)
Migration.verbose = verbose_was
end

def db_configs_with_versions(db_configs) # :nodoc:
def db_configs_with_versions(environment = env) # :nodoc:
db_configs_with_versions = Hash.new { |h, k| h[k] = [] }

with_temporary_connection_for_each do |conn|
with_temporary_connection_for_each(env: environment) do |conn|
db_config = conn.pool.db_config
versions_to_run = conn.migration_context.pending_migration_versions
target_version = ActiveRecord::Tasks::DatabaseTasks.target_version
Expand Down Expand Up @@ -552,10 +560,7 @@ def class_for_adapter(adapter)
end

def each_current_configuration(environment, name = nil)
environments = [environment]
environments << "test" if environment == "development" && !ENV["SKIP_TEST_DATABASE"] && !ENV["DATABASE_URL"]

environments.each do |env|
each_current_environment(environment) do |env|
configs_for(env_name: env).each do |db_config|
next if name && name != db_config.name

Expand All @@ -564,6 +569,12 @@ def each_current_configuration(environment, name = nil)
end
end

def each_current_environment(environment, &block)
environments = [environment]
environments << "test" if environment == "development" && !ENV["SKIP_TEST_DATABASE"] && !ENV["DATABASE_URL"]
environments.each(&block)
end

def each_local_configuration
configs_for.each do |db_config|
next unless db_config.database
Expand Down
29 changes: 29 additions & 0 deletions railties/test/application/rake/multi_dbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,35 @@ class ThreeMigration < ActiveRecord::Migration::Current
end
end

test "db:prepare respects timestamp ordering across databases" do
require "#{app_path}/config/environment"
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration::Current
end
MIGRATION

app_file "db/animals_migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration::Current
end
MIGRATION

app_file "db/animals_migrate/04_four_migration.rb", <<-MIGRATION
class FourMigration < ActiveRecord::Migration::Current
end
MIGRATION

app_file "db/migrate/03_three_migration.rb", <<-MIGRATION
class ThreeMigration < ActiveRecord::Migration::Current
end
MIGRATION

Dir.chdir(app_path) do
output = rails "db:prepare"
entries = output.scan(/^== (\d+).+migrated/).map(&:first).map(&:to_i)
assert_equal [1, 2, 3, 4] * 2, entries # twice because for test env too
end
end

test "migrations in different directories can have the same timestamp" do
require "#{app_path}/config/environment"
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
Expand Down

0 comments on commit c53ec4b

Please sign in to comment.