Skip to content

Commit 532188c

Browse files
authored
Merge pull request rails#45498 from HParker/deprecate-rewhere-on-merge
Deprecate passing `rewhere` to `merge`
2 parents 9084a90 + 12d6d98 commit 532188c

File tree

2 files changed

+111
-26
lines changed

2 files changed

+111
-26
lines changed

activerecord/lib/active_record/relation/spawn_methods.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ def merge(other, *rest)
4242

4343
def merge!(other, *rest) # :nodoc:
4444
options = rest.extract_options!
45+
46+
if options.key?(:rewhere)
47+
if options[:rewhere]
48+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
49+
Specifying `Relation#merge(rewhere: true)` is deprecated, as that has now been
50+
the default since Rails 7.0. Setting the rewhere option will error in Rails 7.2
51+
MSG
52+
else
53+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
54+
`Relation#merge(rewhere: false)` is deprecated without replacement,
55+
and will be removed in Rails 7.2
56+
MSG
57+
end
58+
end
59+
4560
if other.is_a?(Hash)
4661
Relation::HashMerger.new(self, other, options[:rewhere]).merge
4762
elsif other.is_a?(Relation)

activerecord/test/cases/relation/merging_test.rb

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,41 @@ def test_merge_in_clause
2424

2525
assert_equal [mary], david_and_mary.merge(Author.where(id: mary))
2626
assert_equal [mary], david_and_mary.merge(Author.rewhere(id: mary))
27-
assert_equal [mary], david_and_mary.merge(Author.where(id: mary), rewhere: true)
27+
assert_deprecated do
28+
assert_equal [mary], david_and_mary.merge(Author.where(id: mary), rewhere: true)
29+
end
2830

2931
assert_equal [bob], david_and_mary.merge(Author.where(id: bob))
3032
assert_equal [bob], david_and_mary.merge(Author.rewhere(id: bob))
31-
assert_equal [bob], david_and_mary.merge(Author.where(id: bob), rewhere: true)
33+
assert_deprecated do
34+
assert_equal [bob], david_and_mary.merge(Author.where(id: bob), rewhere: true)
35+
end
3236

3337
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]))
34-
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]), rewhere: true)
38+
assert_deprecated do
39+
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]), rewhere: true)
40+
end
3541

3642
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob)
37-
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob, rewhere: true)
43+
assert_deprecated do
44+
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob, rewhere: true)
45+
end
3846
assert_equal [mary], david_and_mary.and(mary_and_bob)
3947
assert_equal authors, david_and_mary.or(mary_and_bob)
4048

4149
assert_equal [david, mary], mary_and_bob.merge(david_and_mary)
42-
assert_equal [david, mary], mary_and_bob.merge(david_and_mary, rewhere: true)
50+
assert_deprecated do
51+
assert_equal [david, mary], mary_and_bob.merge(david_and_mary, rewhere: true)
52+
end
4353
assert_equal [mary], david_and_mary.and(mary_and_bob)
4454
assert_equal authors, david_and_mary.or(mary_and_bob)
4555

4656
david_and_bob = Author.where(id: david).or(Author.where(name: "Bob"))
4757

4858
assert_equal [david], david_and_mary.merge(david_and_bob)
49-
assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true)
59+
assert_deprecated do
60+
assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true)
61+
end
5062
assert_equal [david], david_and_mary.and(david_and_bob)
5163
assert_equal authors, david_and_mary.or(david_and_bob)
5264
end
@@ -62,29 +74,41 @@ def test_merge_between_clause
6274

6375
assert_equal [mary], david_and_mary.merge(Author.where(id: mary))
6476
assert_equal [mary], david_and_mary.merge(Author.rewhere(id: mary))
65-
assert_equal [mary], david_and_mary.merge(Author.where(id: mary), rewhere: true)
77+
assert_deprecated do
78+
assert_equal [mary], david_and_mary.merge(Author.where(id: mary), rewhere: true)
79+
end
6680

6781
assert_equal [bob], david_and_mary.merge(Author.where(id: bob))
6882
assert_equal [bob], david_and_mary.merge(Author.rewhere(id: bob))
69-
assert_equal [bob], david_and_mary.merge(Author.where(id: bob), rewhere: true)
83+
assert_deprecated do
84+
assert_equal [bob], david_and_mary.merge(Author.where(id: bob), rewhere: true)
85+
end
7086

7187
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]))
72-
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]), rewhere: true)
88+
assert_deprecated do
89+
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]), rewhere: true)
90+
end
7391

