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