8 eval "use SQL::Translator";
9 plan skip_all => 'SQL::Translator required' if $@;
11 my $schema = DBICTest->init_schema;
15 my $translator = SQL::Translator->new(
17 'DBIx::Schema' => $schema,
22 $translator->parser('SQL::Translator::Parser::DBIx::Class');
23 $translator->producer('SQLite');
25 my $output = $translator->translate();
28 ok($output, "SQLT produced someoutput")
29 or diag($translator->error);
32 # Note that the constraints listed here are the only ones that are tested -- if
33 # more exist in the Schema than are listed here and all listed constraints are
34 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
35 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
36 # test coverage is not sufficient
38 my %fk_constraints = (
43 'display' => 'twokeys->cd',
44 'selftable' => 'twokeys', 'foreigntable' => 'cd',
45 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
46 on_delete => '', on_update => '',
49 'display' => 'twokeys->artist',
50 'selftable' => 'twokeys', 'foreigntable' => 'artist',
51 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
52 on_delete => 'CASCADE', on_update => 'CASCADE',
57 fourkeys_to_twokeys => [
59 'display' => 'fourkeys_to_twokeys->twokeys',
60 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
61 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
62 on_delete => 'CASCADE', on_update => 'CASCADE',
65 'display' => 'fourkeys_to_twokeys->fourkeys',
66 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
67 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
68 'foreigncols' => [qw(foo bar hello goodbye)],
69 on_delete => 'CASCADE', on_update => 'CASCADE',
76 'display' => 'cd_to_producer->cd',
77 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
78 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
79 on_delete => 'CASCADE', on_update => 'CASCADE',
82 'display' => 'cd_to_producer->producer',
83 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
84 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
85 on_delete => '', on_update => '',
92 'display' => 'self_ref_alias->self_ref for self_ref',
93 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
94 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
95 on_delete => 'CASCADE', on_update => 'CASCADE',
98 'display' => 'self_ref_alias->self_ref for alias',
99 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
100 'selfcols' => ['alias'], 'foreigncols' => ['id'],
101 on_delete => '', on_update => '',
108 'display' => 'cd->artist',
109 'selftable' => 'cd', 'foreigntable' => 'artist',
110 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
111 on_delete => 'CASCADE', on_update => 'CASCADE',
115 # Artist_undirected_map
116 artist_undirected_map => [
118 'display' => 'artist_undirected_map->artist for id1',
119 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
120 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
121 on_delete => 'CASCADE', on_update => '',
124 'display' => 'artist_undirected_map->artist for id2',
125 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
126 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
127 on_delete => 'CASCADE', on_update => '',
134 'display' => 'track->cd',
135 'selftable' => 'track', 'foreigntable' => 'cd',
136 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
137 on_delete => 'CASCADE', on_update => 'CASCADE',
144 'display' => 'treelike->treelike for parent',
145 'selftable' => 'treelike', 'foreigntable' => 'treelike',
146 'selfcols' => ['parent'], 'foreigncols' => ['id'],
147 on_delete => 'CASCADE', on_update => 'CASCADE',
154 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
155 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
156 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
157 on_delete => '', on_update => '',
164 'display' => 'tags->cd',
165 'selftable' => 'tags', 'foreigntable' => 'cd',
166 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
167 on_delete => 'CASCADE', on_update => 'CASCADE',
174 'display' => 'bookmark->link',
175 'selftable' => 'bookmark', 'foreigntable' => 'link',
176 'selfcols' => ['link'], 'foreigncols' => ['id'],
177 on_delete => '', on_update => '',
182 my %unique_constraints = (
186 'display' => 'cd artist and title unique',
187 'table' => 'cd', 'cols' => ['artist', 'title'],
194 'display' => 'producer name unique',
195 'table' => 'producer', 'cols' => ['name'],
202 'display' => 'twokeytreelike name unique',
203 'table' => 'twokeytreelike', 'cols' => ['name'],
208 # Constraint is commented out in DBICTest/Schema/Employee.pm
211 # 'display' => 'employee position and group_id unique',
212 # 'table' => 'employee', cols => ['position', 'group_id'],
225 my $tschema = $translator->schema();
227 # Test that nonexistent constraints are not found
228 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
229 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
230 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
231 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
233 for my $expected_constraints (keys %fk_constraints) {
234 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
235 my $desc = $expected_constraint->{display};
236 my $constraint = get_constraint(
238 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
239 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
241 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
242 test_fk($expected_constraint, $constraint);
246 for my $expected_constraints (keys %unique_constraints) {
247 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
248 my $desc = $expected_constraint->{display};
249 my $constraint = get_constraint(
250 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
252 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
256 for my $table_index (keys %indexes) {
257 for my $expected_index ( @{ $indexes{$table_index} } ) {
259 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
263 # Returns the Constraint object for the specified constraint type, table and
264 # columns from the SQL::Translator schema, or undef if no matching constraint
267 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
268 # two parameters are not used.
270 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
271 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
274 my $table = $tschema->get_table($table_name);
276 my %fields = map { $_ => 1 } @$cols;
277 my %f_fields = map { $_ => 1 } @$f_cols;
280 for my $constraint ( $table->get_constraints ) {
281 next unless $constraint->type eq $type;
282 next unless $constraint->reference_table eq $f_table;
284 my %rev_fields = map { $_ => 1 } $constraint->fields;
285 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
287 # Check that the given fields are a subset of the constraint's fields
288 for my $field ($constraint->fields) {
289 next CONSTRAINT unless $fields{$field};
291 if ($type eq 'FOREIGN KEY') {
292 for my $f_field ($constraint->reference_fields) {
293 next CONSTRAINT unless $f_fields{$f_field};
297 # Check that the constraint's fields are a subset of the given fields
298 for my $field (@$cols) {
299 next CONSTRAINT unless $rev_fields{$field};
301 if ($type eq 'FOREIGN KEY') {
302 for my $f_field (@$f_cols) {
303 next CONSTRAINT unless $rev_f_fields{$f_field};
307 return $constraint; # everything passes, found the constraint
309 return undef; # didn't find a matching constraint
313 my ($table_name, $index) = @_;
315 my $table = $tschema->get_table($table_name);
318 for my $cand_index ( $table->get_indices ) {
320 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
321 || $index->{type} && $cand_index->type ne $index->{type};
323 my %idx_fields = map { $_ => 1 } $cand_index->fields;
325 for my $field ( @{ $index->{fields} } ) {
326 next CAND_INDEX unless $idx_fields{$field};
329 %idx_fields = map { $_ => 1 } @{$index->{fields}};
330 for my $field ( $cand_index->fields) {
331 next CAND_INDEX unless $idx_fields{$field};
337 return undef; # No matching idx
340 # Test parameters in a FOREIGN KEY constraint other than columns
342 my ($expected, $got) = @_;
343 my $desc = $expected->{display};
344 is( $got->on_delete, $expected->{on_delete},
345 "on_delete parameter correct for `$desc'" );
346 is( $got->on_update, $expected->{on_update},
347 "on_update parameter correct for `$desc'" );