Majorly cleanup $rs->update/delete (no $rs-aware code should be in ::Storages)
[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;
7use DBICTest;
887d8da0 8use DBIC::DebugObj;
9use DBIC::SqlMakerTest;
ff1234ad 10
be64931c 11my $schema = DBICTest->init_schema;
12
13my ($sql, @bind);
14my $debugobj = DBIC::DebugObj->new (\$sql, \@bind);
15my $orig_debugobj = $schema->storage->debugobj;
16my $orig_debug = $schema->storage->debug;
ff1234ad 17
18my $tkfks = $schema->resultset('FourKeys_to_TwoKeys');
19
ff1234ad 20my ($fa, $fb) = $tkfks->related_resultset ('fourkeys')->populate ([
21 [qw/foo bar hello goodbye sensors read_count/],
22 [qw/1 1 1 1 a 10 /],
23 [qw/2 2 2 2 b 20 /],
24]);
25
26# This is already provided by DBICTest
27#my ($ta, $tb) = $tkfk->related_resultset ('twokeys')->populate ([
28# [qw/artist cd /],
29# [qw/1 1 /],
30# [qw/2 2 /],
31#]);
32my ($ta, $tb) = $schema->resultset ('TwoKeys')
33 ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
34 ->all;
35
36my $tkfk_cnt = $tkfks->count;
37
38my $non_void_ctx = $tkfks->populate ([
39 { autopilot => 'a', fourkeys => $fa, twokeys => $ta, pilot_sequence => 10 },
40 { autopilot => 'b', fourkeys => $fb, twokeys => $tb, pilot_sequence => 20 },
41 { autopilot => 'x', fourkeys => $fa, twokeys => $tb, pilot_sequence => 30 },
42 { autopilot => 'y', fourkeys => $fb, twokeys => $ta, pilot_sequence => 40 },
43]);
44is ($tkfks->count, $tkfk_cnt += 4, 'FourKeys_to_TwoKeys populated succesfully');
45
46#
47# Make sure the forced group by works (i.e. the joining does not cause double-updates)
48#
49
50# create a resultset matching $fa and $fb only
51my $fks = $schema->resultset ('FourKeys')
52 ->search ({ map { $_ => [1, 2] } qw/foo bar hello goodbye/}, { join => 'fourkeys_to_twokeys' });
53
54is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
be64931c 55
56$schema->storage->debugobj ($debugobj);
57$schema->storage->debug (1);
ff1234ad 58$fks->update ({ read_count => \ 'read_count + 1' });
be64931c 59$schema->storage->debugobj ($orig_debugobj);
60$schema->storage->debug ($orig_debug);
ff1234ad 61
be64931c 62is_same_sql_bind (
63 $sql,
64 \@bind,
65 'UPDATE fourkeys
66 SET read_count = read_count + 1
67 WHERE ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? ) OR ( bar = ? AND foo = ? AND goodbye = ? AND hello = ? )',
68 [ map { "'$_'" } ( (1) x 4, (2) x 4 ) ],
69 'Correct update-SQL without multicolumn in support',
70);
ff1234ad 71
be64931c 72is ($fa->discard_changes->read_count, 11, 'Update ran only once on joined resultset');
73is ($fb->discard_changes->read_count, 21, 'Update ran only once on joined resultset');
74
75# try the same sql with forced multicolumn in
76$schema->storage->_use_multicolumn_in (1);
77$schema->storage->debugobj ($debugobj);
78$schema->storage->debug (1);
79eval { $fks->update ({ read_count => \ 'read_count + 1' }) }; # this can't actually execute, we just need the "as_query"
80$schema->storage->_use_multicolumn_in (undef);
81$schema->storage->debugobj ($orig_debugobj);
82$schema->storage->debug ($orig_debug);
83
84is_same_sql_bind (
85 $sql,
86 \@bind,
87 'UPDATE fourkeys
88 SET read_count = read_count + 1
89 WHERE (
90 (foo, bar, hello, goodbye) IN (
91 SELECT me.foo, me.bar, me.hello, me.goodbye
92 FROM fourkeys me
93 WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? )
94 )
95 )
96 ',
97 [ map { "'$_'" } ( (1, 2) x 4 ) ],
98 'Correct update-SQL with multicolumn in support',
99);
ff1234ad 100
101#
be64931c 102# Make sure multicolumn in or the equivalent functions correctly
ff1234ad 103#
104
105my $sub_rs = $tkfks->search (
106 [
107 { map { $_ => 1 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
108 { map { $_ => 2 } qw/artist.artistid cd.cdid fourkeys.foo fourkeys.bar fourkeys.hello fourkeys.goodbye/ },
109 ],
110 {
111 join => [ 'fourkeys', { twokeys => [qw/artist cd/] } ],
112 },
113);
114
115is ($sub_rs->count, 2, 'Only two rows from fourkeys match');
116
117# attempts to delete a grouped rs should fail miserably
118throws_ok (
119 sub { $sub_rs->search ({}, { distinct => 1 })->delete },
120 qr/attempted a delete operation on a resultset which does group_by/,
121 'Grouped rs update/delete not allowed',
122);
123
124# grouping on PKs only should pass
af668ad6 125$sub_rs->search (
126 {},
127 {
be64931c 128 group_by => [ reverse $sub_rs->result_source->primary_columns ], # reverse to make sure the PK-list comparison works
af668ad6 129 },
130)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
ff1234ad 131
132is_deeply (
133 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
8273e845 134 ->get_column ('pilot_sequence')->all
ff1234ad 135 ],
136 [qw/11 21 30 40/],
137 'Only two rows incremented',
138);
139
af668ad6 140# also make sure weird scalarref usage works (RT#51409)
141$tkfks->search (
142 \ 'pilot_sequence BETWEEN 11 AND 21',
143)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
144
145is_deeply (
146 [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
8273e845 147 ->get_column ('pilot_sequence')->all
af668ad6 148 ],
149 [qw/12 22 30 40/],
150 'Only two rows incremented (where => scalarref works)',
151);
152
59ac6523 153{
154 my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
155 {
156 -or => [
157 { 'me.pilot_sequence' => 12 },
158 { 'me.autopilot' => 'b' },
159 ],
160 }
161 );
162 lives_ok { $rs->update({ autopilot => 'z' }) }
163 'Update with table name qualifier in -or conditions lives';
164 is_deeply (
165 [ $tkfks->search ({ pilot_sequence => [12, 22]})
166 ->get_column ('autopilot')->all
167 ],
168 [qw/z z/],
169 '... and yields the right data',
170 );
171}
172
173
ff1234ad 174$sub_rs->delete;
ff1234ad 175is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
fef47a8e 176
177# make sure limit-only deletion works
178cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
179$tkfks->search ({}, { rows => 1 })->delete;
180is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
181
887d8da0 182
183# Make sure prefetch is properly stripped too
184# check with sql-equality, as sqlite will accept bad sql just fine
be64931c 185$schema->storage->debugobj ($debugobj);
887d8da0 186$schema->storage->debug (1);
187$schema->resultset('CD')->search(
188 { year => { '!=' => 2010 } },
189 { prefetch => 'liner_notes' },
190)->delete;
191
47980c14 192$schema->storage->debugobj ($orig_debugobj);
193$schema->storage->debug ($orig_debug);
194
887d8da0 195is_same_sql_bind (
196 $sql,
197 \@bind,
be64931c 198 'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me WHERE ( year != ? ) ) )',
887d8da0 199 ["'2010'"],
200 'Update on prefetching resultset strips prefetch correctly'
201);
202
fef47a8e 203done_testing;