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,
24 local $SIG{__WARN__} = sub { $warn = shift };
26 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
27 local $relinfo->{attrs}{on_delete} = 'restrict';
29 $schema->source('Track')->sqlt_deploy_callback(sub {
30 my ($self, $sqlt_table) = @_;
32 if ($sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) {
33 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
34 or die $sqlt_table->error;
37 $self->default_sqlt_deploy_hook($sqlt_table);
40 $translator->parser('SQL::Translator::Parser::DBIx::Class');
41 $translator->producer('SQLite');
43 my $output = $translator->translate();
45 ok($output, "SQLT produced someoutput")
46 or diag($translator->error);
48 like ($warn, qr/^SQLT attribute .+? was supplied for relationship/, 'Warn about dubious on_delete/on_update attributes');
51 # Note that the constraints listed here are the only ones that are tested -- if
52 # more exist in the Schema than are listed here and all listed constraints are
53 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
54 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
55 # test coverage is not sufficient
57 my %fk_constraints = (
62 'display' => 'twokeys->cd',
63 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
64 'selftable' => 'twokeys', 'foreigntable' => 'cd',
65 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
67 on_delete => '', on_update => '', deferrable => 0,
70 'display' => 'twokeys->artist',
71 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
72 'selftable' => 'twokeys', 'foreigntable' => 'artist',
73 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
74 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
79 fourkeys_to_twokeys => [
81 'display' => 'fourkeys_to_twokeys->twokeys',
82 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
83 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
84 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
85 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
88 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
89 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
90 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
91 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
92 'foreigncols' => [qw(foo bar hello goodbye)],
93 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
100 'display' => 'cd_to_producer->cd',
101 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
102 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
103 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
104 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
107 'display' => 'cd_to_producer->producer',
108 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
109 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
110 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
111 on_delete => '', on_update => '', deferrable => 1,
118 'display' => 'self_ref_alias->self_ref for self_ref',
119 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
120 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
121 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
122 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
125 'display' => 'self_ref_alias->self_ref for alias',
126 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
127 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
128 'selfcols' => ['alias'], 'foreigncols' => ['id'],
129 on_delete => '', on_update => '', deferrable => 1,
136 'display' => 'cd->artist',
137 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
138 'selftable' => 'cd', 'foreigntable' => 'artist',
139 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
140 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
144 # Artist_undirected_map
145 artist_undirected_map => [
147 'display' => 'artist_undirected_map->artist for id1',
148 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
149 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
150 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
151 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
154 'display' => 'artist_undirected_map->artist for id2',
155 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
156 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
157 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
158 on_delete => '', on_update => 'CASCADE', deferrable => 1,
165 'display' => 'track->cd',
166 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
167 'selftable' => 'track', 'foreigntable' => 'cd',
168 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
169 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
176 'display' => 'treelike->treelike for parent',
177 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
178 'selftable' => 'treelike', 'foreigntable' => 'treelike',
179 'selfcols' => ['parent'], 'foreigncols' => ['id'],
180 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
187 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
188 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
189 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
190 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
191 on_delete => '', on_update => '', deferrable => 1,
198 'display' => 'tags->cd',
199 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
200 'selftable' => 'tags', 'foreigntable' => 'cd',
201 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
202 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
209 'display' => 'bookmark->link',
210 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
211 'selftable' => 'bookmark', 'foreigntable' => 'link',
212 'selfcols' => ['link'], 'foreigncols' => ['id'],
213 on_delete => '', on_update => '', deferrable => 1,
219 'display' => 'forceforeign->artist',
220 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
221 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
222 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
223 on_delete => '', on_update => '', deferrable => 1,
228 my %unique_constraints = (
232 'display' => 'cd artist and title unique',
233 'name' => 'cd_artist_title',
234 'table' => 'cd', 'cols' => ['artist', 'title'],
241 'display' => 'producer name unique',
242 'name' => 'prod_name', # explicit name
243 'table' => 'producer', 'cols' => ['name'],
250 'display' => 'twokeytreelike name unique',
251 'name' => 'tktlnameunique', # explicit name
252 'table' => 'twokeytreelike', 'cols' => ['name'],
257 # Constraint is commented out in DBICTest/Schema/Employee.pm
260 # 'display' => 'employee position and group_id unique',
261 # 'name' => 'position_group',
262 # 'table' => 'employee', cols => ['position', 'group_id'],
275 'fields' => ['title']
280 my $tschema = $translator->schema();
281 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
283 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
285 # Test that nonexistent constraints are not found
286 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
287 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
288 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
289 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
290 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
291 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
293 for my $expected_constraints (keys %fk_constraints) {
294 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
295 my $desc = $expected_constraint->{display};
296 my $constraint = get_constraint(
298 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
299 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
301 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
302 test_fk($expected_constraint, $constraint);
306 for my $expected_constraints (keys %unique_constraints) {
307 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
308 my $desc = $expected_constraint->{display};
309 my $constraint = get_constraint(
310 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
312 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
313 test_unique($expected_constraint, $constraint);
317 for my $table_index (keys %indexes) {
318 for my $expected_index ( @{ $indexes{$table_index} } ) {
319 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
323 # Returns the Constraint object for the specified constraint type, table and
324 # columns from the SQL::Translator schema, or undef if no matching constraint
327 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
328 # two parameters are not used.
330 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
331 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
334 my $table = $tschema->get_table($table_name);
336 my %fields = map { $_ => 1 } @$cols;
337 my %f_fields = map { $_ => 1 } @$f_cols;
339 die "No $table_name" unless $table;
341 for my $constraint ( $table->get_constraints ) {
342 next unless $constraint->type eq $type;
343 next unless $constraint->reference_table eq $f_table;
345 my %rev_fields = map { $_ => 1 } $constraint->fields;
346 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
348 # Check that the given fields are a subset of the constraint's fields
349 for my $field ($constraint->fields) {
350 next CONSTRAINT unless $fields{$field};
352 if ($type eq 'FOREIGN KEY') {
353 for my $f_field ($constraint->reference_fields) {
354 next CONSTRAINT unless $f_fields{$f_field};
358 # Check that the constraint's fields are a subset of the given fields
359 for my $field (@$cols) {
360 next CONSTRAINT unless $rev_fields{$field};
362 if ($type eq 'FOREIGN KEY') {
363 for my $f_field (@$f_cols) {
364 next CONSTRAINT unless $rev_f_fields{$f_field};
368 return $constraint; # everything passes, found the constraint
370 return undef; # didn't find a matching constraint
374 my ($table_name, $index) = @_;
376 my $table = $tschema->get_table($table_name);
379 for my $cand_index ( $table->get_indices ) {
381 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
382 || $index->{type} && $cand_index->type ne $index->{type};
384 my %idx_fields = map { $_ => 1 } $cand_index->fields;
386 for my $field ( @{ $index->{fields} } ) {
387 next CAND_INDEX unless $idx_fields{$field};
390 %idx_fields = map { $_ => 1 } @{$index->{fields}};
391 for my $field ( $cand_index->fields) {
392 next CAND_INDEX unless $idx_fields{$field};
398 return undef; # No matching idx
401 # Test parameters in a FOREIGN KEY constraint other than columns
403 my ($expected, $got) = @_;
404 my $desc = $expected->{display};
405 is( $got->name, $expected->{name},
406 "name parameter correct for `$desc'" );
407 is( $got->on_delete, $expected->{on_delete},
408 "on_delete parameter correct for `$desc'" );
409 is( $got->on_update, $expected->{on_update},
410 "on_update parameter correct for `$desc'" );
411 is( $got->deferrable, $expected->{deferrable},
412 "is_deferrable parameter correct for `$desc'" );
414 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
416 if ($expected->{noindex}) {
417 ok( !defined $index, "index doesn't for `$desc'" );
419 ok( defined $index, "index exists for `$desc'" );
420 is( $index->name, $expected->{index_name}, "index has correct name for `$desc'" );
425 my ($expected, $got) = @_;
426 my $desc = $expected->{display};
427 is( $got->name, $expected->{name},
428 "name parameter correct for `$desc'" );