7492
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob)
75-
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob, rewhere: true)
93+
assert_deprecated do
94+
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob, rewhere: true)
95+
end
7696
assert_equal [mary], david_and_mary.and(mary_and_bob)
7797
assert_equal authors, david_and_mary.or(mary_and_bob)
7898

7999
assert_equal [david, mary], mary_and_bob.merge(david_and_mary)
80-
assert_equal [david, mary], mary_and_bob.merge(david_and_mary, rewhere: true)
100+
assert_deprecated do
101+
assert_equal [david, mary], mary_and_bob.merge(david_and_mary, rewhere: true)
102+
end
81103
assert_equal [mary], david_and_mary.and(mary_and_bob)
82104
assert_equal authors, david_and_mary.or(mary_and_bob)
83105

84106
david_and_bob = Author.where(id: david).or(Author.where(name: "Bob"))
85107

86108
assert_equal [david], david_and_mary.merge(david_and_bob)
87-
assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true)
109+
assert_deprecated do
110+
assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true)
111+
end
88112
assert_equal [david], david_and_mary.and(david_and_bob)
89113
assert_equal authors, david_and_mary.or(david_and_bob)
90114
end
@@ -100,30 +124,42 @@ def test_merge_or_clause
100124

101125
assert_equal [mary], david_and_mary.merge(Author.where(id: mary))
102126
assert_equal [mary], david_and_mary.merge(Author.rewhere(id: mary))
103-
assert_equal [mary], david_and_mary.merge(Author.where(id: mary), rewhere: true)
127+
assert_deprecated do
128+
assert_equal [mary], david_and_mary.merge(Author.where(id: mary), rewhere: true)
129+
end
104130

105131
assert_equal [bob], david_and_mary.merge(Author.where(id: bob))
106132
assert_equal [bob], david_and_mary.merge(Author.rewhere(id: bob))
107-
assert_equal [bob], david_and_mary.merge(Author.where(id: bob), rewhere: true)
133+
assert_deprecated do
134+
assert_equal [bob], david_and_mary.merge(Author.where(id: bob), rewhere: true)
135+
end
108136

109137
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]))
110-
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]), rewhere: true)
138+
assert_deprecated do
139+
assert_equal [david, bob], mary_and_bob.merge(Author.where(id: [david, bob]), rewhere: true)
140+
end
111141

112142

113143
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob)
114-
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob, rewhere: true)
144+
assert_deprecated do
145+
assert_equal [mary, bob], david_and_mary.merge(mary_and_bob, rewhere: true)
146+
end
115147
assert_equal [mary], david_and_mary.and(mary_and_bob)
116148
assert_equal authors, david_and_mary.or(mary_and_bob)
117149

118150
assert_equal [david, mary], mary_and_bob.merge(david_and_mary)
119-
assert_equal [david, mary], mary_and_bob.merge(david_and_mary, rewhere: true)
151+
assert_deprecated do
152+
assert_equal [david, mary], mary_and_bob.merge(david_and_mary, rewhere: true)
153+
end
120154
assert_equal [mary], david_and_mary.and(mary_and_bob)
121155
assert_equal authors, david_and_mary.or(mary_and_bob)
122156

123157
david_and_bob = Author.where(id: david).or(Author.where(name: "Bob"))
124158

125159
assert_equal [david], david_and_mary.merge(david_and_bob)
126-
assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true)
160+
assert_deprecated do
161+
assert_equal [david], david_and_mary.merge(david_and_bob, rewhere: true)
162+
end
127163
assert_equal [david], david_and_mary.and(david_and_bob)
128164
assert_equal authors, david_and_mary.or(david_and_bob)
129165
end
@@ -136,10 +172,14 @@ def test_merge_not_in_clause
136172
assert_equal [david], non_mary_and_bob
137173

138174
assert_equal [david], Author.where(id: david).merge(non_mary_and_bob)
139-
assert_equal [david], Author.where(id: david).merge(non_mary_and_bob, rewhere: true)
175+
assert_deprecated do
176+
assert_equal [david], Author.where(id: david).merge(non_mary_and_bob, rewhere: true)
177+
end
140178

141179
assert_equal [david], Author.where(id: mary).merge(non_mary_and_bob)
142-
assert_equal [david], Author.where(id: mary).merge(non_mary_and_bob, rewhere: true)
180+
assert_deprecated do
181+
assert_equal [david], Author.where(id: mary).merge(non_mary_and_bob, rewhere: true)
182+
end
143183
end
144184

