Skip to content

Commit

Permalink
Fix bug in internal metadata
Browse files Browse the repository at this point in the history
Since changing internal metadata to no longer inherit from Base
in #45982 I accidentally changed the behavior when a key's value is the
same. Prior to this change the record would not be updated if the
environment key was found an the value had not changed. So instead of
just checking whether we have an entry here we also need to check if the
value should actually be updated, otherwise we should return the entry.
  • Loading branch information
eileencodes committed Sep 12, 2022
1 parent ac751f8 commit 3d50f34
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion activerecord/lib/active_record/internal_metadata.rb
Expand Up @@ -96,7 +96,11 @@ def update_or_create_entry(key, value)
entry = select_entry(key)

if entry
update_entry(key, value)
if entry[value_key] != value
update_entry(key, value)
else
entry[value_key]
end
else
create_entry(key, value)
end
Expand Down
25 changes: 25 additions & 0 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -762,6 +762,31 @@ def test_internal_metadata_not_used_when_not_enabled
@internal_metadata.create_table
end

def test_inserting_a_new_entry_into_internal_metadata
@internal_metadata[:version] = "foo"
assert_equal "foo", @internal_metadata[:version]
ensure
@internal_metadata.delete_all_entries
end

def test_updating_an_existing_entry_into_internal_metadata
@internal_metadata[:version] = "foo"
updated_at = @internal_metadata.send(:select_entry, :version)["updated_at"]
assert_equal "foo", @internal_metadata[:version]

# same version doesn't update timestamps
@internal_metadata[:version] = "foo"
assert_equal "foo", @internal_metadata[:version]
assert_equal updated_at, @internal_metadata.send(:select_entry, :version)["updated_at"]

# updated version updates timestamps
@internal_metadata[:version] = "not_foo"
assert_equal "not_foo", @internal_metadata[:version]
assert_not_equal updated_at, @internal_metadata.send(:select_entry, :version)["updated_at"]
ensure
@internal_metadata.delete_all_entries
end

def test_internal_metadata_create_table_wont_be_affected_by_schema_cache
@internal_metadata.drop_table
assert_not_predicate @internal_metadata, :table_exists?
Expand Down

0 comments on commit 3d50f34

Please sign in to comment.