9 use Scalar::Util 'blessed';
14 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
15 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
18 my $custom_deployment_statements_called = 0;
20 sub DBICTest::Schema::deployment_statements {
21 $custom_deployment_statements_called = 1;
23 return $self->next::method(@_);
27 # Check deployment statements ctx sensitivity
29 my $schema = DBICTest->init_schema (no_deploy => 1);
30 my $not_first_table_creation_re = qr/CREATE TABLE "fourkeys_to_twokeys"/;
32 my $statements = $schema->deployment_statements;
35 $not_first_table_creation_re,
36 'All create statements returned in 1 string in scalar ctx'
39 my @statements = $schema->deployment_statements;
40 cmp_ok (scalar @statements, '>', 1, 'Multiple statement lines in array ctx');
43 while ($i <= $#statements) {
44 last if $statements[$i] =~ $not_first_table_creation_re;
49 ($i > 0) && ($i <= $#statements),
50 "Creation statement was found somewherere within array ($i)"
55 # use our own throw-away schema, since we'll be deploying twice
56 my $schema = DBICTest->init_schema (no_deploy => 1);
58 my $deploy_hook_called = 0;
59 $custom_deployment_statements_called = 0;
61 # add a temporary sqlt_deploy_hook to a source
62 local $DBICTest::Schema::Track::hook_cb = sub {
63 my ($class, $sqlt_table) = @_;
65 $deploy_hook_called = 1;
67 is ($class, 'DBICTest::Track', 'Result class passed to plain hook');
70 $sqlt_table->schema->translator->producer_type,
71 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
72 'Production type passed to translator object',
76 my $component_deploy_hook_called = 0;
77 local $DBICTest::DeployComponent::hook_cb = sub {
78 $component_deploy_hook_called = 1;
81 $schema->deploy; # do not remove, this fires the is() test in the callback above
82 ok($deploy_hook_called, 'deploy hook got called');
83 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
84 ok($component_deploy_hook_called, 'component deploy hook got called');
87 my $schema = DBICTest->init_schema (no_deploy => 1);
90 my $deploy_hook_called = 0;
91 $custom_deployment_statements_called = 0;
92 my $sqlt_type = $schema->storage->sqlt_type;
94 # replace the sqlt calback with a custom version ading an index
95 $schema->source('Track')->sqlt_deploy_callback(sub {
96 my ($self, $sqlt_table) = @_;
98 $deploy_hook_called = 1;
101 $sqlt_table->schema->translator->producer_type,
102 join ('::', 'SQL::Translator::Producer', $sqlt_type),
103 'Production type passed to translator object',
106 if ($sqlt_type eq 'SQLite' ) {
107 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
108 or die $sqlt_table->error;
111 $self->default_sqlt_deploy_hook($sqlt_table);
114 $schema->deploy; # do not remove, this fires the is() test in the callback above
115 ok($deploy_hook_called, 'deploy hook got called');
116 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
120 my $translator = SQL::Translator->new(
122 dbic_schema => $schema,
128 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
129 local $relinfo->{attrs}{on_delete} = 'restrict';
131 $translator->parser('SQL::Translator::Parser::DBIx::Class');
132 $translator->producer('SQLite');
134 my $output = $translator->translate();
136 ok($output, "SQLT produced someoutput")
137 or diag($translator->error);
140 (qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/) x 2
141 ], 'Warn about dubious on_delete/on_update attributes';
143 # Note that the constraints listed here are the only ones that are tested -- if
144 # more exist in the Schema than are listed here and all listed constraints are
145 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
146 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
147 # test coverage is not sufficient
149 my %fk_constraints = (
154 'display' => 'twokeys->cd',
155 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
156 'selftable' => 'twokeys', 'foreigntable' => 'cd',
157 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
159 on_delete => '', on_update => '', deferrable => 0,
162 'display' => 'twokeys->artist',
163 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
164 'selftable' => 'twokeys', 'foreigntable' => 'artist',
165 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
166 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
170 # FourKeys_to_TwoKeys
171 fourkeys_to_twokeys => [
173 'display' => 'fourkeys_to_twokeys->twokeys',
174 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
175 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
176 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
177 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
180 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
181 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
182 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
183 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
184 'foreigncols' => [qw(foo bar hello goodbye)],
185 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
192 'display' => 'cd_to_producer->cd',
193 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
194 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
195 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
196 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
199 'display' => 'cd_to_producer->producer',
200 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
201 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
202 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
203 on_delete => '', on_update => '', deferrable => 1,
210 'display' => 'self_ref_alias->self_ref for self_ref',
211 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
212 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
213 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
214 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
217 'display' => 'self_ref_alias->self_ref for alias',
218 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
219 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
220 'selfcols' => ['alias'], 'foreigncols' => ['id'],
221 on_delete => '', on_update => '', deferrable => 1,
228 'display' => 'cd->artist',
229 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
230 'selftable' => 'cd', 'foreigntable' => 'artist',
231 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
232 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
236 # Artist_undirected_map
237 artist_undirected_map => [
239 'display' => 'artist_undirected_map->artist for id1',
240 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
241 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
242 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
243 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
246 'display' => 'artist_undirected_map->artist for id2',
247 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
248 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
249 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
250 on_delete => '', on_update => '', deferrable => 1,
257 'display' => 'track->cd',
258 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
259 'selftable' => 'track', 'foreigntable' => 'cd',
260 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
261 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
268 'display' => 'treelike->treelike for parent',
269 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
270 'selftable' => 'treelike', 'foreigntable' => 'treelike',
271 'selfcols' => ['parent'], 'foreigncols' => ['id'],
272 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
279 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
280 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
281 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
282 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
283 on_delete => '', on_update => '', deferrable => 1,
290 'display' => 'tags->cd',
291 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
292 'selftable' => 'tags', 'foreigntable' => 'cd',
293 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
294 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
301 'display' => 'bookmark->link',
302 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
303 'selftable' => 'bookmark', 'foreigntable' => 'link',
304 'selfcols' => ['link'], 'foreigncols' => ['id'],
305 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
311 'display' => 'forceforeign->artist',
312 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
313 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
314 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
316 on_delete => '', on_update => '', deferrable => 1,
321 my %unique_constraints = (
325 'display' => 'cd artist and title unique',
326 'name' => 'cd_artist_title',
327 'table' => 'cd', 'cols' => ['artist', 'title'],
334 'display' => 'producer name unique',
335 'name' => 'prod_name', # explicit name
336 'table' => 'producer', 'cols' => ['name'],
343 'display' => 'twokeytreelike name unique',
344 'name' => 'tktlnameunique', # explicit name
345 'table' => 'twokeytreelike', 'cols' => ['name'],
350 # Constraint is commented out in DBICTest/Schema/Employee.pm
353 # 'display' => 'employee position and group_id unique',
354 # 'name' => 'position_group',
355 # 'table' => 'employee', cols => ['position', 'group_id'],
368 'fields' => ['title']
373 my $tschema = $translator->schema();
374 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
376 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
378 # Test that the Artist resultsource sqlt_deploy_hook was called okay and added
381 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1)
382 if $schema->storage->sqlt_type ne 'SQLite';
385 { $_->name eq 'artist_name_hookidx' }
386 $tschema->get_table('artist')->get_indices
387 ), 'sqlt_deploy_hook fired within a resultsource');
390 # Test that nonexistent constraints are not found
391 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
392 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
393 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
394 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
395 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
396 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
398 for my $expected_constraints (keys %fk_constraints) {
399 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
400 my $desc = $expected_constraint->{display};
401 my $constraint = get_constraint(
403 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
404 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
406 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
407 test_fk($expected_constraint, $constraint);
411 for my $expected_constraints (keys %unique_constraints) {
412 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
413 my $desc = $expected_constraint->{display};
414 my $constraint = get_constraint(
415 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
417 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
418 test_unique($expected_constraint, $constraint);
422 for my $table_index (keys %indexes) {
423 for my $expected_index ( @{ $indexes{$table_index} } ) {
424 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
428 # Returns the Constraint object for the specified constraint type, table and
429 # columns from the SQL::Translator schema, or undef if no matching constraint
432 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
433 # two parameters are not used.
435 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
436 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
439 my $table = $tschema->get_table($table_name);
441 my %fields = map { $_ => 1 } @$cols;
442 my %f_fields = map { $_ => 1 } @$f_cols;
444 die "No $table_name" unless $table;
446 for my $constraint ( $table->get_constraints ) {
447 next unless $constraint->type eq $type;
448 next unless $constraint->reference_table eq $f_table;
450 my %rev_fields = map { $_ => 1 } $constraint->fields;
451 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
453 # Check that the given fields are a subset of the constraint's fields
454 for my $field ($constraint->fields) {
455 next CONSTRAINT unless $fields{$field};
457 if ($type eq 'FOREIGN KEY') {
458 for my $f_field ($constraint->reference_fields) {
459 next CONSTRAINT unless $f_fields{$f_field};
463 # Check that the constraint's fields are a subset of the given fields
464 for my $field (@$cols) {
465 next CONSTRAINT unless $rev_fields{$field};
467 if ($type eq 'FOREIGN KEY') {
468 for my $f_field (@$f_cols) {
469 next CONSTRAINT unless $rev_f_fields{$f_field};
473 return $constraint; # everything passes, found the constraint
475 return undef; # didn't find a matching constraint
479 my ($table_name, $index) = @_;
481 my $table = $tschema->get_table($table_name);
484 for my $cand_index ( $table->get_indices ) {
486 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
487 || $index->{type} && $cand_index->type ne $index->{type};
489 my %idx_fields = map { $_ => 1 } $cand_index->fields;
491 for my $field ( @{ $index->{fields} } ) {
492 next CAND_INDEX unless $idx_fields{$field};
495 %idx_fields = map { $_ => 1 } @{$index->{fields}};
496 for my $field ( $cand_index->fields) {
497 next CAND_INDEX unless $idx_fields{$field};
503 return undef; # No matching idx
506 # Test parameters in a FOREIGN KEY constraint other than columns
508 my ($expected, $got) = @_;
509 my $desc = $expected->{display};
510 is( $got->name, $expected->{name},
511 "name parameter correct for '$desc'" );
512 is( $got->on_delete, $expected->{on_delete},
513 "on_delete parameter correct for '$desc'" );
514 is( $got->on_update, $expected->{on_update},
515 "on_update parameter correct for '$desc'" );
516 is( $got->deferrable, $expected->{deferrable},
517 "is_deferrable parameter correct for '$desc'" );
519 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
521 if ($expected->{noindex}) {
522 ok( !defined $index, "index doesn't for '$desc'" );
524 ok( defined $index, "index exists for '$desc'" );
525 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
530 my ($expected, $got) = @_;
531 my $desc = $expected->{display};
532 is( $got->name, $expected->{name},
533 "name parameter correct for '$desc'" );