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