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);
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
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
37 my %fk_constraints = (
42 'display' => 'twokeys->cd',
43 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
44 'selftable' => 'twokeys', 'foreigntable' => 'cd',
45 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
46 on_delete => '', on_update => '', deferrable => 0,
49 'display' => 'twokeys->artist',
50 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
51 'selftable' => 'twokeys', 'foreigntable' => 'artist',
52 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
53 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
58 fourkeys_to_twokeys => [
60 'display' => 'fourkeys_to_twokeys->twokeys',
61 'name' => 'fourkeys_to_twokeys_fk_t_cd_t_artist', 'index_name' => 'fourkeys_to_twokeys_idx_t_cd_t_artist',
62 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
63 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
64 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
67 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_goodbye_f_hello_f_bar',
68 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_goodbye_f_hello_f_bar',
69 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
70 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
71 'foreigncols' => [qw(foo bar hello goodbye)],
72 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
79 'display' => 'cd_to_producer->cd',
80 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
81 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
82 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
83 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
86 'display' => 'cd_to_producer->producer',
87 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
88 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
89 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
90 on_delete => '', on_update => '', deferrable => 1,
97 'display' => 'self_ref_alias->self_ref for self_ref',
98 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
99 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
100 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
101 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
104 'display' => 'self_ref_alias->self_ref for alias',
105 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
106 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
107 'selfcols' => ['alias'], 'foreigncols' => ['id'],
108 on_delete => '', on_update => '', deferrable => 1,
115 'display' => 'cd->artist',
116 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
117 'selftable' => 'cd', 'foreigntable' => 'artist',
118 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
119 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
123 # Artist_undirected_map
124 artist_undirected_map => [
126 'display' => 'artist_undirected_map->artist for id1',
127 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
128 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
129 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
130 on_delete => 'CASCADE', on_update => '', deferrable => 1,
133 'display' => 'artist_undirected_map->artist for id2',
134 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
135 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
136 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
137 on_delete => 'CASCADE', on_update => '', deferrable => 1,
144 'display' => 'track->cd',
145 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
146 'selftable' => 'track', 'foreigntable' => 'cd',
147 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
148 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
155 'display' => 'treelike->treelike for parent',
156 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
157 'selftable' => 'treelike', 'foreigntable' => 'treelike',
158 'selfcols' => ['parent'], 'foreigncols' => ['id'],
159 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
166 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
167 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
168 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
169 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
170 on_delete => '', on_update => '', deferrable => 1,
177 'display' => 'tags->cd',
178 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
179 'selftable' => 'tags', 'foreigntable' => 'cd',
180 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
181 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
188 'display' => 'bookmark->link',
189 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
190 'selftable' => 'bookmark', 'foreigntable' => 'link',
191 'selfcols' => ['link'], 'foreigncols' => ['id'],
192 on_delete => '', on_update => '', deferrable => 1,
198 'display' => 'forceforeign->artist',
199 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
200 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
201 'selfcols' => ['artist'], 'foreigncols' => ['artist_id'],
202 on_delete => '', on_update => '', deferrable => 1,
208 my %unique_constraints = (
212 'display' => 'cd artist and title unique',
213 'name' => 'cd_artist_title',
214 'table' => 'cd', 'cols' => ['artist', 'title'],
221 'display' => 'producer name unique',
222 'name' => 'prod_name', # explicit name
223 'table' => 'producer', 'cols' => ['name'],
230 'display' => 'twokeytreelike name unique',
231 'name' => 'tktlnameunique', # explicit name
232 'table' => 'twokeytreelike', 'cols' => ['name'],
237 # Constraint is commented out in DBICTest/Schema/Employee.pm
240 # 'display' => 'employee position and group_id unique',
241 # 'name' => 'position_group',
242 # 'table' => 'employee', cols => ['position', 'group_id'],
255 my $tschema = $translator->schema();
256 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
258 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
260 # Test that nonexistent constraints are not found
261 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
262 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
263 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
264 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
265 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
266 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
268 for my $expected_constraints (keys %fk_constraints) {
269 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
270 my $desc = $expected_constraint->{display};
271 my $constraint = get_constraint(
273 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
274 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
276 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
277 test_fk($expected_constraint, $constraint);
281 for my $expected_constraints (keys %unique_constraints) {
282 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
283 my $desc = $expected_constraint->{display};
284 my $constraint = get_constraint(
285 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
287 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
288 test_unique($expected_constraint, $constraint);
292 for my $table_index (keys %indexes) {
293 for my $expected_index ( @{ $indexes{$table_index} } ) {
295 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
299 # Returns the Constraint object for the specified constraint type, table and
300 # columns from the SQL::Translator schema, or undef if no matching constraint
303 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
304 # two parameters are not used.
306 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
307 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
310 my $table = $tschema->get_table($table_name);
312 my %fields = map { $_ => 1 } @$cols;
313 my %f_fields = map { $_ => 1 } @$f_cols;
315 die "No $table_name" unless $table;
317 for my $constraint ( $table->get_constraints ) {
318 next unless $constraint->type eq $type;
319 next unless $constraint->reference_table eq $f_table;
321 my %rev_fields = map { $_ => 1 } $constraint->fields;
322 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
324 # Check that the given fields are a subset of the constraint's fields
325 for my $field ($constraint->fields) {
326 next CONSTRAINT unless $fields{$field};
328 if ($type eq 'FOREIGN KEY') {
329 for my $f_field ($constraint->reference_fields) {
330 next CONSTRAINT unless $f_fields{$f_field};
334 # Check that the constraint's fields are a subset of the given fields
335 for my $field (@$cols) {
336 next CONSTRAINT unless $rev_fields{$field};
338 if ($type eq 'FOREIGN KEY') {
339 for my $f_field (@$f_cols) {
340 next CONSTRAINT unless $rev_f_fields{$f_field};
344 return $constraint; # everything passes, found the constraint
346 return undef; # didn't find a matching constraint
350 my ($table_name, $index) = @_;
352 my $table = $tschema->get_table($table_name);
355 for my $cand_index ( $table->get_indices ) {
357 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
358 || $index->{type} && $cand_index->type ne $index->{type};
360 my %idx_fields = map { $_ => 1 } $cand_index->fields;
362 for my $field ( @{ $index->{fields} } ) {
363 next CAND_INDEX unless $idx_fields{$field};
366 %idx_fields = map { $_ => 1 } @{$index->{fields}};
367 for my $field ( $cand_index->fields) {
368 next CAND_INDEX unless $idx_fields{$field};
374 return undef; # No matching idx
377 # Test parameters in a FOREIGN KEY constraint other than columns
379 my ($expected, $got) = @_;
380 my $desc = $expected->{display};
381 is( $got->name, $expected->{name},
382 "name parameter correct for `$desc'" );
383 is( $got->on_delete, $expected->{on_delete},
384 "on_delete parameter correct for `$desc'" );
385 is( $got->on_update, $expected->{on_update},
386 "on_update parameter correct for `$desc'" );
387 is( $got->deferrable, $expected->{deferrable},
388 "is_deferrable parameter correct for `$desc'" );
390 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
391 ok( defined $index, "index exists for `$desc'" );
392 is( $index->name, $expected->{index_name}, "index has correct name for `$desc'" );
396 my ($expected, $got) = @_;
397 my $desc = $expected->{display};
398 is( $got->name, $expected->{name},
399 "name parameter correct for `$desc'" );