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