Skip to content

Commit

Permalink
Test for all affected methods, tightens up deprecation query
Browse files Browse the repository at this point in the history
  • Loading branch information
Schwad committed Jan 20, 2025
1 parent fdb7173 commit c4c3956
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1128,13 +1128,13 @@ def pretty_print(pp) # :nodoc:
%w(insert insert_all insert! insert_all! upsert upsert_all).each do |method|
class_eval <<~RUBY, __FILE__, __LINE__ + 1
def #{method}(...)
if loaded? && @association&.target&.any? { |r| r.new_record? }
if @association&.target&.any? { |r| r.new_record? }
association_name = @association.reflection.name
ActiveRecord.deprecator.warn(<<~MSG)
Using #{method} on association with unpersisted records
(\#{to_a.reject(&:persisted?).map(&:class).uniq.join(', ')})
Using #{method} on association \#{association_name} with unpersisted records
is deprecated. The unpersisted records will be lost after this operation.
Please either persist your records first or store them separately before
calling #{method}. This will become an error in Rails 8.1.
calling #{method}.
MSG
scope.#{method}(...)
else
Expand Down
94 changes: 85 additions & 9 deletions activerecord/test/cases/insert_all_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -859,28 +859,104 @@ def test_insert_all_when_table_name_contains_database
end
end

def test_insert_all_with_unpersisted_records_deprecation
author = Author.create!(name: "DHH")
def test_insert_all_with_unpersisted_records_triggers_deprecation
author = Author.create!(name: "Rafael")
author.books.build(title: "Unpersisted Book")
author.books.load

assert_deprecated(ActiveRecord.deprecator) do
author.books.insert({ title: "New Book" })
end

author.books.load
assert_includes author.books.pluck(:title), "Unpersisted Book"
end

def test_insert_all_without_unpersisted_records_has_no_deprecation
author = Author.create!(name: "Rafael")

assert_not_deprecated(ActiveRecord.deprecator) do
author.books.insert_all([{ title: "New Book" }])
end
end

def test_insert_with_unpersisted_records_triggers_deprecation
author = Author.create!(name: "Rafael")
author.books.build(title: "Unpersisted Book")

assert_deprecated(ActiveRecord.deprecator) do
author.books.insert({ title: "New Book" })
end

assert_equal 2, author.books.to_a.size
assert_equal ["Unpersisted Book", "New Book"], author.books.pluck(:title)
author.books.load
assert_includes author.books.pluck(:title), "Unpersisted Book"
end

def test_insert_all_resets_without_unpersisted_records
author = Author.create!(name: "DHH")
def test_insert_without_unpersisted_records_has_no_deprecation
author = Author.create!(name: "Rafael")

assert_not_deprecated(ActiveRecord.deprecator) do
author.books.insert({ title: "New Book" })
end
end

def test_insert_all_bang_with_unpersisted_record_triggers_deprecation
author = Author.create!(name: "Rafael")
author.books.build(title: "Unpersisted Book")

assert_deprecated(ActiveRecord.deprecator) do
author.books.insert_all!([{ title: "New Book" }])
end

author.books.load
assert_includes author.books.pluck(:title), "Unpersisted Book"
end

def test_insert_all_bang_without_unpersisted_records_has_no_deprecation
author = Author.create!(name: "Rafael")

assert_not_deprecated(ActiveRecord.deprecator) do
author.books.insert_all([{ title: "New Book" }])
author.books.insert_all!([{ title: "New Book" }])
end
end

def test_upsert_all_with_unpersisted_record_triggers_deprecation
author = Author.create!(name: "Rafael")
author.books.build(title: "Unpersisted Book")

assert_deprecated(ActiveRecord.deprecator) do
author.books.upsert_all([{ title: "New Book" }])
end

author.books.load
assert_includes author.books.pluck(:title), "Unpersisted Book"
end

def test_upsert_all_without_unpersisted_records_has_no_deprecation
author = Author.create!(name: "Rafael")

assert_not_deprecated(ActiveRecord.deprecator) do
author.books.upsert_all([{ title: "New Book" }])
end
end

def test_upsert_with_unpersisted_record_triggers_deprecation
author = Author.create!(name: "Rafael")
author.books.build(title: "Unpersisted Book")

assert_deprecated(ActiveRecord.deprecator) do
author.books.upsert({ title: "New Book" })
end

assert_not author.books.loaded?
author.books.load
assert_includes author.books.pluck(:title), "Unpersisted Book"
end

def test_upsert_without_unpersisted_records_has_no_deprecation
author = Author.create!(name: "Rafael")

assert_not_deprecated(ActiveRecord.deprecator) do
author.books.upsert({ title: "New Book" })
end
end

private
Expand Down

0 comments on commit c4c3956

Please sign in to comment.