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(@_);
25 my $schema = DBICTest->init_schema (no_deploy => 1);
28 # Check deployment statements ctx sensitivity
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');
88 my $deploy_hook_called = 0;
89 $custom_deployment_statements_called = 0;
91 # replace the sqlt calback with a custom version ading an index
92 $schema->source('Track')->sqlt_deploy_callback(sub {
93 my ($self, $sqlt_table) = @_;
95 $deploy_hook_called = 1;
98 $sqlt_table->schema->translator->producer_type,
99 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
100 'Production type passed to translator object',
103 if ($schema->storage->sqlt_type eq 'SQLite' ) {
104 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
105 or die $sqlt_table->error;
108 $self->default_sqlt_deploy_hook($sqlt_table);
111 $schema->deploy; # do not remove, this fires the is() test in the callback above
112 ok($deploy_hook_called, 'deploy hook got called');
113 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
117 my $translator = SQL::Translator->new(
119 'DBIx::Schema' => $schema,
126 local $SIG{__WARN__} = sub { $warn = shift };
128 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
129 local $relinfo->{attrs}{on_delete} = 'restrict';
132 $translator->parser('SQL::Translator::Parser::DBIx::Class');
133 $translator->producer('SQLite');
135 my $output = $translator->translate();
137 ok($output, "SQLT produced someoutput")
138 or diag($translator->error);
143 qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/,
144 'Warn about dubious on_delete/on_update attributes',
148 # Note that the constraints listed here are the only ones that are tested -- if
149 # more exist in the Schema than are listed here and all listed constraints are
150 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
151 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
152 # test coverage is not sufficient
154 my %fk_constraints = (
159 'display' => 'twokeys->cd',
160 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
161 'selftable' => 'twokeys', 'foreigntable' => 'cd',
162 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
164 on_delete => '', on_update => '', deferrable => 0,
167 'display' => 'twokeys->artist',
168 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
169 'selftable' => 'twokeys', 'foreigntable' => 'artist',
170 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
171 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
175 # FourKeys_to_TwoKeys
176 fourkeys_to_twokeys => [
178 'display' => 'fourkeys_to_twokeys->twokeys',
179 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
180 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
181 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
182 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
185 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
186 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
187 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
188 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
189 'foreigncols' => [qw(foo bar hello goodbye)],
190 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
197 'display' => 'cd_to_producer->cd',
198 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
199 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
200 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
201 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
204 'display' => 'cd_to_producer->producer',
205 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
206 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
207 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
208 on_delete => '', on_update => '', deferrable => 1,
215 'display' => 'self_ref_alias->self_ref for self_ref',
216 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
217 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
218 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
219 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
222 'display' => 'self_ref_alias->self_ref for alias',
223 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
224 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
225 'selfcols' => ['alias'], 'foreigncols' => ['id'],
226 on_delete => '', on_update => '', deferrable => 1,
233 'display' => 'cd->artist',
234 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
235 'selftable' => 'cd', 'foreigntable' => 'artist',
236 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
237 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
241 # Artist_undirected_map
242 artist_undirected_map => [
244 'display' => 'artist_undirected_map->artist for id1',
245 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
246 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
247 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
248 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
251 'display' => 'artist_undirected_map->artist for id2',
252 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
253 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
254 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
255 on_delete => '', on_update => '', deferrable => 1,
262 'display' => 'track->cd',
263 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
264 'selftable' => 'track', 'foreigntable' => 'cd',
265 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
266 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
273 'display' => 'treelike->treelike for parent',
274 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
275 'selftable' => 'treelike', 'foreigntable' => 'treelike',
276 'selfcols' => ['parent'], 'foreigncols' => ['id'],
277 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
284 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
285 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
286 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
287 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
288 on_delete => '', on_update => '', deferrable => 1,
295 'display' => 'tags->cd',
296 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
297 'selftable' => 'tags', 'foreigntable' => 'cd',
298 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
299 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
306 'display' => 'bookmark->link',
307 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
308 'selftable' => 'bookmark', 'foreigntable' => 'link',
309 'selfcols' => ['link'], 'foreigncols' => ['id'],
310 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
316 'display' => 'forceforeign->artist',
317 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
318 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
319 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
321 on_delete => '', on_update => '', deferrable => 1,
326 my %unique_constraints = (
330 'display' => 'cd artist and title unique',
331 'name' => 'cd_artist_title',
332 'table' => 'cd', 'cols' => ['artist', 'title'],
339 'display' => 'producer name unique',
340 'name' => 'prod_name', # explicit name
341 'table' => 'producer', 'cols' => ['name'],
348 'display' => 'twokeytreelike name unique',
349 'name' => 'tktlnameunique', # explicit name
350 'table' => 'twokeytreelike', 'cols' => ['name'],
355 # Constraint is commented out in DBICTest/Schema/Employee.pm
358 # 'display' => 'employee position and group_id unique',
359 # 'name' => 'position_group',
360 # 'table' => 'employee', cols => ['position', 'group_id'],
373 'fields' => ['title']
378 my $tschema = $translator->schema();
379 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
381 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
383 # Test that the Artist resultsource sqlt_deploy_hook was called okay and added
386 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1)
387 if $schema->storage->sqlt_type ne 'SQLite';
390 { $_->name eq 'artist_name_hookidx' }
391 $tschema->get_table('artist')->get_indices
392 ), 'sqlt_deploy_hook fired within a resultsource');
395 # Test that nonexistent constraints are not found
396 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
397 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
398 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
399 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
400 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
401 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
403 for my $expected_constraints (keys %fk_constraints) {
404 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
405 my $desc = $expected_constraint->{display};
406 my $constraint = get_constraint(
408 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
409 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
411 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
412 test_fk($expected_constraint, $constraint);
416 for my $expected_constraints (keys %unique_constraints) {
417 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
418 my $desc = $expected_constraint->{display};
419 my $constraint = get_constraint(
420 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
422 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
423 test_unique($expected_constraint, $constraint);
427 for my $table_index (keys %indexes) {
428 for my $expected_index ( @{ $indexes{$table_index} } ) {
429 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
433 # Returns the Constraint object for the specified constraint type, table and
434 # columns from the SQL::Translator schema, or undef if no matching constraint
437 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
438 # two parameters are not used.
440 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
441 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
444 my $table = $tschema->get_table($table_name);
446 my %fields = map { $_ => 1 } @$cols;
447 my %f_fields = map { $_ => 1 } @$f_cols;
449 die "No $table_name" unless $table;
451 for my $constraint ( $table->get_constraints ) {
452 next unless $constraint->type eq $type;
453 next unless $constraint->reference_table eq $f_table;
455 my %rev_fields = map { $_ => 1 } $constraint->fields;
456 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
458 # Check that the given fields are a subset of the constraint's fields
459 for my $field ($constraint->fields) {
460 next CONSTRAINT unless $fields{$field};
462 if ($type eq 'FOREIGN KEY') {
463 for my $f_field ($constraint->reference_fields) {
464 next CONSTRAINT unless $f_fields{$f_field};
468 # Check that the constraint's fields are a subset of the given fields
469 for my $field (@$cols) {
470 next CONSTRAINT unless $rev_fields{$field};
472 if ($type eq 'FOREIGN KEY') {
473 for my $f_field (@$f_cols) {
474 next CONSTRAINT unless $rev_f_fields{$f_field};
478 return $constraint; # everything passes, found the constraint
480 return undef; # didn't find a matching constraint
484 my ($table_name, $index) = @_;
486 my $table = $tschema->get_table($table_name);
489 for my $cand_index ( $table->get_indices ) {
491 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
492 || $index->{type} && $cand_index->type ne $index->{type};
494 my %idx_fields = map { $_ => 1 } $cand_index->fields;
496 for my $field ( @{ $index->{fields} } ) {
497 next CAND_INDEX unless $idx_fields{$field};
500 %idx_fields = map { $_ => 1 } @{$index->{fields}};
501 for my $field ( $cand_index->fields) {
502 next CAND_INDEX unless $idx_fields{$field};
508 return undef; # No matching idx
511 # Test parameters in a FOREIGN KEY constraint other than columns
513 my ($expected, $got) = @_;
514 my $desc = $expected->{display};
515 is( $got->name, $expected->{name},
516 "name parameter correct for '$desc'" );
517 is( $got->on_delete, $expected->{on_delete},
518 "on_delete parameter correct for '$desc'" );
519 is( $got->on_update, $expected->{on_update},
520 "on_update parameter correct for '$desc'" );
521 is( $got->deferrable, $expected->{deferrable},
522 "is_deferrable parameter correct for '$desc'" );
524 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
526 if ($expected->{noindex}) {
527 ok( !defined $index, "index doesn't for '$desc'" );
529 ok( defined $index, "index exists for '$desc'" );
530 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
535 my ($expected, $got) = @_;
536 my $desc = $expected->{display};
537 is( $got->name, $expected->{name},
538 "name parameter correct for '$desc'" );