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