11 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
15 my $custom_deployment_statements_called = 0;
17 sub DBICTest::Schema::deployment_statements {
18 $custom_deployment_statements_called = 1;
20 return $self->next::method(@_);
23 my $schema = DBICTest->init_schema (no_deploy => 1);
26 # Check deployment statements ctx sensitivity
28 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)"
56 my $deploy_hook_called = 0;
58 # replace the sqlt calback with a custom version ading an index
59 $schema->source('Track')->sqlt_deploy_callback(sub {
60 my ($self, $sqlt_table) = @_;
62 $deploy_hook_called = 1;
65 $sqlt_table->schema->translator->producer_type,
66 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
67 'Production type passed to translator object',
70 if ($schema->storage->sqlt_type eq 'SQLite' ) {
71 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
72 or die $sqlt_table->error;
75 $self->default_sqlt_deploy_hook($sqlt_table);
78 $schema->deploy; # do not remove, this fires the is() test in the callback above
79 ok($deploy_hook_called, 'deploy hook got called');
80 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
84 my $translator = SQL::Translator->new(
86 'DBIx::Schema' => $schema,
93 local $SIG{__WARN__} = sub { $warn = shift };
95 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
96 local $relinfo->{attrs}{on_delete} = 'restrict';
99 $translator->parser('SQL::Translator::Parser::DBIx::Class');
100 $translator->producer('SQLite');
102 my $output = $translator->translate();
104 ok($output, "SQLT produced someoutput")
105 or diag($translator->error);
110 qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/,
111 'Warn about dubious on_delete/on_update attributes',
115 # Note that the constraints listed here are the only ones that are tested -- if
116 # more exist in the Schema than are listed here and all listed constraints are
117 # correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
118 # KEY constraints to DBICTest::Schema, add tests here if you think the existing
119 # test coverage is not sufficient
121 my %fk_constraints = (
126 'display' => 'twokeys->cd',
127 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
128 'selftable' => 'twokeys', 'foreigntable' => 'cd',
129 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
131 on_delete => '', on_update => '', deferrable => 0,
134 'display' => 'twokeys->artist',
135 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
136 'selftable' => 'twokeys', 'foreigntable' => 'artist',
137 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
138 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
142 # FourKeys_to_TwoKeys
143 fourkeys_to_twokeys => [
145 'display' => 'fourkeys_to_twokeys->twokeys',
146 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
147 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
148 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
149 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
152 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
153 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
154 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
155 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
156 'foreigncols' => [qw(foo bar hello goodbye)],
157 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
164 'display' => 'cd_to_producer->cd',
165 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
166 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
167 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
168 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
171 'display' => 'cd_to_producer->producer',
172 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
173 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
174 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
175 on_delete => '', on_update => '', deferrable => 1,
182 'display' => 'self_ref_alias->self_ref for self_ref',
183 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
184 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
185 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
186 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
189 'display' => 'self_ref_alias->self_ref for alias',
190 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
191 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
192 'selfcols' => ['alias'], 'foreigncols' => ['id'],
193 on_delete => '', on_update => '', deferrable => 1,
200 'display' => 'cd->artist',
201 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
202 'selftable' => 'cd', 'foreigntable' => 'artist',
203 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
204 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
208 # Artist_undirected_map
209 artist_undirected_map => [
211 'display' => 'artist_undirected_map->artist for id1',
212 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
213 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
214 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
215 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
218 'display' => 'artist_undirected_map->artist for id2',
219 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
220 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
221 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
222 on_delete => '', on_update => '', deferrable => 1,
229 'display' => 'track->cd',
230 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
231 'selftable' => 'track', 'foreigntable' => 'cd',
232 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
233 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
240 'display' => 'treelike->treelike for parent',
241 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
242 'selftable' => 'treelike', 'foreigntable' => 'treelike',
243 'selfcols' => ['parent'], 'foreigncols' => ['id'],
244 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
251 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
252 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
253 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
254 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
255 on_delete => '', on_update => '', deferrable => 1,
262 'display' => 'tags->cd',
263 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
264 'selftable' => 'tags', 'foreigntable' => 'cd',
265 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
266 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
273 'display' => 'bookmark->link',
274 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
275 'selftable' => 'bookmark', 'foreigntable' => 'link',
276 'selfcols' => ['link'], 'foreigncols' => ['id'],
277 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
283 'display' => 'forceforeign->artist',
284 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
285 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
286 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
288 on_delete => '', on_update => '', deferrable => 1,
293 my %unique_constraints = (
297 'display' => 'cd artist and title unique',
298 'name' => 'cd_artist_title',
299 'table' => 'cd', 'cols' => ['artist', 'title'],
306 'display' => 'producer name unique',
307 'name' => 'prod_name', # explicit name
308 'table' => 'producer', 'cols' => ['name'],
315 'display' => 'twokeytreelike name unique',
316 'name' => 'tktlnameunique', # explicit name
317 'table' => 'twokeytreelike', 'cols' => ['name'],
322 # Constraint is commented out in DBICTest/Schema/Employee.pm
325 # 'display' => 'employee position and group_id unique',
326 # 'name' => 'position_group',
327 # 'table' => 'employee', cols => ['position', 'group_id'],
340 'fields' => ['title']
345 my $tschema = $translator->schema();
346 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
348 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
350 # Test that the Artist resultsource sqlt_deploy_hook was called okay and added
353 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1)
354 if $schema->storage->sqlt_type ne 'SQLite';
357 { $_->name eq 'artist_name_hookidx' }
358 $tschema->get_table('artist')->get_indices
359 ), 'sqlt_deploy_hook fired within a resultsource');
362 # Test that nonexistent constraints are not found
363 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
364 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
365 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
366 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
367 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
368 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
370 for my $expected_constraints (keys %fk_constraints) {
371 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
372 my $desc = $expected_constraint->{display};
373 my $constraint = get_constraint(
375 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
376 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
378 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
379 test_fk($expected_constraint, $constraint);
383 for my $expected_constraints (keys %unique_constraints) {
384 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
385 my $desc = $expected_constraint->{display};
386 my $constraint = get_constraint(
387 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
389 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
390 test_unique($expected_constraint, $constraint);
394 for my $table_index (keys %indexes) {
395 for my $expected_index ( @{ $indexes{$table_index} } ) {
396 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
400 # Returns the Constraint object for the specified constraint type, table and
401 # columns from the SQL::Translator schema, or undef if no matching constraint
404 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
405 # two parameters are not used.
407 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
408 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
411 my $table = $tschema->get_table($table_name);
413 my %fields = map { $_ => 1 } @$cols;
414 my %f_fields = map { $_ => 1 } @$f_cols;
416 die "No $table_name" unless $table;
418 for my $constraint ( $table->get_constraints ) {
419 next unless $constraint->type eq $type;
420 next unless $constraint->reference_table eq $f_table;
422 my %rev_fields = map { $_ => 1 } $constraint->fields;
423 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
425 # Check that the given fields are a subset of the constraint's fields
426 for my $field ($constraint->fields) {
427 next CONSTRAINT unless $fields{$field};
429 if ($type eq 'FOREIGN KEY') {
430 for my $f_field ($constraint->reference_fields) {
431 next CONSTRAINT unless $f_fields{$f_field};
435 # Check that the constraint's fields are a subset of the given fields
436 for my $field (@$cols) {
437 next CONSTRAINT unless $rev_fields{$field};
439 if ($type eq 'FOREIGN KEY') {
440 for my $f_field (@$f_cols) {
441 next CONSTRAINT unless $rev_f_fields{$f_field};
445 return $constraint; # everything passes, found the constraint
447 return undef; # didn't find a matching constraint
451 my ($table_name, $index) = @_;
453 my $table = $tschema->get_table($table_name);
456 for my $cand_index ( $table->get_indices ) {
458 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
459 || $index->{type} && $cand_index->type ne $index->{type};
461 my %idx_fields = map { $_ => 1 } $cand_index->fields;
463 for my $field ( @{ $index->{fields} } ) {
464 next CAND_INDEX unless $idx_fields{$field};
467 %idx_fields = map { $_ => 1 } @{$index->{fields}};
468 for my $field ( $cand_index->fields) {
469 next CAND_INDEX unless $idx_fields{$field};
475 return undef; # No matching idx
478 # Test parameters in a FOREIGN KEY constraint other than columns
480 my ($expected, $got) = @_;
481 my $desc = $expected->{display};
482 is( $got->name, $expected->{name},
483 "name parameter correct for '$desc'" );
484 is( $got->on_delete, $expected->{on_delete},
485 "on_delete parameter correct for '$desc'" );
486 is( $got->on_update, $expected->{on_update},
487 "on_update parameter correct for '$desc'" );
488 is( $got->deferrable, $expected->{deferrable},
489 "is_deferrable parameter correct for '$desc'" );
491 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
493 if ($expected->{noindex}) {
494 ok( !defined $index, "index doesn't for '$desc'" );
496 ok( defined $index, "index exists for '$desc'" );
497 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
502 my ($expected, $got) = @_;
503 my $desc = $expected->{display};
504 is( $got->name, $expected->{name},
505 "name parameter correct for '$desc'" );