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