9 require DBIx::Class::Storage::DBI;
11 'Test needs SQL::Translator ' . DBIx::Class::Storage::DBI->_sqlt_minimum_version
12 if not DBIx::Class::Storage::DBI->_sqlt_version_ok;
15 my $schema = DBICTest->init_schema (no_deploy => 1);
18 # Check deployment statements ctx sensitivity
20 my $not_first_table_creation_re = qr/CREATE TABLE fourkeys_to_twokeys/;
23 my $statements = $schema->deployment_statements;
26 $not_first_table_creation_re,
27 'All create statements returned in 1 string in scalar ctx'
30 my @statements = $schema->deployment_statements;
31 cmp_ok (scalar @statements, '>', 1, 'Multiple statement lines in array ctx');
34 while ($i <= $#statements) {
35 last if $statements[$i] =~ $not_first_table_creation_re;
40 ($i > 0) && ($i <= $#statements),
41 "Creation statement was found somewherere within array ($i)"
47 # replace the sqlt calback with a custom version ading an index
48 $schema->source('Track')->sqlt_deploy_callback(sub {
49 my ($self, $sqlt_table) = @_;
52 $sqlt_table->schema->translator->producer_type,
53 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
54 'Production type passed to translator object',
57 if ($schema->storage->sqlt_type eq 'SQLite' ) {
58 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
59 or die $sqlt_table->error;
62 $self->default_sqlt_deploy_hook($sqlt_table);
65 $schema->deploy; # do not remove, this fires the is() test in the callback above
69 my $translator = SQL::Translator->new(
71 'DBIx::Schema' => $schema,
78 local $SIG{__WARN__} = sub { $warn = shift };
80 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
81 local $relinfo->{attrs}{on_delete} = 'restrict';
84 $translator->parser('SQL::Translator::Parser::DBIx::Class');
85 $translator->producer('SQLite');
87 my $output = $translator->translate();
89 ok($output, "SQLT produced someoutput")
90 or diag($translator->error);
95 qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/,
96 'Warn about dubious on_delete/on_update attributes',
100 # Note that the constraints listed here are the only ones that are tested -- if
101 # more exist in the Schema than are listed here and all listed constraints are
102 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
103 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
104 # test coverage is not sufficient
106 my %fk_constraints = (
111 'display' => 'twokeys->cd',
112 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
113 'selftable' => 'twokeys', 'foreigntable' => 'cd',
114 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
116 on_delete => '', on_update => '', deferrable => 0,
119 'display' => 'twokeys->artist',
120 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
121 'selftable' => 'twokeys', 'foreigntable' => 'artist',
122 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
123 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
127 # FourKeys_to_TwoKeys
128 fourkeys_to_twokeys => [
130 'display' => 'fourkeys_to_twokeys->twokeys',
131 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
132 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
133 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
134 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
137 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
138 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
139 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
140 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
141 'foreigncols' => [qw(foo bar hello goodbye)],
142 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
149 'display' => 'cd_to_producer->cd',
150 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
151 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
152 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
153 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
156 'display' => 'cd_to_producer->producer',
157 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
158 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
159 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
160 on_delete => '', on_update => '', deferrable => 1,
167 'display' => 'self_ref_alias->self_ref for self_ref',
168 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
169 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
170 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
171 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
174 'display' => 'self_ref_alias->self_ref for alias',
175 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
176 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
177 'selfcols' => ['alias'], 'foreigncols' => ['id'],
178 on_delete => '', on_update => '', deferrable => 1,
185 'display' => 'cd->artist',
186 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
187 'selftable' => 'cd', 'foreigntable' => 'artist',
188 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
189 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
193 # Artist_undirected_map
194 artist_undirected_map => [
196 'display' => 'artist_undirected_map->artist for id1',
197 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
198 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
199 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
200 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
203 'display' => 'artist_undirected_map->artist for id2',
204 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
205 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
206 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
207 on_delete => '', on_update => '', deferrable => 1,
214 'display' => 'track->cd',
215 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
216 'selftable' => 'track', 'foreigntable' => 'cd',
217 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
218 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
225 'display' => 'treelike->treelike for parent',
226 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
227 'selftable' => 'treelike', 'foreigntable' => 'treelike',
228 'selfcols' => ['parent'], 'foreigncols' => ['id'],
229 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
236 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
237 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
238 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
239 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
240 on_delete => '', on_update => '', deferrable => 1,
247 'display' => 'tags->cd',
248 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
249 'selftable' => 'tags', 'foreigntable' => 'cd',
250 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
251 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
258 'display' => 'bookmark->link',
259 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
260 'selftable' => 'bookmark', 'foreigntable' => 'link',
261 'selfcols' => ['link'], 'foreigncols' => ['id'],
262 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
268 'display' => 'forceforeign->artist',
269 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
270 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
271 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
273 on_delete => '', on_update => '', deferrable => 1,
278 my %unique_constraints = (
282 'display' => 'cd artist and title unique',
283 'name' => 'cd_artist_title',
284 'table' => 'cd', 'cols' => ['artist', 'title'],
291 'display' => 'producer name unique',
292 'name' => 'prod_name', # explicit name
293 'table' => 'producer', 'cols' => ['name'],
300 'display' => 'twokeytreelike name unique',
301 'name' => 'tktlnameunique', # explicit name
302 'table' => 'twokeytreelike', 'cols' => ['name'],
307 # Constraint is commented out in DBICTest/Schema/Employee.pm
310 # 'display' => 'employee position and group_id unique',
311 # 'name' => 'position_group',
312 # 'table' => 'employee', cols => ['position', 'group_id'],
325 'fields' => ['title']
330 my $tschema = $translator->schema();
331 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
333 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
335 # Test that the Artist resultsource sqlt_deploy_hook was called okay and added
338 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1)
339 if $schema->storage->sqlt_type ne 'SQLite';
342 { $_->name eq 'artist_name_hookidx' }
343 $tschema->get_table('artist')->get_indices
344 ), 'sqlt_deploy_hook fired within a resultsource');
347 # Test that nonexistent constraints are not found
348 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
349 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
350 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
351 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
352 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
353 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
355 for my $expected_constraints (keys %fk_constraints) {
356 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
357 my $desc = $expected_constraint->{display};
358 my $constraint = get_constraint(
360 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
361 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
363 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
364 test_fk($expected_constraint, $constraint);
368 for my $expected_constraints (keys %unique_constraints) {
369 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
370 my $desc = $expected_constraint->{display};
371 my $constraint = get_constraint(
372 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
374 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
375 test_unique($expected_constraint, $constraint);
379 for my $table_index (keys %indexes) {
380 for my $expected_index ( @{ $indexes{$table_index} } ) {
381 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
385 # Returns the Constraint object for the specified constraint type, table and
386 # columns from the SQL::Translator schema, or undef if no matching constraint
389 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
390 # two parameters are not used.
392 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
393 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
396 my $table = $tschema->get_table($table_name);
398 my %fields = map { $_ => 1 } @$cols;
399 my %f_fields = map { $_ => 1 } @$f_cols;
401 die "No $table_name" unless $table;
403 for my $constraint ( $table->get_constraints ) {
404 next unless $constraint->type eq $type;
405 next unless $constraint->reference_table eq $f_table;
407 my %rev_fields = map { $_ => 1 } $constraint->fields;
408 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
410 # Check that the given fields are a subset of the constraint's fields
411 for my $field ($constraint->fields) {
412 next CONSTRAINT unless $fields{$field};
414 if ($type eq 'FOREIGN KEY') {
415 for my $f_field ($constraint->reference_fields) {
416 next CONSTRAINT unless $f_fields{$f_field};
420 # Check that the constraint's fields are a subset of the given fields
421 for my $field (@$cols) {
422 next CONSTRAINT unless $rev_fields{$field};
424 if ($type eq 'FOREIGN KEY') {
425 for my $f_field (@$f_cols) {
426 next CONSTRAINT unless $rev_f_fields{$f_field};
430 return $constraint; # everything passes, found the constraint
432 return undef; # didn't find a matching constraint
436 my ($table_name, $index) = @_;
438 my $table = $tschema->get_table($table_name);
441 for my $cand_index ( $table->get_indices ) {
443 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
444 || $index->{type} && $cand_index->type ne $index->{type};
446 my %idx_fields = map { $_ => 1 } $cand_index->fields;
448 for my $field ( @{ $index->{fields} } ) {
449 next CAND_INDEX unless $idx_fields{$field};
452 %idx_fields = map { $_ => 1 } @{$index->{fields}};
453 for my $field ( $cand_index->fields) {
454 next CAND_INDEX unless $idx_fields{$field};
460 return undef; # No matching idx
463 # Test parameters in a FOREIGN KEY constraint other than columns
465 my ($expected, $got) = @_;
466 my $desc = $expected->{display};
467 is( $got->name, $expected->{name},
468 "name parameter correct for '$desc'" );
469 is( $got->on_delete, $expected->{on_delete},
470 "on_delete parameter correct for '$desc'" );
471 is( $got->on_update, $expected->{on_update},
472 "on_update parameter correct for '$desc'" );
473 is( $got->deferrable, $expected->{deferrable},
474 "is_deferrable parameter correct for '$desc'" );
476 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
478 if ($expected->{noindex}) {
479 ok( !defined $index, "index doesn't for '$desc'" );
481 ok( defined $index, "index exists for '$desc'" );
482 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
487 my ($expected, $got) = @_;
488 my $desc = $expected->{display};
489 is( $got->name, $expected->{name},
490 "name parameter correct for '$desc'" );