8 use Scalar::Util 'blessed';
13 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
14 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
17 my $custom_deployment_statements_called = 0;
19 sub DBICTest::Schema::deployment_statements {
20 $custom_deployment_statements_called = 1;
22 return $self->next::method(@_);
26 # Check deployment statements ctx sensitivity
28 my $schema = DBICTest->init_schema (no_deploy => 1);
29 my $not_first_table_creation_re = qr/CREATE TABLE fourkeys_to_twokeys/;
31 my $statements = $schema->deployment_statements;
34 $not_first_table_creation_re,
35 'All create statements returned in 1 string in scalar ctx'
38 my @statements = $schema->deployment_statements;
39 cmp_ok (scalar @statements, '>', 1, 'Multiple statement lines in array ctx');
42 while ($i <= $#statements) {
43 last if $statements[$i] =~ $not_first_table_creation_re;
48 ($i > 0) && ($i <= $#statements),
49 "Creation statement was found somewherere within array ($i)"
54 # use our own throw-away schema, since we'll be deploying twice
55 my $schema = DBICTest->init_schema (no_deploy => 1);
57 my $deploy_hook_called = 0;
58 $custom_deployment_statements_called = 0;
60 # add a temporary sqlt_deploy_hook to a source
61 local $DBICTest::Schema::Track::hook_cb = sub {
62 my ($class, $sqlt_table) = @_;
64 $deploy_hook_called = 1;
66 is ($class, 'DBICTest::Track', 'Result class passed to plain hook');
69 $sqlt_table->schema->translator->producer_type,
70 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
71 'Production type passed to translator object',
75 my $component_deploy_hook_called = 0;
76 local $DBICTest::DeployComponent::hook_cb = sub {
77 $component_deploy_hook_called = 1;
80 $schema->deploy; # do not remove, this fires the is() test in the callback above
81 ok($deploy_hook_called, 'deploy hook got called');
82 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
83 ok($component_deploy_hook_called, 'component deploy hook got called');
86 my $schema = DBICTest->init_schema (no_deploy => 1);
89 my $deploy_hook_called = 0;
90 $custom_deployment_statements_called = 0;
91 my $sqlt_type = $schema->storage->sqlt_type;
93 # replace the sqlt calback with a custom version ading an index
94 $schema->source('Track')->sqlt_deploy_callback(sub {
95 my ($self, $sqlt_table) = @_;
97 $deploy_hook_called = 1;
100 $sqlt_table->schema->translator->producer_type,
101 join ('::', 'SQL::Translator::Producer', $sqlt_type),
102 'Production type passed to translator object',
105 if ($sqlt_type eq 'SQLite' ) {
106 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
107 or die $sqlt_table->error;
110 $self->default_sqlt_deploy_hook($sqlt_table);
113 $schema->deploy; # do not remove, this fires the is() test in the callback above
114 ok($deploy_hook_called, 'deploy hook got called');
115 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
119 my $translator = SQL::Translator->new(
121 'DBIx::Schema' => $schema,
128 local $SIG{__WARN__} = sub { $warn = shift };
130 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
131 local $relinfo->{attrs}{on_delete} = 'restrict';
134 $translator->parser('SQL::Translator::Parser::DBIx::Class');
135 $translator->producer('SQLite');
137 my $output = $translator->translate();
139 ok($output, "SQLT produced someoutput")
140 or diag($translator->error);
145 qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/,
146 'Warn about dubious on_delete/on_update attributes',
150 # Note that the constraints listed here are the only ones that are tested -- if
151 # more exist in the Schema than are listed here and all listed constraints are
152 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
153 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
154 # test coverage is not sufficient
156 my %fk_constraints = (
161 'display' => 'twokeys->cd',
162 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
163 'selftable' => 'twokeys', 'foreigntable' => 'cd',
164 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
166 on_delete => '', on_update => '', deferrable => 0,
169 'display' => 'twokeys->artist',
170 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
171 'selftable' => 'twokeys', 'foreigntable' => 'artist',
172 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
173 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
177 # FourKeys_to_TwoKeys
178 fourkeys_to_twokeys => [
180 'display' => 'fourkeys_to_twokeys->twokeys',
181 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
182 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
183 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
184 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
187 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
188 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
189 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
190 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
191 'foreigncols' => [qw(foo bar hello goodbye)],
192 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
199 'display' => 'cd_to_producer->cd',
200 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
201 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
202 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
203 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
206 'display' => 'cd_to_producer->producer',
207 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
208 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
209 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
210 on_delete => '', on_update => '', deferrable => 1,
217 'display' => 'self_ref_alias->self_ref for self_ref',
218 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
219 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
220 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
221 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
224 'display' => 'self_ref_alias->self_ref for alias',
225 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
226 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
227 'selfcols' => ['alias'], 'foreigncols' => ['id'],
228 on_delete => '', on_update => '', deferrable => 1,
235 'display' => 'cd->artist',
236 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
237 'selftable' => 'cd', 'foreigntable' => 'artist',
238 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
239 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
243 # Artist_undirected_map
244 artist_undirected_map => [
246 'display' => 'artist_undirected_map->artist for id1',
247 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
248 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
249 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
250 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
253 'display' => 'artist_undirected_map->artist for id2',
254 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
255 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
256 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
257 on_delete => '', on_update => '', deferrable => 1,
264 'display' => 'track->cd',
265 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
266 'selftable' => 'track', 'foreigntable' => 'cd',
267 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
268 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
275 'display' => 'treelike->treelike for parent',
276 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
277 'selftable' => 'treelike', 'foreigntable' => 'treelike',
278 'selfcols' => ['parent'], 'foreigncols' => ['id'],
279 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
286 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
287 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
288 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
289 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
290 on_delete => '', on_update => '', deferrable => 1,
297 'display' => 'tags->cd',
298 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
299 'selftable' => 'tags', 'foreigntable' => 'cd',
300 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
301 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
308 'display' => 'bookmark->link',
309 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
310 'selftable' => 'bookmark', 'foreigntable' => 'link',
311 'selfcols' => ['link'], 'foreigncols' => ['id'],
312 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
318 'display' => 'forceforeign->artist',
319 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
320 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
321 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
323 on_delete => '', on_update => '', deferrable => 1,
328 my %unique_constraints = (
332 'display' => 'cd artist and title unique',
333 'name' => 'cd_artist_title',
334 'table' => 'cd', 'cols' => ['artist', 'title'],
341 'display' => 'producer name unique',
342 'name' => 'prod_name', # explicit name
343 'table' => 'producer', 'cols' => ['name'],
350 'display' => 'twokeytreelike name unique',
351 'name' => 'tktlnameunique', # explicit name
352 'table' => 'twokeytreelike', 'cols' => ['name'],
357 # Constraint is commented out in DBICTest/Schema/Employee.pm
360 # 'display' => 'employee position and group_id unique',
361 # 'name' => 'position_group',
362 # 'table' => 'employee', cols => ['position', 'group_id'],
375 'fields' => ['title']
380 my $tschema = $translator->schema();
381 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
383 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
385 # Test that the Artist resultsource sqlt_deploy_hook was called okay and added
388 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1)
389 if $schema->storage->sqlt_type ne 'SQLite';
392 { $_->name eq 'artist_name_hookidx' }
393 $tschema->get_table('artist')->get_indices
394 ), 'sqlt_deploy_hook fired within a resultsource');
397 # Test that nonexistent constraints are not found
398 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
399 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
400 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
401 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
402 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
403 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
405 for my $expected_constraints (keys %fk_constraints) {
406 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
407 my $desc = $expected_constraint->{display};
408 my $constraint = get_constraint(
410 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
411 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
413 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
414 test_fk($expected_constraint, $constraint);
418 for my $expected_constraints (keys %unique_constraints) {
419 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
420 my $desc = $expected_constraint->{display};
421 my $constraint = get_constraint(
422 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
424 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
425 test_unique($expected_constraint, $constraint);
429 for my $table_index (keys %indexes) {
430 for my $expected_index ( @{ $indexes{$table_index} } ) {
431 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
435 # Returns the Constraint object for the specified constraint type, table and
436 # columns from the SQL::Translator schema, or undef if no matching constraint
439 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
440 # two parameters are not used.
442 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
443 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
446 my $table = $tschema->get_table($table_name);
448 my %fields = map { $_ => 1 } @$cols;
449 my %f_fields = map { $_ => 1 } @$f_cols;
451 die "No $table_name" unless $table;
453 for my $constraint ( $table->get_constraints ) {
454 next unless $constraint->type eq $type;
455 next unless $constraint->reference_table eq $f_table;
457 my %rev_fields = map { $_ => 1 } $constraint->fields;
458 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
460 # Check that the given fields are a subset of the constraint's fields
461 for my $field ($constraint->fields) {
462 next CONSTRAINT unless $fields{$field};
464 if ($type eq 'FOREIGN KEY') {
465 for my $f_field ($constraint->reference_fields) {
466 next CONSTRAINT unless $f_fields{$f_field};
470 # Check that the constraint's fields are a subset of the given fields
471 for my $field (@$cols) {
472 next CONSTRAINT unless $rev_fields{$field};
474 if ($type eq 'FOREIGN KEY') {
475 for my $f_field (@$f_cols) {
476 next CONSTRAINT unless $rev_f_fields{$f_field};
480 return $constraint; # everything passes, found the constraint
482 return undef; # didn't find a matching constraint
486 my ($table_name, $index) = @_;
488 my $table = $tschema->get_table($table_name);
491 for my $cand_index ( $table->get_indices ) {
493 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
494 || $index->{type} && $cand_index->type ne $index->{type};
496 my %idx_fields = map { $_ => 1 } $cand_index->fields;
498 for my $field ( @{ $index->{fields} } ) {
499 next CAND_INDEX unless $idx_fields{$field};
502 %idx_fields = map { $_ => 1 } @{$index->{fields}};
503 for my $field ( $cand_index->fields) {
504 next CAND_INDEX unless $idx_fields{$field};
510 return undef; # No matching idx
513 # Test parameters in a FOREIGN KEY constraint other than columns
515 my ($expected, $got) = @_;
516 my $desc = $expected->{display};
517 is( $got->name, $expected->{name},
518 "name parameter correct for '$desc'" );
519 is( $got->on_delete, $expected->{on_delete},
520 "on_delete parameter correct for '$desc'" );
521 is( $got->on_update, $expected->{on_update},
522 "on_update parameter correct for '$desc'" );
523 is( $got->deferrable, $expected->{deferrable},
524 "is_deferrable parameter correct for '$desc'" );
526 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
528 if ($expected->{noindex}) {
529 ok( !defined $index, "index doesn't for '$desc'" );
531 ok( defined $index, "index exists for '$desc'" );
532 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
537 my ($expected, $got) = @_;
538 my $desc = $expected->{display};
539 is( $got->name, $expected->{name},
540 "name parameter correct for '$desc'" );