Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
637ca936 |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
637ca936 |
7 | |
8 | eval "use SQL::Translator"; |
9 | plan skip_all => 'SQL::Translator required' if $@; |
10 | |
661fc8eb |
11 | my $schema = DBICTest->init_schema; |
637ca936 |
12 | |
1f5bf324 |
13 | plan tests => 133; |
637ca936 |
14 | |
661fc8eb |
15 | my $translator = SQL::Translator->new( |
16 | parser_args => { |
17 | 'DBIx::Schema' => $schema, |
18 | }, |
19 | producer_args => {}, |
637ca936 |
20 | ); |
21 | |
e377d723 |
22 | { |
23 | my $warn = ''; |
24 | local $SIG{__WARN__} = sub { $warn = shift }; |
637ca936 |
25 | |
e377d723 |
26 | my $relinfo = $schema->source('Artist')->relationship_info ('cds'); |
27 | local $relinfo->{attrs}{on_delete} = 'restrict'; |
637ca936 |
28 | |
f89bb832 |
29 | $schema->source('Track')->sqlt_deploy_callback(sub { |
30 | my ($self, $sqlt_table) = @_; |
31 | |
1f5bf324 |
32 | if ($schema->storage->sqlt_type eq 'SQLite' ) { |
f89bb832 |
33 | $sqlt_table->add_index( name => 'track_title', fields => ['title'] ) |
34 | or die $sqlt_table->error; |
35 | } |
36 | |
37 | $self->default_sqlt_deploy_hook($sqlt_table); |
38 | }); |
39 | |
e377d723 |
40 | $translator->parser('SQL::Translator::Parser::DBIx::Class'); |
41 | $translator->producer('SQLite'); |
256e87b0 |
42 | |
e377d723 |
43 | my $output = $translator->translate(); |
44 | |
45 | ok($output, "SQLT produced someoutput") |
46 | or diag($translator->error); |
47 | |
48 | like ($warn, qr/^SQLT attribute .+? was supplied for relationship/, 'Warn about dubious on_delete/on_update attributes'); |
49 | } |
256e87b0 |
50 | |
b1edf9f9 |
51 | # Note that the constraints listed here are the only ones that are tested -- if |
52 | # more exist in the Schema than are listed here and all listed constraints are |
c75b18e9 |
53 | # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN |
54 | # KEY constraints to DBICTest::Schema, add tests here if you think the existing |
55 | # test coverage is not sufficient |
b1edf9f9 |
56 | |
57 | my %fk_constraints = ( |
661fc8eb |
58 | |
59 | # TwoKeys |
b1edf9f9 |
60 | twokeys => [ |
61 | { |
62 | 'display' => 'twokeys->cd', |
bb0f01d0 |
63 | 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd', |
b1edf9f9 |
64 | 'selftable' => 'twokeys', 'foreigntable' => 'cd', |
65 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
9c1f7965 |
66 | 'noindex' => 1, |
13de943d |
67 | on_delete => '', on_update => '', deferrable => 0, |
b1edf9f9 |
68 | }, |
69 | { |
70 | 'display' => 'twokeys->artist', |
bb0f01d0 |
71 | 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist', |
b1edf9f9 |
72 | 'selftable' => 'twokeys', 'foreigntable' => 'artist', |
73 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
e394339b |
74 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
75 | }, |
76 | ], |
661fc8eb |
77 | |
78 | # FourKeys_to_TwoKeys |
b1edf9f9 |
79 | fourkeys_to_twokeys => [ |
80 | { |
81 | 'display' => 'fourkeys_to_twokeys->twokeys', |
f34cb1fd |
82 | 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd', |
b1edf9f9 |
83 | 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys', |
84 | 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'], |
e394339b |
85 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
86 | }, |
87 | { |
f34cb1fd |
88 | 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye', |
d1b264d3 |
89 | 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye', |
b1edf9f9 |
90 | 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys', |
91 | 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)], |
92 | 'foreigncols' => [qw(foo bar hello goodbye)], |
e394339b |
93 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
94 | }, |
95 | ], |
661fc8eb |
96 | |
97 | # CD_to_Producer |
b1edf9f9 |
98 | cd_to_producer => [ |
99 | { |
100 | 'display' => 'cd_to_producer->cd', |
bb0f01d0 |
101 | 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd', |
b1edf9f9 |
102 | 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', |
103 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
e394339b |
104 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
105 | }, |
106 | { |
107 | 'display' => 'cd_to_producer->producer', |
bb0f01d0 |
108 | 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer', |
b1edf9f9 |
109 | 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer', |
110 | 'selfcols' => ['producer'], 'foreigncols' => ['producerid'], |
e394339b |
111 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
112 | }, |
113 | ], |
661fc8eb |
114 | |
115 | # Self_ref_alias |
b1edf9f9 |
116 | self_ref_alias => [ |
117 | { |
118 | 'display' => 'self_ref_alias->self_ref for self_ref', |
bb0f01d0 |
119 | 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref', |
b1edf9f9 |
120 | 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', |
121 | 'selfcols' => ['self_ref'], 'foreigncols' => ['id'], |
e394339b |
122 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
123 | }, |
124 | { |
125 | 'display' => 'self_ref_alias->self_ref for alias', |
bb0f01d0 |
126 | 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias', |
b1edf9f9 |
127 | 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', |
128 | 'selfcols' => ['alias'], 'foreigncols' => ['id'], |
e394339b |
129 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
130 | }, |
131 | ], |
661fc8eb |
132 | |
133 | # CD |
b1edf9f9 |
134 | cd => [ |
135 | { |
136 | 'display' => 'cd->artist', |
bb0f01d0 |
137 | 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist', |
b1edf9f9 |
138 | 'selftable' => 'cd', 'foreigntable' => 'artist', |
139 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
a0dd8679 |
140 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
141 | }, |
142 | ], |
661fc8eb |
143 | |
144 | # Artist_undirected_map |
b1edf9f9 |
145 | artist_undirected_map => [ |
146 | { |
147 | 'display' => 'artist_undirected_map->artist for id1', |
bb0f01d0 |
148 | 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1', |
b1edf9f9 |
149 | 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', |
150 | 'selfcols' => ['id1'], 'foreigncols' => ['artistid'], |
e377d723 |
151 | on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
152 | }, |
153 | { |
154 | 'display' => 'artist_undirected_map->artist for id2', |
bb0f01d0 |
155 | 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2', |
b1edf9f9 |
156 | 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', |
157 | 'selfcols' => ['id2'], 'foreigncols' => ['artistid'], |
e377d723 |
158 | on_delete => '', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
159 | }, |
160 | ], |
661fc8eb |
161 | |
162 | # Track |
b1edf9f9 |
163 | track => [ |
164 | { |
165 | 'display' => 'track->cd', |
bb0f01d0 |
166 | 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd', |
b1edf9f9 |
167 | 'selftable' => 'track', 'foreigntable' => 'cd', |
168 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
e394339b |
169 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
170 | }, |
171 | ], |
661fc8eb |
172 | |
173 | # TreeLike |
b1edf9f9 |
174 | treelike => [ |
175 | { |
176 | 'display' => 'treelike->treelike for parent', |
61177e44 |
177 | 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent', |
b1edf9f9 |
178 | 'selftable' => 'treelike', 'foreigntable' => 'treelike', |
61177e44 |
179 | 'selfcols' => ['parent'], 'foreigncols' => ['id'], |
e394339b |
180 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
181 | }, |
182 | ], |
183 | |
184 | # TwoKeyTreeLike |
185 | twokeytreelike => [ |
186 | { |
187 | 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2', |
bb0f01d0 |
188 | 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2', |
b1edf9f9 |
189 | 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike', |
190 | 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'], |
e394339b |
191 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
192 | }, |
193 | ], |
ae515736 |
194 | |
661fc8eb |
195 | # Tags |
b1edf9f9 |
196 | tags => [ |
197 | { |
198 | 'display' => 'tags->cd', |
bb0f01d0 |
199 | 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd', |
b1edf9f9 |
200 | 'selftable' => 'tags', 'foreigntable' => 'cd', |
201 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
e394339b |
202 | on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1, |
b1edf9f9 |
203 | }, |
204 | ], |
661fc8eb |
205 | |
206 | # Bookmark |
b1edf9f9 |
207 | bookmark => [ |
208 | { |
209 | 'display' => 'bookmark->link', |
bb0f01d0 |
210 | 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link', |
b1edf9f9 |
211 | 'selftable' => 'bookmark', 'foreigntable' => 'link', |
212 | 'selfcols' => ['link'], 'foreigncols' => ['id'], |
e394339b |
213 | on_delete => '', on_update => '', deferrable => 1, |
b1edf9f9 |
214 | }, |
215 | ], |
a0024650 |
216 | # ForceForeign |
217 | forceforeign => [ |
218 | { |
219 | 'display' => 'forceforeign->artist', |
bb0f01d0 |
220 | 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist', |
a0024650 |
221 | 'selftable' => 'forceforeign', 'foreigntable' => 'artist', |
8871d4ad |
222 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
e394339b |
223 | on_delete => '', on_update => '', deferrable => 1, |
a0024650 |
224 | }, |
225 | ], |
b1edf9f9 |
226 | ); |
227 | |
228 | my %unique_constraints = ( |
229 | # CD |
230 | cd => [ |
231 | { |
232 | 'display' => 'cd artist and title unique', |
0da8b7da |
233 | 'name' => 'cd_artist_title', |
b1edf9f9 |
234 | 'table' => 'cd', 'cols' => ['artist', 'title'], |
235 | }, |
236 | ], |
237 | |
238 | # Producer |
239 | producer => [ |
240 | { |
241 | 'display' => 'producer name unique', |
0da8b7da |
242 | 'name' => 'prod_name', # explicit name |
b1edf9f9 |
243 | 'table' => 'producer', 'cols' => ['name'], |
244 | }, |
245 | ], |
246 | |
247 | # TwoKeyTreeLike |
248 | twokeytreelike => [ |
249 | { |
250 | 'display' => 'twokeytreelike name unique', |
0da8b7da |
251 | 'name' => 'tktlnameunique', # explicit name |
b1edf9f9 |
252 | 'table' => 'twokeytreelike', 'cols' => ['name'], |
253 | }, |
254 | ], |
255 | |
256 | # Employee |
257 | # Constraint is commented out in DBICTest/Schema/Employee.pm |
258 | # employee => [ |
259 | # { |
260 | # 'display' => 'employee position and group_id unique', |
0da8b7da |
261 | # 'name' => 'position_group', |
b1edf9f9 |
262 | # 'table' => 'employee', cols => ['position', 'group_id'], |
263 | # }, |
264 | # ], |
7b90bb13 |
265 | ); |
266 | |
17cab2f0 |
267 | my %indexes = ( |
c385ecea |
268 | artist => [ |
269 | { |
270 | 'fields' => ['name'] |
271 | }, |
f89bb832 |
272 | ], |
273 | track => [ |
274 | { |
275 | 'fields' => ['title'] |
276 | } |
277 | ], |
c385ecea |
278 | ); |
279 | |
637ca936 |
280 | my $tschema = $translator->schema(); |
d6c79cb3 |
281 | # Test that the $schema->sqlt_deploy_hook was called okay and that it removed |
458e0292 |
282 | # the 'dummy' table |
283 | ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook"); |
d6c79cb3 |
284 | |
1f5bf324 |
285 | # Test that the Artist resultsource sqlt_deploy_hook was called okay and added |
286 | # an index |
287 | SKIP: { |
288 | skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1) |
289 | if $schema->storage->sqlt_type ne 'SQLite'; |
290 | |
291 | ok( ( grep |
292 | { $_->name eq 'artist_name_hookidx' } |
293 | $tschema->get_table('artist')->get_indices |
294 | ), 'sqlt_deploy_hook fired within a resultsource'); |
295 | } |
296 | |
b1edf9f9 |
297 | # Test that nonexistent constraints are not found |
298 | my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']); |
299 | ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' ); |
300 | $constraint = get_constraint('UNIQUE', 'cd', ['artist']); |
301 | ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' ); |
a0024650 |
302 | $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']); |
303 | ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' ); |
b1edf9f9 |
304 | |
305 | for my $expected_constraints (keys %fk_constraints) { |
306 | for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) { |
307 | my $desc = $expected_constraint->{display}; |
308 | my $constraint = get_constraint( |
309 | 'FOREIGN KEY', |
310 | $expected_constraint->{selftable}, $expected_constraint->{selfcols}, |
311 | $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols}, |
312 | ); |
313 | ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" ); |
314 | test_fk($expected_constraint, $constraint); |
661fc8eb |
315 | } |
637ca936 |
316 | } |
317 | |
b1edf9f9 |
318 | for my $expected_constraints (keys %unique_constraints) { |
319 | for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) { |
320 | my $desc = $expected_constraint->{display}; |
321 | my $constraint = get_constraint( |
322 | 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols}, |
323 | ); |
324 | ok( defined($constraint), "UNIQUE constraint matching `$desc' found" ); |
0da8b7da |
325 | test_unique($expected_constraint, $constraint); |
b1edf9f9 |
326 | } |
637ca936 |
327 | } |
328 | |
17cab2f0 |
329 | for my $table_index (keys %indexes) { |
330 | for my $expected_index ( @{ $indexes{$table_index} } ) { |
c385ecea |
331 | ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table"); |
332 | } |
333 | } |
334 | |
b1edf9f9 |
335 | # Returns the Constraint object for the specified constraint type, table and |
336 | # columns from the SQL::Translator schema, or undef if no matching constraint |
337 | # is found. |
338 | # |
339 | # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last |
340 | # two parameters are not used. |
341 | sub get_constraint { |
342 | my ($type, $table_name, $cols, $f_table, $f_cols) = @_; |
343 | $f_table ||= ''; # For UNIQUE constraints, reference_table is '' |
344 | $f_cols ||= []; |
345 | |
346 | my $table = $tschema->get_table($table_name); |
347 | |
348 | my %fields = map { $_ => 1 } @$cols; |
349 | my %f_fields = map { $_ => 1 } @$f_cols; |
350 | |
a7e65bb5 |
351 | die "No $table_name" unless $table; |
b1edf9f9 |
352 | CONSTRAINT: |
353 | for my $constraint ( $table->get_constraints ) { |
354 | next unless $constraint->type eq $type; |
355 | next unless $constraint->reference_table eq $f_table; |
356 | |
357 | my %rev_fields = map { $_ => 1 } $constraint->fields; |
358 | my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields; |
359 | |
360 | # Check that the given fields are a subset of the constraint's fields |
361 | for my $field ($constraint->fields) { |
362 | next CONSTRAINT unless $fields{$field}; |
363 | } |
364 | if ($type eq 'FOREIGN KEY') { |
365 | for my $f_field ($constraint->reference_fields) { |
366 | next CONSTRAINT unless $f_fields{$f_field}; |
661fc8eb |
367 | } |
b1edf9f9 |
368 | } |
661fc8eb |
369 | |
b1edf9f9 |
370 | # Check that the constraint's fields are a subset of the given fields |
371 | for my $field (@$cols) { |
372 | next CONSTRAINT unless $rev_fields{$field}; |
373 | } |
374 | if ($type eq 'FOREIGN KEY') { |
375 | for my $f_field (@$f_cols) { |
376 | next CONSTRAINT unless $rev_f_fields{$f_field}; |
661fc8eb |
377 | } |
378 | } |
b1edf9f9 |
379 | |
380 | return $constraint; # everything passes, found the constraint |
661fc8eb |
381 | } |
b1edf9f9 |
382 | return undef; # didn't find a matching constraint |
7b90bb13 |
383 | } |
384 | |
c385ecea |
385 | sub get_index { |
386 | my ($table_name, $index) = @_; |
387 | |
388 | my $table = $tschema->get_table($table_name); |
389 | |
390 | CAND_INDEX: |
391 | for my $cand_index ( $table->get_indices ) { |
392 | |
393 | next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name} |
394 | || $index->{type} && $cand_index->type ne $index->{type}; |
395 | |
396 | my %idx_fields = map { $_ => 1 } $cand_index->fields; |
397 | |
398 | for my $field ( @{ $index->{fields} } ) { |
399 | next CAND_INDEX unless $idx_fields{$field}; |
400 | } |
401 | |
402 | %idx_fields = map { $_ => 1 } @{$index->{fields}}; |
403 | for my $field ( $cand_index->fields) { |
404 | next CAND_INDEX unless $idx_fields{$field}; |
405 | } |
406 | |
407 | return $cand_index; |
408 | } |
409 | |
410 | return undef; # No matching idx |
411 | } |
412 | |
b1edf9f9 |
413 | # Test parameters in a FOREIGN KEY constraint other than columns |
414 | sub test_fk { |
415 | my ($expected, $got) = @_; |
416 | my $desc = $expected->{display}; |
0da8b7da |
417 | is( $got->name, $expected->{name}, |
418 | "name parameter correct for `$desc'" ); |
b1edf9f9 |
419 | is( $got->on_delete, $expected->{on_delete}, |
420 | "on_delete parameter correct for `$desc'" ); |
421 | is( $got->on_update, $expected->{on_update}, |
422 | "on_update parameter correct for `$desc'" ); |
13de943d |
423 | is( $got->deferrable, $expected->{deferrable}, |
424 | "is_deferrable parameter correct for `$desc'" ); |
0da8b7da |
425 | |
426 | my $index = get_index( $got->table, { fields => $expected->{selfcols} } ); |
9c1f7965 |
427 | |
428 | if ($expected->{noindex}) { |
429 | ok( !defined $index, "index doesn't for `$desc'" ); |
430 | } else { |
431 | ok( defined $index, "index exists for `$desc'" ); |
432 | is( $index->name, $expected->{index_name}, "index has correct name for `$desc'" ); |
433 | } |
0da8b7da |
434 | } |
435 | |
436 | sub test_unique { |
437 | my ($expected, $got) = @_; |
438 | my $desc = $expected->{display}; |
439 | is( $got->name, $expected->{name}, |
440 | "name parameter correct for `$desc'" ); |
637ca936 |
441 | } |