Remove all uses of Scope::Guard from the tests, use our own version
[dbsrgits/DBIx-Class.git] / t / resultset / update_delete.t
CommitLineData
ff1234ad 1use strict;
2use warnings;
3
4use lib qw(t/lib);
5use Test::More;
6use Test::Exception;
31073ac7 7
1b658919 8# MASSIVE FIXME - there is a hole in ::RSC / as_subselect_rs
9# losing the order. Needs a rework/extract of the realiaser,
10# and that's a whole another bag of dicks
11BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
12
bbf6a9a5 13use DBIx::Class::_Util 'scope_guard';
14
31073ac7 15use DBICTest::Schema::CD;
16BEGIN {
17 # the default scalarref table name will not work well for this test
18 DBICTest::Schema::CD->table('cd');
19}
20
ff1234ad 21use DBICTest;
22
be64931c 23my $schema = DBICTest->init_schema;
24
ff1234ad 25my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
26
fd8076c8 27my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
ff1234ad 28 [qw/foo bar hello goodbye sensors read_count/],
29 [qw/1 1 1 1 a 10 /],
30 [qw/2 2 2 2 b 20 /],
fd8076c8 31 [qw/1 1 1 2 c 30 /],
ff1234ad 32]);
33
34# This is already provided by DBICTest
35#my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
36# [qw/artist cd /],
37# [qw/1 1 /],
38# [qw/2 2 /],
39#]);
40my ($ta, $tb) = $schema->resultset ('TwoKeys')
1b658919 41 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ], { order_by => 'artist' })
ff1234ad 42 ->all;
43
44my $tkfk_cnt = $tkfks->count;
45
46my $non_void_ctx = $tkfks->populate ([
47 { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
48 { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
49 { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
50 { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
51]);
52is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
53
54#
55# Make sure the forced group by works (i.e. the joining does not cause double-updates)
56#
57
58# create a resultset matching $fa and $fb only
fd8076c8 59my $fks = $schema->resultset ('FourKeys')->search (
60 {
61 sensors => { '!=', 'c' },
62 ( map { $_ => [1, 2] } qw/foo bar hello goodbye/ ),
eb58c082 63 }, { join => { fourkeys_to_twokeys => 'twokeys' }}
fd8076c8 64);
ff1234ad 65
66is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
2cfc22dd 67$schema->is_executed_sql_bind( sub {
68 $fks->update ({ read_count => \ 'read_count + 1' })
69}, [[
be64931c 70 'UPDATE fourkeys
71 SET read_count = read_count + 1
fd8076c8 72 WHERE ( ( ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? ) )
73 ',
2cfc22dd 74 (1, 2) x 4,
75 'c',
76]], 'Correct update-SQL with multijoin with pruning' );
fd8076c8 77
78is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset');
79is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset');
80is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
81
82# make the multi-join stick
1b658919 83my $fks_multi = $fks->search(
84 { 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } },
85 { order_by => [ $fks->result_source->primary_columns ] },
86);
2cfc22dd 87$schema->is_executed_sql_bind( sub {
88 $fks_multi->update ({ read_count => \ 'read_count + 1' })
89}, [
90 [ 'BEGIN' ],
91 [
92 'SELECT me.foo, me.bar, me.hello, me.goodbye
93 FROM fourkeys me
94 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
95 ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
96 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
97 GROUP BY me.foo, me.bar, me.hello, me.goodbye
1b658919 98 ORDER BY foo, bar, hello, goodbye
2cfc22dd 99 ',
100 (1, 2) x 2,
101 666,
102 (1, 2) x 2,
103 'c',
104 ],
105 [
106 'UPDATE fourkeys
107 SET read_count = read_count + 1
108 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
109 ',
110 ( (1) x 4, (2) x 4 ),
111 ],
112 [ 'COMMIT' ],
113], 'Correct update-SQL with multijoin without pruning' );
ff1234ad 114
fd8076c8 115is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset');
116is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset');
117is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
be64931c 118
31160673 119$schema->is_executed_sql_bind( sub {
120 my $res = $fks_multi->search (\' "blah" = "bleh" ')->delete;
121 ok ($res, 'operation is true');
122 cmp_ok ($res, '==', 0, 'zero rows affected');
123}, [
124 [ 'BEGIN' ],
125 [
126 'SELECT me.foo, me.bar, me.hello, me.goodbye
127 FROM fourkeys me
128 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
129 ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
130 WHERE "blah" = "bleh" AND ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
131 GROUP BY me.foo, me.bar, me.hello, me.goodbye
1b658919 132 ORDER BY foo, bar, hello, goodbye
31160673 133 ',
134 (1, 2) x 2,
135 666,
136 (1, 2) x 2,
137 'c',
138 ],
139 [ 'COMMIT' ],
140], 'Correct null-delete-SQL with multijoin without pruning' );
141
142
be64931c 143# try the same sql with forced multicolumn in
2cfc22dd 144$schema->is_executed_sql_bind( sub {
b74b15b0 145
146 my $orig_umi = $schema->storage->_use_multicolumn_in;
bbf6a9a5 147 my $sg = scope_guard {
b74b15b0 148 $schema->storage->_use_multicolumn_in($orig_umi);
bbf6a9a5 149 };
b74b15b0 150
151 $schema->storage->_use_multicolumn_in(1);
2cfc22dd 152
153 # this can't actually execute on sqlite
154 eval { $fks_multi->update ({ read_count => \ 'read_count + 1' }) };
155}, [[
be64931c 156 'UPDATE fourkeys
157 SET read_count = read_count + 1
158 WHERE (
159 (foo, bar, hello, goodbye) IN (
160 SELECT me.foo, me.bar, me.hello, me.goodbye
161 FROM fourkeys me
fd8076c8 162 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys ON
163 fourkeys_to_twokeys.f_bar = me.bar
164 AND fourkeys_to_twokeys.f_foo = me.foo
165 AND fourkeys_to_twokeys.f_goodbye = me.goodbye
166 AND fourkeys_to_twokeys.f_hello = me.hello
8d005ad9 167 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
1b658919 168 ORDER BY foo, bar, hello, goodbye
be64931c 169 )
170 )
171 ',
2cfc22dd 172 ( 1, 2) x 2,
173 666,
174 ( 1, 2) x 2,
175 'c',
176]], 'Correct update-SQL with multicolumn in support' );
177
178$schema->is_executed_sql_bind( sub {
179 $fks->search({ 'twokeys.artist' => { '!=' => 666 } })->update({ read_count => \ 'read_count + 1' });
180}, [
181 [ 'BEGIN' ],
fd8076c8 182 [
2cfc22dd 183 'SELECT me.foo, me.bar, me.hello, me.goodbye
184 FROM fourkeys me
185 LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
186 ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
187 LEFT JOIN twokeys twokeys
188 ON twokeys.artist = fourkeys_to_twokeys.t_artist AND twokeys.cd = fourkeys_to_twokeys.t_cd
189 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ? AND twokeys.artist != ?
190 GROUP BY me.foo, me.bar, me.hello, me.goodbye
191 ',
192 (1, 2) x 4,
193 'c',
194 666,
fd8076c8 195 ],
2cfc22dd 196 [
197 'UPDATE fourkeys
198 SET read_count = read_count + 1
199 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )
200 ',
201 ( (1) x 4, (2) x 4 ),
202 ],
203 [ 'COMMIT' ],
204], 'Correct update-SQL with premultiplied restricting join without pruning' );
eb58c082 205
206is ($fa->discard_changes->read_count, 13, 'Update ran only once on joined resultset');
207is ($fb->discard_changes->read_count, 23, 'Update ran only once on joined resultset');
208is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
209
ff1234ad 210#
be64931c 211# Make sure multicolumn in or the equivalent functions correctly
ff1234ad 212#
213
214my $sub_rs = $tkfks->search (
215 [
216 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
217 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
218 ],
219 {
220 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
221 },
222);
223
224is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
225
226# attempts to delete a grouped rs should fail miserably
227throws_ok (
228 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
229 qr/attempted a delete operation on a resultset which does group_by/,
230 'Grouped rs update/delete not allowed',
231);
232
233# grouping on PKs only should pass
af668ad6 234$sub_rs->search (
235 {},
236 {
be64931c 237 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
af668ad6 238 },
239)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
ff1234ad 240
241is_deeply (
242 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
8273e845 243 ->get_column ('pilot_sequence')->all
ff1234ad 244 ],
245 [qw/11 21 30 40/],
246 'Only two rows incremented',
247);
248
af668ad6 249# also make sure weird scalarref usage works (RT#51409)
250$tkfks->search (
251 \ 'pilot_sequence BETWEEN 11 AND 21',
252)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
253
254is_deeply (
255 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
8273e845 256 ->get_column ('pilot_sequence')->all
af668ad6 257 ],
258 [qw/12 22 30 40/],
259 'Only two rows incremented (where => scalarref works)',
260);
261
59ac6523 262{
263 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
264 {
265 -or => [
266 { 'me.pilot_sequence' => 12 },
267 { 'me.autopilot' => 'b' },
268 ],
269 }
270 );
271 lives_ok { $rs->update({ autopilot => 'z' }) }
272 'Update with table name qualifier in -or conditions lives';
273 is_deeply (
274 [ $tkfks->search ({ pilot_sequence => [12, 22]})
275 ->get_column ('autopilot')->all
276 ],
277 [qw/z z/],
278 '... and yields the right data',
279 );
280}
281
282
ff1234ad 283$sub_rs->delete;
ff1234ad 284is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
fef47a8e 285
286# make sure limit-only deletion works
287cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
288$tkfks->search ({}, { rows => 1 })->delete;
289is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
290
4c41a875 291throws_ok {
292 $tkfks->search ({}, { rows => 0 })->delete
293} qr/rows attribute must be a positive integer/;
294is ($tkfks->count, $tkfk_cnt, 'Nothing deleted');
887d8da0 295
fd8076c8 296# check with sql-equality, as sqlite will accept most bad sql just fine
fd8076c8 297{
298 my $rs = $schema->resultset('CD')->search(
299 { 'me.year' => { '!=' => 2010 } },
300 );
301
2cfc22dd 302 $schema->is_executed_sql_bind( sub {
303 $rs->search({}, { join => 'liner_notes' })->delete;
304 }, [[
fd8076c8 305 'DELETE FROM cd WHERE ( year != ? )',
2cfc22dd 306 2010,
307 ]], 'Non-restricting multijoins properly thrown out' );
fd8076c8 308
2cfc22dd 309 $schema->is_executed_sql_bind( sub {
310 $rs->search({}, { prefetch => 'liner_notes' })->delete;
311 }, [[
fd8076c8 312 'DELETE FROM cd WHERE ( year != ? )',
2cfc22dd 313 2010,
314 ]], 'Non-restricting multiprefetch thrown out' );
fd8076c8 315
2cfc22dd 316 $schema->is_executed_sql_bind( sub {
317 $rs->search({}, { prefetch => 'artist' })->delete;
318 }, [[
fd8076c8 319 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me JOIN artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
2cfc22dd 320 2010,
321 ]], 'Restricting prefetch left in, selector thrown out');
fd8076c8 322
2cfc22dd 323### switch artist and cd to fully qualified table names
324### make sure nothing is stripped out
554f3621 325 my $cd_rsrc = $schema->source('CD');
326 $cd_rsrc->name('main.cd');
327 $cd_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
328 for $cd_rsrc->relationships;
329
330 my $art_rsrc = $schema->source('Artist');
331 $art_rsrc->name(\'main.artist');
332 $art_rsrc->relationship_info($_)->{attrs}{cascade_delete} = 0
333 for $art_rsrc->relationships;
334
2cfc22dd 335 $schema->is_executed_sql_bind( sub {
336 $rs->delete
337 }, [[
338 'DELETE FROM main.cd WHERE year != ?',
339 2010,
340 ]], 'delete with fully qualified table name' );
554f3621 341
342 $rs->create({ title => 'foo', artist => 1, year => 2000 });
2cfc22dd 343 $schema->is_executed_sql_bind( sub {
344 $rs->delete_all
345 }, [
346 [ 'BEGIN' ],
347 [
348 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM main.cd me WHERE me.year != ?',
349 2010,
350 ],
351 [
352 'DELETE FROM main.cd WHERE ( cdid = ? )',
353 1,
354 ],
355 [ 'COMMIT' ],
356 ], 'delete_all with fully qualified table name' );
554f3621 357
358 $rs->create({ cdid => 42, title => 'foo', artist => 2, year => 2000 });
2cfc22dd 359 my $cd42 = $rs->find(42);
360
361 $schema->is_executed_sql_bind( sub {
362 $cd42->delete
363 }, [[
364 'DELETE FROM main.cd WHERE cdid = ?',
365 42,
366 ]], 'delete of object from table with fully qualified name' );
367
368 $schema->is_executed_sql_bind( sub {
369 $cd42->related_resultset('artist')->delete
370 }, [[
554f3621 371 'DELETE FROM main.artist WHERE ( artistid IN ( SELECT me.artistid FROM main.artist me WHERE ( me.artistid = ? ) ) )',
2cfc22dd 372 2,
373 ]], 'delete of related object from scalarref fully qualified named table' );
374
375 my $art3 = $schema->resultset('Artist')->find(3);
554f3621 376
2cfc22dd 377 $schema->is_executed_sql_bind( sub {
378 $art3->related_resultset('cds')->delete;
379 }, [[
554f3621 380 'DELETE FROM main.cd WHERE ( artist = ? )',
2cfc22dd 381 3,
382 ]], 'delete of related object from fully qualified named table' );
fd8076c8 383
2cfc22dd 384 $schema->is_executed_sql_bind( sub {
385 $art3->cds_unordered->delete;
386 }, [[
554f3621 387 'DELETE FROM main.cd WHERE ( artist = ? )',
2cfc22dd 388 3,
389 ]], 'delete of related object from fully qualified named table via relaccessor' );
554f3621 390
2cfc22dd 391 $schema->is_executed_sql_bind( sub {
392 $rs->search({}, { prefetch => 'artist' })->delete;
393 }, [[
554f3621 394 'DELETE FROM main.cd WHERE ( cdid IN ( SELECT me.cdid FROM main.cd me JOIN main.artist artist ON artist.artistid = me.artist WHERE ( me.year != ? ) ) )',
2cfc22dd 395 2010,
396 ]], 'delete with fully qualified table name and subquery correct' );
fd8076c8 397
31073ac7 398 # check that as_subselect_rs works ok
399 # inner query is untouched, then a selector
400 # and an IN condition
2cfc22dd 401 $schema->is_executed_sql_bind( sub {
402 $schema->resultset('CD')->search({
403 'me.cdid' => 1,
404 'artist.name' => 'partytimecity',
405 }, {
406 join => 'artist',
407 })->as_subselect_rs->delete;
408 }, [[
31073ac7 409 '
554f3621 410 DELETE FROM main.cd
31073ac7 411 WHERE (
412 cdid IN (
413 SELECT me.cdid
414 FROM (
415 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
554f3621 416 FROM main.cd me
417 JOIN main.artist artist ON artist.artistid = me.artist
31073ac7 418 WHERE artist.name = ? AND me.cdid = ?
419 ) me
420 )
421 )
422 ',
2cfc22dd 423 'partytimecity',
424 1,
425 ]], 'Delete from as_subselect_rs works correctly' );
fd8076c8 426}
887d8da0 427
fef47a8e 428done_testing;