Skip to content

Commit

Permalink
Merge pull request #46879 from fatkodima/db_prepare-fix-test-environment
Browse files Browse the repository at this point in the history
Store correct environment in `internal_metadata` when run `rails db:prepare`
  • Loading branch information
eileencodes committed Jan 5, 2023
2 parents 5fc55b7 + 2e7f287 commit 43b1c33
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 33 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/migration.rb
Expand Up @@ -1427,7 +1427,7 @@ def migrate_without_lock
def record_environment
return if down?

@internal_metadata[:environment] = connection.migration_context.current_environment
@internal_metadata[:environment] = connection.pool.db_config.env_name
end

def ran?(migration)
Expand Down
25 changes: 7 additions & 18 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -689,32 +689,17 @@ def test_internal_metadata_table_name
end

def test_internal_metadata_stores_environment
current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
current_env = env_name(@internal_metadata.connection)
migrations_path = MIGRATIONS_ROOT + "/valid"
migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration, @internal_metadata)

migrator.up
assert_equal current_env, @internal_metadata[:environment]

original_rails_env = ENV["RAILS_ENV"]
original_rack_env = ENV["RACK_ENV"]
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = "foofoo"
new_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call

assert_not_equal current_env, new_env

sleep 1 # mysql by default does not store fractional seconds in the database
migrator.up
assert_equal new_env, @internal_metadata[:environment]
ensure
ENV["RAILS_ENV"] = original_rails_env
ENV["RACK_ENV"] = original_rack_env
migrator.up
end

def test_internal_metadata_stores_environment_when_migration_fails
@internal_metadata.delete_all_entries
current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
current_env = env_name(@internal_metadata.connection)

migration = Class.new(ActiveRecord::Migration::Current) {
def version; 101 end
Expand All @@ -732,7 +717,7 @@ def test_internal_metadata_stores_environment_when_other_data_exists
@internal_metadata.delete_all_entries
@internal_metadata[:foo] = "bar"

current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
current_env = env_name(@internal_metadata.connection)
migrations_path = MIGRATIONS_ROOT + "/valid"

migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration, @internal_metadata)
Expand Down Expand Up @@ -1184,6 +1169,10 @@ def with_another_process_holding_lock(lock_id)
test_terminated.count_down
other_process.join
end

def env_name(connection)
connection.pool.db_config.env_name
end
end

class ReservedWordsMigrationTest < ActiveRecord::TestCase
Expand Down
15 changes: 1 addition & 14 deletions activerecord/test/cases/multi_db_migrator_test.rb
Expand Up @@ -197,26 +197,13 @@ def test_migrator_forward
end

def test_internal_metadata_stores_environment
current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
current_env = ActiveRecord::Base.connection.pool.db_config.env_name
migrations_path = MIGRATIONS_ROOT + "/valid"
migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration_b, @internal_metadata_b)

migrator.up
assert_equal current_env, @internal_metadata_b[:environment]

original_rails_env = ENV["RAILS_ENV"]
original_rack_env = ENV["RACK_ENV"]
ENV["RAILS_ENV"] = ENV["RACK_ENV"] = "foofoo"
new_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call

assert_not_equal current_env, new_env

sleep 1 # mysql by default does not store fractional seconds in the database
migrator.up
assert_equal new_env, @internal_metadata_b[:environment]
ensure
ENV["RAILS_ENV"] = original_rails_env
ENV["RACK_ENV"] = original_rack_env
migrator.down if migrator
end

Expand Down
6 changes: 6 additions & 0 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -716,6 +716,12 @@ def db_fixtures_load(expected_database)

tables = rails("runner", "p ActiveRecord::Base.connection.tables.sort").strip
assert_equal('["ar_internal_metadata", "books", "recipes", "schema_migrations"]', tables)

test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::Base.connection.internal_metadata[:environment]").strip }
development_environment = lambda { rails("runner", "puts ActiveRecord::Base.connection.internal_metadata[:environment]").strip }

assert_equal "development", development_environment.call
assert_equal "test", test_environment.call
end
end

Expand Down

0 comments on commit 43b1c33

Please sign in to comment.