'undef =>' isn't what one would want
[dbsrgits/DBIx-Class-Historic.git] / t / 86sqlt.t
CommitLineData
cb551b07 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'deploy';
2
70350518 3use strict;
4use warnings;
5
637ca936 6use Test::More;
052a832c 7use Test::Warn;
fed15b91 8use Scalar::Util 'blessed';
9
cb551b07 10use lib qw(t/lib);
11use DBICTest;
637ca936 12
c66a805c 13my $custom_deployment_statements_called = 0;
14
15sub DBICTest::Schema::deployment_statements {
16 $custom_deployment_statements_called = 1;
17 my $self = shift;
18 return $self->next::method(@_);
19}
20
30ae562b 21# Check deployment statements ctx sensitivity
22{
111364b3 23 my $schema = DBICTest->init_schema (no_deploy => 1, quote_names => 1);
f9b5239a 24 my $not_first_table_creation_re = qr/CREATE TABLE "fourkeys_to_twokeys"/;
30ae562b 25
30ae562b 26 my $statements = $schema->deployment_statements;
27 like (
28 $statements,
29 $not_first_table_creation_re,
30 'All create statements returned in 1 string in scalar ctx'
31 );
32
33 my @statements = $schema->deployment_statements;
34 cmp_ok (scalar @statements, '>', 1, 'Multiple statement lines in array ctx');
35
36 my $i = 0;
37 while ($i <= $#statements) {
38 last if $statements[$i] =~ $not_first_table_creation_re;
39 $i++;
40 }
41
42 ok (
43 ($i > 0) && ($i <= $#statements),
44 "Creation statement was found somewherere within array ($i)"
45 );
46}
47
fed15b91 48{
49 # use our own throw-away schema, since we'll be deploying twice
50 my $schema = DBICTest->init_schema (no_deploy => 1);
51
52 my $deploy_hook_called = 0;
53 $custom_deployment_statements_called = 0;
54
55 # add a temporary sqlt_deploy_hook to a source
a267ea85 56 local $DBICTest::Schema::Track::hook_cb = sub {
7f3fd262 57 my ($class, $sqlt_table) = @_;
fed15b91 58
59 $deploy_hook_called = 1;
60
7f3fd262 61 is ($class, 'DBICTest::Track', 'Result class passed to plain hook');
30ae562b 62
fed15b91 63 is (
64 $sqlt_table->schema->translator->producer_type,
65 join ('::', 'SQL::Translator::Producer', $schema->storage->sqlt_type),
66 'Production type passed to translator object',
67 );
68 };
69
a267ea85 70 my $component_deploy_hook_called = 0;
71 local $DBICTest::DeployComponent::hook_cb = sub {
72 $component_deploy_hook_called = 1;
73 };
74
fed15b91 75 $schema->deploy; # do not remove, this fires the is() test in the callback above
76 ok($deploy_hook_called, 'deploy hook got called');
77 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
a267ea85 78 ok($component_deploy_hook_called, 'component deploy hook got called');
fed15b91 79}
30ae562b 80
6ddb4ac0 81my $schema = DBICTest->init_schema (no_deploy => 1);
82
427c4089 83{
84 my $deploy_hook_called = 0;
fed15b91 85 $custom_deployment_statements_called = 0;
65d35121 86 my $sqlt_type = $schema->storage->sqlt_type;
0fd7e9a3 87
427c4089 88 # replace the sqlt calback with a custom version ading an index
89 $schema->source('Track')->sqlt_deploy_callback(sub {
90 my ($self, $sqlt_table) = @_;
0fd7e9a3 91
427c4089 92 $deploy_hook_called = 1;
0fd7e9a3 93
427c4089 94 is (
95 $sqlt_table->schema->translator->producer_type,
65d35121 96 join ('::', 'SQL::Translator::Producer', $sqlt_type),
427c4089 97 'Production type passed to translator object',
98 );
0fd7e9a3 99
65d35121 100 if ($sqlt_type eq 'SQLite' ) {
427c4089 101 $sqlt_table->add_index( name => 'track_title', fields => ['title'] )
102 or die $sqlt_table->error;
103 }
104
105 $self->default_sqlt_deploy_hook($sqlt_table);
106 });
107
108 $schema->deploy; # do not remove, this fires the is() test in the callback above
109 ok($deploy_hook_called, 'deploy hook got called');
c66a805c 110 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
427c4089 111}
637ca936 112
637ca936 113
8273e845 114my $translator = SQL::Translator->new(
661fc8eb 115 parser_args => {
052a832c 116 dbic_schema => $schema,
661fc8eb 117 },
118 producer_args => {},
637ca936 119);
120
052a832c 121warnings_exist {
e377d723 122 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
123 local $relinfo->{attrs}{on_delete} = 'restrict';
637ca936 124
e377d723 125 $translator->parser('SQL::Translator::Parser::DBIx::Class');
126 $translator->producer('SQLite');
256e87b0 127
e377d723 128 my $output = $translator->translate();
129
130 ok($output, "SQLT produced someoutput")
131 or diag($translator->error);
132
052a832c 133} [
134 (qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/) x 2
135], 'Warn about dubious on_delete/on_update attributes';
256e87b0 136
b1edf9f9 137# Note that the constraints listed here are the only ones that are tested -- if
138# more exist in the Schema than are listed here and all listed constraints are
c75b18e9 139# correct, the test will still pass. If you add a class with UNIQUE or FOREIGN
140# KEY constraints to DBICTest::Schema, add tests here if you think the existing
141# test coverage is not sufficient
b1edf9f9 142
143my %fk_constraints = (
661fc8eb 144
145 # TwoKeys
b1edf9f9 146 twokeys => [
147 {
148 'display' => 'twokeys->cd',
bb0f01d0 149 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
8273e845 150 'selftable' => 'twokeys', 'foreigntable' => 'cd',
151 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
9c1f7965 152 'noindex' => 1,
13de943d 153 on_delete => '', on_update => '', deferrable => 0,
b1edf9f9 154 },
155 {
156 'display' => 'twokeys->artist',
bb0f01d0 157 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
8273e845 158 'selftable' => 'twokeys', 'foreigntable' => 'artist',
b1edf9f9 159 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
e394339b 160 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 161 },
162 ],
661fc8eb 163
164 # FourKeys_to_TwoKeys
b1edf9f9 165 fourkeys_to_twokeys => [
166 {
167 'display' => 'fourkeys_to_twokeys->twokeys',
f34cb1fd 168 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
8273e845 169 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
170 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
e394339b 171 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 172 },
173 {
f34cb1fd 174 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
d1b264d3 175 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
8273e845 176 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
b1edf9f9 177 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
8273e845 178 'foreigncols' => [qw(foo bar hello goodbye)],
e394339b 179 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 180 },
181 ],
661fc8eb 182
183 # CD_to_Producer
b1edf9f9 184 cd_to_producer => [
185 {
186 'display' => 'cd_to_producer->cd',
bb0f01d0 187 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
8273e845 188 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
b1edf9f9 189 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 190 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 191 },
192 {
193 'display' => 'cd_to_producer->producer',
bb0f01d0 194 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
8273e845 195 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
b1edf9f9 196 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
e394339b 197 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 198 },
199 ],
661fc8eb 200
201 # Self_ref_alias
b1edf9f9 202 self_ref_alias => [
203 {
204 'display' => 'self_ref_alias->self_ref for self_ref',
bb0f01d0 205 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
8273e845 206 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
b1edf9f9 207 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
e394339b 208 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 209 },
210 {
211 'display' => 'self_ref_alias->self_ref for alias',
bb0f01d0 212 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
8273e845 213 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
b1edf9f9 214 'selfcols' => ['alias'], 'foreigncols' => ['id'],
e394339b 215 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 216 },
217 ],
661fc8eb 218
219 # CD
b1edf9f9 220 cd => [
221 {
222 'display' => 'cd->artist',
bb0f01d0 223 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
8273e845 224 'selftable' => 'cd', 'foreigntable' => 'artist',
b1edf9f9 225 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
a0dd8679 226 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 227 },
228 ],
661fc8eb 229
230 # Artist_undirected_map
b1edf9f9 231 artist_undirected_map => [
232 {
233 'display' => 'artist_undirected_map->artist for id1',
bb0f01d0 234 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
8273e845 235 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
b1edf9f9 236 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
e377d723 237 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 238 },
239 {
240 'display' => 'artist_undirected_map->artist for id2',
bb0f01d0 241 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
8273e845 242 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
b1edf9f9 243 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
b230b4be 244 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 245 },
246 ],
661fc8eb 247
248 # Track
b1edf9f9 249 track => [
250 {
251 'display' => 'track->cd',
bb0f01d0 252 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
8273e845 253 'selftable' => 'track', 'foreigntable' => 'cd',
b1edf9f9 254 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 255 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 256 },
257 ],
661fc8eb 258
259 # TreeLike
b1edf9f9 260 treelike => [
261 {
262 'display' => 'treelike->treelike for parent',
61177e44 263 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
8273e845 264 'selftable' => 'treelike', 'foreigntable' => 'treelike',
61177e44 265 'selfcols' => ['parent'], 'foreigncols' => ['id'],
e394339b 266 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 267 },
268 ],
269
270 # TwoKeyTreeLike
271 twokeytreelike => [
272 {
273 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
bb0f01d0 274 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
8273e845 275 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
b1edf9f9 276 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
e394339b 277 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 278 },
279 ],
ae515736 280
661fc8eb 281 # Tags
b1edf9f9 282 tags => [
283 {
284 'display' => 'tags->cd',
bb0f01d0 285 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
8273e845 286 'selftable' => 'tags', 'foreigntable' => 'cd',
b1edf9f9 287 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 288 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 289 },
290 ],
661fc8eb 291
292 # Bookmark
b1edf9f9 293 bookmark => [
294 {
295 'display' => 'bookmark->link',
bb0f01d0 296 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
8273e845 297 'selftable' => 'bookmark', 'foreigntable' => 'link',
b1edf9f9 298 'selfcols' => ['link'], 'foreigncols' => ['id'],
def17c59 299 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 300 },
301 ],
a0024650 302 # ForceForeign
303 forceforeign => [
304 {
305 'display' => 'forceforeign->artist',
bb0f01d0 306 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
8273e845 307 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
308 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
827a808f 309 'noindex' => 1,
e394339b 310 on_delete => '', on_update => '', deferrable => 1,
a0024650 311 },
312 ],
b1edf9f9 313);
314
315my %unique_constraints = (
316 # CD
317 cd => [
318 {
319 'display' => 'cd artist and title unique',
0da8b7da 320 'name' => 'cd_artist_title',
b1edf9f9 321 'table' => 'cd', 'cols' => ['artist', 'title'],
322 },
323 ],
324
325 # Producer
326 producer => [
327 {
328 'display' => 'producer name unique',
0da8b7da 329 'name' => 'prod_name', # explicit name
b1edf9f9 330 'table' => 'producer', 'cols' => ['name'],
331 },
332 ],
333
334 # TwoKeyTreeLike
335 twokeytreelike => [
336 {
337 'display' => 'twokeytreelike name unique',
0da8b7da 338 'name' => 'tktlnameunique', # explicit name
b1edf9f9 339 'table' => 'twokeytreelike', 'cols' => ['name'],
340 },
341 ],
342
343 # Employee
344# Constraint is commented out in DBICTest/Schema/Employee.pm
345# employee => [
346# {
347# 'display' => 'employee position and group_id unique',
0da8b7da 348# 'name' => 'position_group',
b1edf9f9 349# 'table' => 'employee', cols => ['position', 'group_id'],
350# },
351# ],
7b90bb13 352);
353
17cab2f0 354my %indexes = (
c385ecea 355 artist => [
356 {
357 'fields' => ['name']
358 },
f89bb832 359 ],
360 track => [
361 {
362 'fields' => ['title']
363 }
364 ],
c385ecea 365);
366
637ca936 367my $tschema = $translator->schema();
d6c79cb3 368# Test that the $schema->sqlt_deploy_hook was called okay and that it removed
458e0292 369# the 'dummy' table
370ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
d6c79cb3 371
1f5bf324 372# Test that the Artist resultsource sqlt_deploy_hook was called okay and added
373# an index
374SKIP: {
375 skip ('Artist sqlt_deploy_hook is only called with an SQLite backend', 1)
376 if $schema->storage->sqlt_type ne 'SQLite';
377
8273e845 378 ok( ( grep
1f5bf324 379 { $_->name eq 'artist_name_hookidx' }
380 $tschema->get_table('artist')->get_indices
381 ), 'sqlt_deploy_hook fired within a resultsource');
382}
383
b1edf9f9 384# Test that nonexistent constraints are not found
385my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
386ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
387$constraint = get_constraint('UNIQUE', 'cd', ['artist']);
388ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
a0024650 389$constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
390ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
b1edf9f9 391
392for my $expected_constraints (keys %fk_constraints) {
393 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
394 my $desc = $expected_constraint->{display};
395 my $constraint = get_constraint(
396 'FOREIGN KEY',
397 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
398 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
399 );
400 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
401 test_fk($expected_constraint, $constraint);
661fc8eb 402 }
637ca936 403}
404
b1edf9f9 405for my $expected_constraints (keys %unique_constraints) {
406 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
407 my $desc = $expected_constraint->{display};
408 my $constraint = get_constraint(
409 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
410 );
411 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
0da8b7da 412 test_unique($expected_constraint, $constraint);
b1edf9f9 413 }
637ca936 414}
415
17cab2f0 416for my $table_index (keys %indexes) {
417 for my $expected_index ( @{ $indexes{$table_index} } ) {
c385ecea 418 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
419 }
420}
421
b1edf9f9 422# Returns the Constraint object for the specified constraint type, table and
423# columns from the SQL::Translator schema, or undef if no matching constraint
424# is found.
425#
426# NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
427# two parameters are not used.
428sub get_constraint {
429 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
430 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
431 $f_cols ||= [];
432
433 my $table = $tschema->get_table($table_name);
434
435 my %fields = map { $_ => 1 } @$cols;
436 my %f_fields = map { $_ => 1 } @$f_cols;
437
a7e65bb5 438 die "No $table_name" unless $table;
b1edf9f9 439 CONSTRAINT:
440 for my $constraint ( $table->get_constraints ) {
441 next unless $constraint->type eq $type;
442 next unless $constraint->reference_table eq $f_table;
443
444 my %rev_fields = map { $_ => 1 } $constraint->fields;
445 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
446
447 # Check that the given fields are a subset of the constraint's fields
448 for my $field ($constraint->fields) {
449 next CONSTRAINT unless $fields{$field};
450 }
451 if ($type eq 'FOREIGN KEY') {
452 for my $f_field ($constraint->reference_fields) {
453 next CONSTRAINT unless $f_fields{$f_field};
661fc8eb 454 }
b1edf9f9 455 }
661fc8eb 456
b1edf9f9 457 # Check that the constraint's fields are a subset of the given fields
458 for my $field (@$cols) {
459 next CONSTRAINT unless $rev_fields{$field};
460 }
461 if ($type eq 'FOREIGN KEY') {
462 for my $f_field (@$f_cols) {
463 next CONSTRAINT unless $rev_f_fields{$f_field};
661fc8eb 464 }
465 }
b1edf9f9 466
467 return $constraint; # everything passes, found the constraint
661fc8eb 468 }
b1edf9f9 469 return undef; # didn't find a matching constraint
7b90bb13 470}
471
c385ecea 472sub get_index {
473 my ($table_name, $index) = @_;
474
475 my $table = $tschema->get_table($table_name);
476
477 CAND_INDEX:
478 for my $cand_index ( $table->get_indices ) {
8273e845 479
c385ecea 480 next CAND_INDEX if $index->{name} && $cand_index->name ne $index->{name}
481 || $index->{type} && $cand_index->type ne $index->{type};
482
483 my %idx_fields = map { $_ => 1 } $cand_index->fields;
484
485 for my $field ( @{ $index->{fields} } ) {
486 next CAND_INDEX unless $idx_fields{$field};
487 }
488
489 %idx_fields = map { $_ => 1 } @{$index->{fields}};
490 for my $field ( $cand_index->fields) {
491 next CAND_INDEX unless $idx_fields{$field};
492 }
493
494 return $cand_index;
495 }
496
497 return undef; # No matching idx
498}
499
b1edf9f9 500# Test parameters in a FOREIGN KEY constraint other than columns
501sub test_fk {
502 my ($expected, $got) = @_;
503 my $desc = $expected->{display};
0da8b7da 504 is( $got->name, $expected->{name},
827a808f 505 "name parameter correct for '$desc'" );
b1edf9f9 506 is( $got->on_delete, $expected->{on_delete},
827a808f 507 "on_delete parameter correct for '$desc'" );
b1edf9f9 508 is( $got->on_update, $expected->{on_update},
827a808f 509 "on_update parameter correct for '$desc'" );
13de943d 510 is( $got->deferrable, $expected->{deferrable},
827a808f 511 "is_deferrable parameter correct for '$desc'" );
0da8b7da 512
513 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
9c1f7965 514
515 if ($expected->{noindex}) {
827a808f 516 ok( !defined $index, "index doesn't for '$desc'" );
9c1f7965 517 } else {
827a808f 518 ok( defined $index, "index exists for '$desc'" );
519 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
9c1f7965 520 }
0da8b7da 521}
522
523sub test_unique {
524 my ($expected, $got) = @_;
525 my $desc = $expected->{display};
526 is( $got->name, $expected->{name},
827a808f 527 "name parameter correct for '$desc'" );
637ca936 528}
0fd7e9a3 529
530done_testing;