Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store correct environment in internal_metadata when run rails db:prepare #46879

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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