Merge pull request #52320 from rails/fxn/constant-deprecation

Deprecate ActiveRecord::ImmutableRelation
This commit is contained in:
Xavier Noria 2024-07-13 12:41:21 +02:00 committed by GitHub
commit 5b0e7a31bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 13 deletions

@ -1,6 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
require "active_support/deprecation"
module ActiveRecord module ActiveRecord
include ActiveSupport::Deprecation::DeprecatedConstantAccessor
# = Active Record Errors # = Active Record Errors
# #
# Generic Active Record exception class. # Generic Active Record exception class.
@ -476,10 +480,15 @@ def initialize(model = nil, description = nil)
# relation.loaded? # => true # relation.loaded? # => true
# #
# # Methods which try to mutate a loaded relation fail. # # Methods which try to mutate a loaded relation fail.
# relation.where!(title: 'TODO') # => ActiveRecord::ImmutableRelation # relation.where!(title: 'TODO') # => ActiveRecord::UnmodifiableRelation
# relation.limit!(5) # => ActiveRecord::ImmutableRelation # relation.limit!(5) # => ActiveRecord::UnmodifiableRelation
class ImmutableRelation < ActiveRecordError class UnmodifiableRelation < ActiveRecordError
end end
deprecate_constant(
:ImmutableRelation,
"ActiveRecord::UnmodifiableRelation",
deprecator: ActiveRecord.deprecator
)
# TransactionIsolationError will be raised under the following conditions: # TransactionIsolationError will be raised under the following conditions:
# #

@ -174,7 +174,7 @@ def #{method_name} # def includes_values
end # end end # end
def #{method_name}=(value) # def includes_values=(value) def #{method_name}=(value) # def includes_values=(value)
assert_mutability! # assert_mutability! assert_modifiable! # assert_modifiable!
@values[:#{name}] = value # @values[:includes] = value @values[:#{name}] = value # @values[:includes] = value
end # end end # end
CODE CODE
@ -814,7 +814,7 @@ def unscope!(*args) # :nodoc:
if !VALID_UNSCOPING_VALUES.include?(scope) if !VALID_UNSCOPING_VALUES.include?(scope)
raise ArgumentError, "Called unscope() with invalid unscoping argument ':#{scope}'. Valid arguments are :#{VALID_UNSCOPING_VALUES.to_a.join(", :")}." raise ArgumentError, "Called unscope() with invalid unscoping argument ':#{scope}'. Valid arguments are :#{VALID_UNSCOPING_VALUES.to_a.join(", :")}."
end end
assert_mutability! assert_modifiable!
@values.delete(scope) @values.delete(scope)
when Hash when Hash
scope.each do |key, target_value| scope.each do |key, target_value|
@ -1723,8 +1723,8 @@ def build_join_dependencies
) )
end end
def assert_mutability! def assert_modifiable!
raise ImmutableRelation if @loaded || @arel raise UnmodifiableRelation if @loaded || @arel
end end
def build_arel(connection, aliases = nil) def build_arel(connection, aliases = nil)

@ -1,6 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cases/helper" require "cases/helper"
require "active_record/errors"
class ErrorsTest < ActiveRecord::TestCase class ErrorsTest < ActiveRecord::TestCase
def test_can_be_instantiated_with_no_args def test_can_be_instantiated_with_no_args
@ -15,4 +16,11 @@ def test_can_be_instantiated_with_no_args
end end
end end
end end
def test_active_record_immutable_relation_deprecation
expected_message = "ActiveRecord::ImmutableRelation is deprecated! Use ActiveRecord::UnmodifiableRelation instead"
assert_deprecated(expected_message, ActiveRecord.deprecator) do
assert_same ActiveRecord::UnmodifiableRelation, ActiveRecord::ImmutableRelation
end
end
end end

@ -2057,7 +2057,7 @@ def test_destroy_by
relation = Post.all relation = Post.all
relation.to_a relation.to_a
assert_raises(ActiveRecord::ImmutableRelation) do assert_raises(ActiveRecord::UnmodifiableRelation) do
relation.where! "foo" relation.where! "foo"
end end
end end
@ -2066,7 +2066,7 @@ def test_destroy_by
relation = Post.all relation = Post.all
relation.to_a relation.to_a
assert_raises(ActiveRecord::ImmutableRelation) do assert_raises(ActiveRecord::UnmodifiableRelation) do
relation.limit! 5 relation.limit! 5
end end
end end
@ -2075,7 +2075,7 @@ def test_destroy_by
relation = Post.all relation = Post.all
relation.to_a relation.to_a
assert_raises(ActiveRecord::ImmutableRelation) do assert_raises(ActiveRecord::UnmodifiableRelation) do
relation.merge! where: "foo" relation.merge! where: "foo"
end end
end end
@ -2084,7 +2084,7 @@ def test_destroy_by
relation = Post.all relation = Post.all
relation.to_a relation.to_a
assert_raises(ActiveRecord::ImmutableRelation) do assert_raises(ActiveRecord::UnmodifiableRelation) do
relation.extending! Module.new relation.extending! Module.new
end end
end end
@ -2093,8 +2093,8 @@ def test_destroy_by
relation = Post.all relation = Post.all
relation.arel relation.arel
assert_raises(ActiveRecord::ImmutableRelation) { relation.limit!(5) } assert_raises(ActiveRecord::UnmodifiableRelation) { relation.limit!(5) }
assert_raises(ActiveRecord::ImmutableRelation) { relation.where!("1 = 2") } assert_raises(ActiveRecord::UnmodifiableRelation) { relation.where!("1 = 2") }
end end
test "relations show the records in #inspect" do test "relations show the records in #inspect" do