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'],
47 on_delete => '', on_update => '', deferrable => 0,
50 'display' => 'twokeys->artist',
51 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
52 'selftable' => 'twokeys', 'foreigntable' => 'artist',
53 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
54 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
59 fourkeys_to_twokeys => [
61 'display' => 'fourkeys_to_twokeys->twokeys',
62 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
63 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
64 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
65 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
68 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
69 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
70 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
71 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
72 'foreigncols' => [qw(foo bar hello goodbye)],
73 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
80 'display' => 'cd_to_producer->cd',
81 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
82 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
83 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
84 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
87 'display' => 'cd_to_producer->producer',
88 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
89 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
90 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
91 on_delete => '', on_update => '', deferrable => 1,
98 'display' => 'self_ref_alias->self_ref for self_ref',
99 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
100 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
101 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
102 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
105 'display' => 'self_ref_alias->self_ref for alias',
106 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
107 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
108 'selfcols' => ['alias'], 'foreigncols' => ['id'],
109 on_delete => '', on_update => '', deferrable => 1,
116 'display' => 'cd->artist',
117 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
118 'selftable' => 'cd', 'foreigntable' => 'artist',
119 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
120 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
124 # Artist_undirected_map
125 artist_undirected_map => [
127 'display' => 'artist_undirected_map->artist for id1',
128 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
129 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
130 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
131 on_delete => 'CASCADE', on_update => '', deferrable => 1,
134 'display' => 'artist_undirected_map->artist for id2',
135 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
136 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
137 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
138 on_delete => 'CASCADE', on_update => '', deferrable => 1,
145 'display' => 'track->cd',
146 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
147 'selftable' => 'track', 'foreigntable' => 'cd',
148 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
149 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
156 'display' => 'treelike->treelike for parent',
157 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
158 'selftable' => 'treelike', 'foreigntable' => 'treelike',
159 'selfcols' => ['parent'], 'foreigncols' => ['id'],
160 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
167 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
168 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
169 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
170 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
171 on_delete => '', on_update => '', deferrable => 1,
178 'display' => 'tags->cd',
179 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
180 'selftable' => 'tags', 'foreigntable' => 'cd',
181 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
182 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
189 'display' => 'bookmark->link',
190 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
191 'selftable' => 'bookmark', 'foreigntable' => 'link',
192 'selfcols' => ['link'], 'foreigncols' => ['id'],
193 on_delete => '', on_update => '', deferrable => 1,
199 'display' => 'forceforeign->artist',
200 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
201 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
202 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
203 on_delete => '', on_update => '', deferrable => 1,
209 my %unique_constraints = (
213 'display' => 'cd artist and title unique',
214 'name' => 'cd_artist_title',
215 'table' => 'cd', 'cols' => ['artist', 'title'],
222 'display' => 'producer name unique',
223 'name' => 'prod_name', # explicit name
224 'table' => 'producer', 'cols' => ['name'],
231 'display' => 'twokeytreelike name unique',
232 'name' => 'tktlnameunique', # explicit name
233 'table' => 'twokeytreelike', 'cols' => ['name'],
238 # Constraint is commented out in DBICTest/Schema/Employee.pm
241 # 'display' => 'employee position and group_id unique',
242 # 'name' => 'position_group',
243 # 'table' => 'employee', cols => ['position', 'group_id'],
256 my $tschema = $translator->schema();
257 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
259 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
261 # Test that nonexistent constraints are not found
262 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
263 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
264 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
265 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
266 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
267 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
269 for my $expected_constraints (keys %fk_constraints) {
270 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
271 my $desc = $expected_constraint->{display};
272 my $constraint = get_constraint(
274 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
275 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
277 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
278 test_fk($expected_constraint, $constraint);
282 for my $expected_constraints (keys %unique_constraints) {
283 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
284 my $desc = $expected_constraint->{display};
285 my $constraint = get_constraint(
286 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
288 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
289 test_unique($expected_constraint, $constraint);
293 for my $table_index (keys %indexes) {
294 for my $expected_index ( @{ $indexes{$table_index} } ) {
296 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
300 # Returns the Constraint object for the specified constraint type, table and
301 # columns from the SQL::Translator schema, or undef if no matching constraint
304 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
305 # two parameters are not used.
307 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
308 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
311 my $table = $tschema->get_table($table_name);
313 my %fields = map { $_ => 1 } @$cols;
314 my %f_fields = map { $_ => 1 } @$f_cols;
316 die "No $table_name" unless $table;
318 for my $constraint ( $table->get_constraints ) {
319 next unless $constraint->type eq $type;
320 next unless $constraint->reference_table eq $f_table;
322 my %rev_fields = map { $_ => 1 } $constraint->fields;
323 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
325 # Check that the given fields are a subset of the constraint's fields
326 for my $field ($constraint->fields) {
327 next CONSTRAINT unless $fields{$field};
329 if ($type eq 'FOREIGN KEY') {
330 for my $f_field ($constraint->reference_fields) {
331 next CONSTRAINT unless $f_fields{$f_field};
335 # Check that the constraint's fields are a subset of the given fields
336 for my $field (@$cols) {
337 next CONSTRAINT unless $rev_fields{$field};
339 if ($type eq 'FOREIGN KEY') {
340 for my $f_field (@$f_cols) {
341 next CONSTRAINT unless $rev_f_fields{$f_field};
345 return $constraint; # everything passes, found the constraint
347 return undef; # didn't find a matching constraint
351 my ($table_name, $index) = @_;
353 my $table = $tschema->get_table($table_name);
356 for my $cand_index ( $table->get_indices ) {
358 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
359 || $index->{type} && $cand_index->type ne $index->{type};
361 my %idx_fields = map { $_ => 1 } $cand_index->fields;
363 for my $field ( @{ $index->{fields} } ) {
364 next CAND_INDEX unless $idx_fields{$field};
367 %idx_fields = map { $_ => 1 } @{$index->{fields}};
368 for my $field ( $cand_index->fields) {
369 next CAND_INDEX unless $idx_fields{$field};
375 return undef; # No matching idx
378 # Test parameters in a FOREIGN KEY constraint other than columns
380 my ($expected, $got) = @_;
381 my $desc = $expected->{display};
382 is( $got->name, $expected->{name},
383 "name parameter correct for `$desc'" );
384 is( $got->on_delete, $expected->{on_delete},
385 "on_delete parameter correct for `$desc'" );
386 is( $got->on_update, $expected->{on_update},
387 "on_update parameter correct for `$desc'" );
388 is( $got->deferrable, $expected->{deferrable},
389 "is_deferrable parameter correct for `$desc'" );
391 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
393 if ($expected->{noindex}) {
394 ok( !defined $index, "index doesn't for `$desc'" );
396 ok( defined $index, "index exists for `$desc'" );
397 is( $index->name, $expected->{index_name}, "index has correct name for `$desc'" );
402 my ($expected, $got) = @_;
403 my $desc = $expected->{display};
404 is( $got->name, $expected->{name},
405 "name parameter correct for `$desc'" );