145185
def test_merge_not_range_clause
@@ -150,10 +190,14 @@ def test_merge_not_range_clause
150190
assert_equal [david, mary], less_than_bob
151191

152192
assert_equal [david, mary], Author.where(id: david).merge(less_than_bob)
153-
assert_equal [david, mary], Author.where(id: david).merge(less_than_bob, rewhere: true)
193+
assert_deprecated do
194+
assert_equal [david, mary], Author.where(id: david).merge(less_than_bob, rewhere: true)
195+
end
154196

155197
assert_equal [david, mary], Author.where(id: mary).merge(less_than_bob)
156-
assert_equal [david, mary], Author.where(id: mary).merge(less_than_bob, rewhere: true)
198+
assert_deprecated do
199+
assert_equal [david, mary], Author.where(id: mary).merge(less_than_bob, rewhere: true)
200+
end
157201
end
158202

159203
def test_merge_doesnt_duplicate_same_clauses
@@ -174,15 +218,19 @@ def test_merge_doesnt_duplicate_same_clauses
174218
end
175219

176220
assert_sql(/WHERE \(#{Regexp.escape(author_id)} IN \('1'\)\)\z/) do
177-
assert_equal [david], only_david.merge(only_david, rewhere: true)
221+
assert_deprecated do
222+
assert_equal [david], only_david.merge(only_david, rewhere: true)
223+
end
178224
end
179225
else
180226
assert_sql(/WHERE \(#{Regexp.escape(author_id)} IN \(1\)\)\z/) do
181227
assert_equal [david], only_david.merge(only_david)
182228
end
183229

184230
assert_sql(/WHERE \(#{Regexp.escape(author_id)} IN \(1\)\)\z/) do
185-
assert_equal [david], only_david.merge(only_david, rewhere: true)
231+
assert_deprecated do
232+
assert_equal [david], only_david.merge(only_david, rewhere: true)
233+
end
186234
end
187235
end
188236
end
@@ -207,7 +255,9 @@ def test_relation_merging_with_arel_equalities_keeps_last_equality
207255
devs = Developer.where(salary_attr.eq(80000)).merge(Developer.where(salary_attr.eq(9000)))
208256
assert_equal [developers(:poor_jamis)], devs.to_a
209257

210-
devs = Developer.where(salary_attr.eq(80000)).merge(Developer.where(salary_attr.eq(9000)), rewhere: true)
258+
assert_deprecated do
259+
devs = Developer.where(salary_attr.eq(80000)).merge(Developer.where(salary_attr.eq(9000)), rewhere: true)
260+
end
211261
assert_equal [developers(:poor_jamis)], devs.to_a
212262

213263
devs = Developer.where(salary_attr.eq(80000)).rewhere(salary_attr.eq(9000))
@@ -221,7 +271,9 @@ def test_relation_merging_with_arel_equalities_keeps_last_equality_with_non_attr
221271
devs = Developer.where(abs_salary.eq(80000)).merge(Developer.where(abs_salary.eq(9000)))
222272
assert_equal [developers(:poor_jamis)], devs.to_a
223273

224-
devs = Developer.where(abs_salary.eq(80000)).merge(Developer.where(abs_salary.eq(9000)), rewhere: true)
274+
assert_deprecated do
275+
devs = Developer.where(abs_salary.eq(80000)).merge(Developer.where(abs_salary.eq(9000)), rewhere: true)
276+
end
225277
assert_equal [developers(:poor_jamis)], devs.to_a
226278

227279
devs = Developer.where(abs_salary.eq(80000)).rewhere(abs_salary.eq(9000))
@@ -347,6 +399,24 @@ def test_merging_duplicated_annotations
347399
Post.annotate("bar").merge(Post.annotate("foo")).merge(posts).to_a
348400
end
349401
end
402+
403+
def test_rewhere_true_is_deprecated
404+
message = <<-MSG.squish
405+
Specifying `Relation#merge(rewhere: true)` is deprecated
406+
MSG
407+
assert_deprecated(message) do
408+
Author.where(id: 1).merge(Author.where(id: 2), rewhere: true)
409+
end
410+
end
411+
412+
def test_rewhere_false_is_deprecated
413+
message = <<-MSG.squish
414+
Relation#merge(rewhere: false)` is deprecated without replacement
415+
MSG
416+
assert_deprecated(message) do
417+
Author.where(id: 1).merge(Author.where(id: 2), rewhere: false)
418+
end
419+
end
350420
end
351421

352422
class MergingDifferentRelationsTest < ActiveRecord::TestCase

0 commit comments

Comments
 (0)