aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authoryuuji.yaginuma <yuuji.yaginuma@gmail.com>2016-04-25 19:35:16 +0900
committeryuuji.yaginuma <yuuji.yaginuma@gmail.com>2016-04-27 10:33:27 +0900
commitcfa1df4b07bee5b2bbcbf9edd2ac287b4fb23c18 (patch)
treef58b7ffdccf997a767241119deb2a177f33cd5d3 /activerecord
parente007afd3cd7255c752fe8ab7fa24b59526ab95cb (diff)
downloadrails-cfa1df4b07bee5b2bbcbf9edd2ac287b4fb23c18.tar.gz
rails-cfa1df4b07bee5b2bbcbf9edd2ac287b4fb23c18.tar.bz2
rails-cfa1df4b07bee5b2bbcbf9edd2ac287b4fb23c18.zip
update record specified in key
`#first_or_initialize` does not use attributes to data acquisition. Therefore, there is a possibility of updating the different record than the one specified in the key, I think this is not expected behavior.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/internal_metadata.rb2
-rw-r--r--activerecord/test/cases/migration_test.rb17
2 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/internal_metadata.rb b/activerecord/lib/active_record/internal_metadata.rb
index 81db96bffd..17a5dc1d1b 100644
--- a/activerecord/lib/active_record/internal_metadata.rb
+++ b/activerecord/lib/active_record/internal_metadata.rb
@@ -19,7 +19,7 @@ module ActiveRecord
end
def []=(key, value)
- first_or_initialize(key: key).update_attributes!(value: value)
+ find_or_initialize_by(key: key).update_attributes!(value: value)
end
def [](key)
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index 5a6d2ce80c..e0a04ca9ed 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -424,6 +424,23 @@ class MigrationTest < ActiveRecord::TestCase
ENV["RACK_ENV"] = original_rack_env
end
+ def test_internal_metadata_stores_environment_when_other_data_exists
+ ActiveRecord::InternalMetadata.delete_all
+ ActiveRecord::InternalMetadata[:foo] = 'bar'
+
+ current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
+ migrations_path = MIGRATIONS_ROOT + "/valid"
+ old_path = ActiveRecord::Migrator.migrations_paths
+ ActiveRecord::Migrator.migrations_paths = migrations_path
+
+ current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
+ ActiveRecord::Migrator.up(migrations_path)
+ assert_equal current_env, ActiveRecord::InternalMetadata[:environment]
+ assert_equal 'bar', ActiveRecord::InternalMetadata[:foo]
+ ensure
+ ActiveRecord::Migrator.migrations_paths = old_path
+ end
+
def test_rename_internal_metadata_table
original_internal_metadata_table_name = ActiveRecord::Base.internal_metadata_table_name