1use strict; 2use warnings; 3 4use Test::More; 5use lib qw(t/lib); 6use DBICTest; 7 8BEGIN { 9 require DBIx::Class; 10 plan skip_all => 11 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy') 12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy') 13} 14 15my $custom_deployment_statements_called = 0; 16 17sub DBICTest::Schema::deployment_statements { 18 $custom_deployment_statements_called = 1; 19 my $self = shift; 20 return $self->next::method(@_); 21} 22 23my $schema = DBICTest->init_schema (no_deploy => 1); 24 25 26# Check deployment statements ctx sensitivity 27{ 28 my $not_first_table_creation_re = qr/CREATE TABLE fourkeys_to_twokeys/; 29 30 31 my $statements = $schema->deployment_statements; 32 like ( 33 $statements, 34 $not_first_table_creation_re, 35 'All create statements returned in 1 string in scalar ctx' 36 ); 37 38 my @statements = $schema->deployment_statements; 39 cmp_ok (scalar @statements, '>', 1, 'Multiple statement lines in array ctx'); 40 41 my $i = 0; 42 while ($i <= $#statements) { 43 last if $statements[$i] =~ $not_first_table_creation_re; 44 $i++; 45 } 46 47 ok ( 48 ($i > 0) && ($i <= $#statements), 49 "Creation statement was found somewherere within array ($i)" 50 ); 51} 52 53 54 55{ 56 my $deploy_hook_called = 0; 57 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) = @_; 61 62 $deploy_hook_called = 1; 63 64 is ( 65 $sqlt_table->schema->translator->producer_type, 66 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type), 67 'Production type passed to translator object', 68 ); 69 70 if ($schema->storage->sqlt_type eq 'SQLite' ) { 71 $sqlt_table->add_index( name => 'track_title', fields => ['title'] ) 72 or die $sqlt_table->error; 73 } 74 75 $self->default_sqlt_deploy_hook($sqlt_table); 76 }); 77 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'); 81} 82 83 84my $translator = SQL::Translator->new( 85 parser_args => { 86 'DBIx::Schema' => $schema, 87 }, 88 producer_args => {}, 89); 90 91{ 92 my $warn = ''; 93 local $SIG{__WARN__} = sub { $warn = shift }; 94 95 my $relinfo = $schema->source('Artist')->relationship_info ('cds'); 96 local $relinfo->{attrs}{on_delete} = 'restrict'; 97 98 99 $translator->parser('SQL::Translator::Parser::DBIx::Class'); 100 $translator->producer('SQLite'); 101 102 my $output = $translator->translate(); 103 104 ok($output, "SQLT produced someoutput") 105 or diag($translator->error); 106 107 108 like ( 109 $warn, 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', 112 ); 113} 114 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 120 121my %fk_constraints = ( 122 123 # TwoKeys 124 twokeys => [ 125 { 126 'display' => 'twokeys->cd', 127 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd', 128 'selftable' => 'twokeys', 'foreigntable' => 'cd', 129 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], 130 'noindex' => 1, 131 on_delete => '', on_update => '', deferrable => 0, 132 }, 133 { 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, 139 }, 140 ], 141 142 # FourKeys_to_TwoKeys 143 fourkeys_to_twokeys => [ 144 { 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, 150 }, 151 { 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, 158 }, 159 ], 160 161 # CD_to_Producer 162 cd_to_producer => [ 163 { 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, 169 }, 170 { 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, 176 }, 177 ], 178 179 # Self_ref_alias 180 self_ref_alias => [ 181 { 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, 187 }, 188 { 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, 194 }, 195 ], 196 197 # CD 198 cd => [ 199 { 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, 205 }, 206 ], 207 208 # Artist_undirected_map 209 artist_undirected_map => [ 210 { 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, 216 }, 217 { 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, 223 }, 224 ], 225 226 # Track 227 track => [ 228 { 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, 234 }, 235 ], 236 237 # TreeLike 238 treelike => [ 239 { 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, 245 }, 246 ], 247 248 # TwoKeyTreeLike 249 twokeytreelike => [ 250 { 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, 256 }, 257 ], 258 259 # Tags 260 tags => [ 261 { 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, 267 }, 268 ], 269 270 # Bookmark 271 bookmark => [ 272 { 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, 278 }, 279 ], 280 # ForceForeign 281 forceforeign => [ 282 { 283 'display' => 'forceforeign->artist', 284 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist', 285 'selftable' => 'forceforeign', 'foreigntable' => 'artist', 286 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], 287 'noindex' => 1, 288 on_delete => '', on_update => '', deferrable => 1, 289 }, 290 ], 291); 292 293my %unique_constraints = ( 294 # CD 295 cd => [ 296 { 297 'display' => 'cd artist and title unique', 298 'name' => 'cd_artist_title', 299 'table' => 'cd', 'cols' => ['artist', 'title'], 300 }, 301 ], 302 303 # Producer 304 producer => [ 305 { 306 'display' => 'producer name unique', 307 'name' => 'prod_name', # explicit name 308 'table' => 'producer', 'cols' => ['name'], 309 }, 310 ], 311 312 # TwoKeyTreeLike 313 twokeytreelike => [ 314 { 315 'display' => 'twokeytreelike name unique', 316 'name' => 'tktlnameunique', # explicit name 317 'table' => 'twokeytreelike', 'cols' => ['name'], 318 }, 319 ], 320 321 # Employee 322# Constraint is commented out in DBICTest/Schema/Employee.pm 323# employee => [ 324# { 325# 'display' => 'employee position and group_id unique', 326# 'name' => 'position_group', 327# 'table' => 'employee', cols => ['position', 'group_id'], 328# }, 329# ], 330); 331 332my %indexes = ( 333 artist => [ 334 { 335 'fields' => ['name'] 336 }, 337 ], 338 track => [ 339 { 340 'fields' => ['title'] 341 } 342 ], 343); 344 345my $tschema = $translator->schema(); 346# Test that the $schema->sqlt_deploy_hook was called okay and that it removed 347# the 'dummy' table 348ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook"); 349 350# Test that the Artist resultsource sqlt_deploy_hook was called okay and added 351# an index 352SKIP: { 353 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1) 354 if $schema->storage->sqlt_type ne 'SQLite'; 355 356 ok( ( grep 357 { $_->name eq 'artist_name_hookidx' } 358 $tschema->get_table('artist')->get_indices 359 ), 'sqlt_deploy_hook fired within a resultsource'); 360} 361 362# Test that nonexistent constraints are not found 363my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']); 364ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' ); 365$constraint = get_constraint('UNIQUE', 'cd', ['artist']); 366ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' ); 367$constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']); 368ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' ); 369 370for 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( 374 'FOREIGN KEY', 375 $expected_constraint->{selftable}, $expected_constraint->{selfcols}, 376 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols}, 377 ); 378 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" ); 379 test_fk($expected_constraint, $constraint); 380 } 381} 382 383for 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}, 388 ); 389 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" ); 390 test_unique($expected_constraint, $constraint); 391 } 392} 393 394for 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"); 397 } 398} 399 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 402# is found. 403# 404# NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last 405# two parameters are not used. 406sub get_constraint { 407 my ($type, $table_name, $cols, $f_table, $f_cols) = @_; 408 $f_table ||= ''; # For UNIQUE constraints, reference_table is '' 409 $f_cols ||= []; 410 411 my $table = $tschema->get_table($table_name); 412 413 my %fields = map { $_ => 1 } @$cols; 414 my %f_fields = map { $_ => 1 } @$f_cols; 415 416 die "No $table_name" unless $table; 417 CONSTRAINT: 418 for my $constraint ( $table->get_constraints ) { 419 next unless $constraint->type eq $type; 420 next unless $constraint->reference_table eq $f_table; 421 422 my %rev_fields = map { $_ => 1 } $constraint->fields; 423 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields; 424 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}; 428 } 429 if ($type eq 'FOREIGN KEY') { 430 for my $f_field ($constraint->reference_fields) { 431 next CONSTRAINT unless $f_fields{$f_field}; 432 } 433 } 434 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}; 438 } 439 if ($type eq 'FOREIGN KEY') { 440 for my $f_field (@$f_cols) { 441 next CONSTRAINT unless $rev_f_fields{$f_field}; 442 } 443 } 444 445 return $constraint; # everything passes, found the constraint 446 } 447 return undef; # didn't find a matching constraint 448} 449 450sub get_index { 451 my ($table_name, $index) = @_; 452 453 my $table = $tschema->get_table($table_name); 454 455 CAND_INDEX: 456 for my $cand_index ( $table->get_indices ) { 457 458 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name} 459 || $index->{type} && $cand_index->type ne $index->{type}; 460 461 my %idx_fields = map { $_ => 1 } $cand_index->fields; 462 463 for my $field ( @{ $index->{fields} } ) { 464 next CAND_INDEX unless $idx_fields{$field}; 465 } 466 467 %idx_fields = map { $_ => 1 } @{$index->{fields}}; 468 for my $field ( $cand_index->fields) { 469 next CAND_INDEX unless $idx_fields{$field}; 470 } 471 472 return $cand_index; 473 } 474 475 return undef; # No matching idx 476} 477 478# Test parameters in a FOREIGN KEY constraint other than columns 479sub test_fk { 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'" ); 490 491 my $index = get_index( $got->table, { fields => $expected->{selfcols} } ); 492 493 if ($expected->{noindex}) { 494 ok( !defined $index, "index doesn't for '$desc'" ); 495 } else { 496 ok( defined $index, "index exists for '$desc'" ); 497 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" ); 498 } 499} 500 501sub test_unique { 502 my ($expected, $got) = @_; 503 my $desc = $expected->{display}; 504 is( $got->name, $expected->{name}, 505 "name parameter correct for '$desc'" ); 506} 507 508done_testing; 509