From 81f6a1f87f18bc097c7897dfaf363b5bae468ef6 Mon Sep 17 00:00:00 2001 From: Nick Burns Date: Fri, 3 Apr 2020 11:26:59 -0700 Subject: [PATCH] Apply suggestions from review Co-Authored-By: Eileen M. Uchitelle --- .../lib/active_record/internal_metadata.rb | 4 +-- activerecord/lib/active_record/migration.rb | 5 ++-- activerecord/test/cases/migration_test.rb | 25 ++++++++++++++----- guides/source/configuring.md | 7 +++++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/activerecord/lib/active_record/internal_metadata.rb b/activerecord/lib/active_record/internal_metadata.rb index d0d09fbe59..6545f6a941 100644 --- a/activerecord/lib/active_record/internal_metadata.rb +++ b/activerecord/lib/active_record/internal_metadata.rb @@ -7,8 +7,8 @@ module ActiveRecord # This class is used to create a table that keeps track of values and keys such # as which environment migrations were run in. # - # It is possible to enable or disable this functionality by setting the - # adapter configuration option `use_metadata_table` to false + # This is enabled by default. To disable this functionality set + # `use_metadata_table` to false in your database configuration. class InternalMetadata < ActiveRecord::Base # :nodoc: class << self def enabled? diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 3d25f03a90..d7b677bcfb 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -190,10 +190,11 @@ module ActiveRecord end end - class EnvironmentStorageError < ActiveRecordError #:nodoc: + class EnvironmentStorageError < ActiveRecordError # :nodoc: def initialize msg = +"You are attempting to store the environment in a database where metadata is disabled.\n" - msg << "Check your database configuration to see if this is inteded." + msg << "Check your database configuration to see if this is intended." + super(msg) end end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 7d9e39e2b9..d9ef7a59b8 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -641,13 +641,26 @@ class MigrationTest < ActiveRecord::TestCase end def test_internal_metadata_not_used_when_not_enabled - ActiveRecord::InternalMetadata.stub(:enabled?, false) do - migrations_path = MIGRATIONS_ROOT + "/valid" + ActiveRecord::InternalMetadata.drop_table + original_config = ActiveRecord::Base.connection.instance_variable_get("@config") - migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration) - migrator.up - assert_not ActiveRecord::InternalMetadata[:environment] - end + modified_config = original_config.dup.merge(use_metadata_table: false) + + ActiveRecord::Base.connection + .instance_variable_set("@config", modified_config) + + assert_not ActiveRecord::InternalMetadata.enabled? + assert_not ActiveRecord::InternalMetadata.table_exists? + + migrations_path = MIGRATIONS_ROOT + "/valid" + migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration) + migrator.up + + assert_not ActiveRecord::InternalMetadata[:environment] + assert_not ActiveRecord::InternalMetadata.table_exists? + ensure + ActiveRecord::Base.connection.instance_variable_set("@config", original_config) + ActiveRecord::InternalMetadata.create_table end def test_proper_table_name_on_migration diff --git a/guides/source/configuring.md b/guides/source/configuring.md index c6e69cd7fb..8d4d0f3fae 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -1260,7 +1260,12 @@ Change the username and password in the `development` section as appropriate. #### Configuring Metadata Storage -By default Rails will store information about your environment and schema in an internal table. If you do not want this table to be created, and are willing to give up the protections it provides (useful when working with a shared database and a database user that can not create tables). +By default Rails will store information about your Rails environment and schema +in an internal table named `ar_internal_metadata`. + +To turn this off per connection, set `use_metadata_table` in your database +configuration. This is useful when working with a shared database and/or +database user that cannot create tables. ```yaml development: