This test in fact requires DT::F::SQLite
[dbsrgits/DBIx-Class-Historic.git] / t / 86sqlt.t
CommitLineData
70350518 1use strict;
2use warnings;
3
637ca936 4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
637ca936 7
7f6f5b69 8BEGIN {
2527233b 9 require DBIx::Class;
7f6f5b69 10 plan skip_all =>
2527233b 11 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('deploy')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('deploy')
7f6f5b69 13}
637ca936 14
c66a805c 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
0fd7e9a3 23my $schema = DBICTest->init_schema (no_deploy => 1);
24
30ae562b 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
427c4089 55{
56 my $deploy_hook_called = 0;
0fd7e9a3 57
427c4089 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) = @_;
0fd7e9a3 61
427c4089 62 $deploy_hook_called = 1;
0fd7e9a3 63
427c4089 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 );
0fd7e9a3 69
427c4089 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');
c66a805c 80 ok($custom_deployment_statements_called, '->deploy used the schemas deploy_statements method');
427c4089 81}
637ca936 82
637ca936 83
661fc8eb 84my $translator = SQL::Translator->new(
85 parser_args => {
86 'DBIx::Schema' => $schema,
87 },
88 producer_args => {},
637ca936 89);
90
e377d723 91{
92 my $warn = '';
93 local $SIG{__WARN__} = sub { $warn = shift };
637ca936 94
e377d723 95 my $relinfo = $schema->source('Artist')->relationship_info ('cds');
96 local $relinfo->{attrs}{on_delete} = 'restrict';
637ca936 97
f89bb832 98
e377d723 99 $translator->parser('SQL::Translator::Parser::DBIx::Class');
100 $translator->producer('SQLite');
256e87b0 101
e377d723 102 my $output = $translator->translate();
103
104 ok($output, "SQLT produced someoutput")
105 or diag($translator->error);
106
0fd7e9a3 107
48850f9a 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 );
e377d723 113}
256e87b0 114
b1edf9f9 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
c75b18e9 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
b1edf9f9 120
121my %fk_constraints = (
661fc8eb 122
123 # TwoKeys
b1edf9f9 124 twokeys => [
125 {
126 'display' => 'twokeys->cd',
bb0f01d0 127 'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
b1edf9f9 128 'selftable' => 'twokeys', 'foreigntable' => 'cd',
129 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
9c1f7965 130 'noindex' => 1,
13de943d 131 on_delete => '', on_update => '', deferrable => 0,
b1edf9f9 132 },
133 {
134 'display' => 'twokeys->artist',
bb0f01d0 135 'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
b1edf9f9 136 'selftable' => 'twokeys', 'foreigntable' => 'artist',
137 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
e394339b 138 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 139 },
140 ],
661fc8eb 141
142 # FourKeys_to_TwoKeys
b1edf9f9 143 fourkeys_to_twokeys => [
144 {
145 'display' => 'fourkeys_to_twokeys->twokeys',
f34cb1fd 146 'name' => 'fourkeys_to_twokeys_fk_t_artist_t_cd', 'index_name' => 'fourkeys_to_twokeys_idx_t_artist_t_cd',
b1edf9f9 147 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
148 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
e394339b 149 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 150 },
151 {
f34cb1fd 152 'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_bar_f_hello_f_goodbye',
d1b264d3 153 'name' => 'fourkeys_to_twokeys_fk_f_foo_f_bar_f_hello_f_goodbye',
b1edf9f9 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)],
e394339b 157 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 158 },
159 ],
661fc8eb 160
161 # CD_to_Producer
b1edf9f9 162 cd_to_producer => [
163 {
164 'display' => 'cd_to_producer->cd',
bb0f01d0 165 'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
b1edf9f9 166 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
167 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 168 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 169 },
170 {
171 'display' => 'cd_to_producer->producer',
bb0f01d0 172 'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
b1edf9f9 173 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
174 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
e394339b 175 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 176 },
177 ],
661fc8eb 178
179 # Self_ref_alias
b1edf9f9 180 self_ref_alias => [
181 {
182 'display' => 'self_ref_alias->self_ref for self_ref',
bb0f01d0 183 'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
b1edf9f9 184 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
185 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
e394339b 186 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 187 },
188 {
189 'display' => 'self_ref_alias->self_ref for alias',
bb0f01d0 190 'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
b1edf9f9 191 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
192 'selfcols' => ['alias'], 'foreigncols' => ['id'],
e394339b 193 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 194 },
195 ],
661fc8eb 196
197 # CD
b1edf9f9 198 cd => [
199 {
200 'display' => 'cd->artist',
bb0f01d0 201 'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
b1edf9f9 202 'selftable' => 'cd', 'foreigntable' => 'artist',
203 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
a0dd8679 204 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 205 },
206 ],
661fc8eb 207
208 # Artist_undirected_map
b1edf9f9 209 artist_undirected_map => [
210 {
211 'display' => 'artist_undirected_map->artist for id1',
bb0f01d0 212 'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
b1edf9f9 213 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
214 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
e377d723 215 on_delete => 'RESTRICT', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 216 },
217 {
218 'display' => 'artist_undirected_map->artist for id2',
bb0f01d0 219 'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
b1edf9f9 220 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
221 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
b230b4be 222 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 223 },
224 ],
661fc8eb 225
226 # Track
b1edf9f9 227 track => [
228 {
229 'display' => 'track->cd',
bb0f01d0 230 'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
b1edf9f9 231 'selftable' => 'track', 'foreigntable' => 'cd',
232 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 233 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 234 },
235 ],
661fc8eb 236
237 # TreeLike
b1edf9f9 238 treelike => [
239 {
240 'display' => 'treelike->treelike for parent',
61177e44 241 'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
b1edf9f9 242 'selftable' => 'treelike', 'foreigntable' => 'treelike',
61177e44 243 'selfcols' => ['parent'], 'foreigncols' => ['id'],
e394339b 244 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 245 },
246 ],
247
248 # TwoKeyTreeLike
249 twokeytreelike => [
250 {
251 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
bb0f01d0 252 'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
b1edf9f9 253 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
254 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
e394339b 255 on_delete => '', on_update => '', deferrable => 1,
b1edf9f9 256 },
257 ],
ae515736 258
661fc8eb 259 # Tags
b1edf9f9 260 tags => [
261 {
262 'display' => 'tags->cd',
bb0f01d0 263 'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
b1edf9f9 264 'selftable' => 'tags', 'foreigntable' => 'cd',
265 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
e394339b 266 on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 267 },
268 ],
661fc8eb 269
270 # Bookmark
b1edf9f9 271 bookmark => [
272 {
273 'display' => 'bookmark->link',
bb0f01d0 274 'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
b1edf9f9 275 'selftable' => 'bookmark', 'foreigntable' => 'link',
276 'selfcols' => ['link'], 'foreigncols' => ['id'],
def17c59 277 on_delete => 'SET NULL', on_update => 'CASCADE', deferrable => 1,
b1edf9f9 278 },
279 ],
a0024650 280 # ForceForeign
281 forceforeign => [
282 {
283 'display' => 'forceforeign->artist',
bb0f01d0 284 'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
a0024650 285 'selftable' => 'forceforeign', 'foreigntable' => 'artist',
8871d4ad 286 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
827a808f 287 'noindex' => 1,
e394339b 288 on_delete => '', on_update => '', deferrable => 1,
a0024650 289 },
290 ],
b1edf9f9 291);
292
293my %unique_constraints = (
294 # CD
295 cd => [
296 {
297 'display' => 'cd artist and title unique',
0da8b7da 298 'name' => 'cd_artist_title',
b1edf9f9 299 'table' => 'cd', 'cols' => ['artist', 'title'],
300 },
301 ],
302
303 # Producer
304 producer => [
305 {
306 'display' => 'producer name unique',
0da8b7da 307 'name' => 'prod_name', # explicit name
b1edf9f9 308 'table' => 'producer', 'cols' => ['name'],
309 },
310 ],
311
312 # TwoKeyTreeLike
313 twokeytreelike => [
314 {
315 'display' => 'twokeytreelike name unique',
0da8b7da 316 'name' => 'tktlnameunique', # explicit name
b1edf9f9 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',
0da8b7da 326# 'name' => 'position_group',
b1edf9f9 327# 'table' => 'employee', cols => ['position', 'group_id'],
328# },
329# ],
7b90bb13 330);
331
17cab2f0 332my %indexes = (
c385ecea 333 artist => [
334 {
335 'fields' => ['name']
336 },
f89bb832 337 ],
338 track => [
339 {
340 'fields' => ['title']
341 }
342 ],
c385ecea 343);
344
637ca936 345my $tschema = $translator->schema();
d6c79cb3 346# Test that the $schema->sqlt_deploy_hook was called okay and that it removed
458e0292 347# the 'dummy' table
348ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
d6c79cb3 349
1f5bf324 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
b1edf9f9 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' );
a0024650 367$constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
368ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
b1edf9f9 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);
661fc8eb 380 }
637ca936 381}
382
b1edf9f9 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" );
0da8b7da 390 test_unique($expected_constraint, $constraint);
b1edf9f9 391 }
637ca936 392}
393
17cab2f0 394for my $table_index (keys %indexes) {
395 for my $expected_index ( @{ $indexes{$table_index} } ) {
c385ecea 396 ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
397 }
398}
399
b1edf9f9 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
a7e65bb5 416 die "No $table_name" unless $table;
b1edf9f9 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};
661fc8eb 432 }
b1edf9f9 433 }
661fc8eb 434
b1edf9f9 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};
661fc8eb 442 }
443 }
b1edf9f9 444
445 return $constraint; # everything passes, found the constraint
661fc8eb 446 }
b1edf9f9 447 return undef; # didn't find a matching constraint
7b90bb13 448}
449
c385ecea 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
b1edf9f9 478# Test parameters in a FOREIGN KEY constraint other than columns
479sub test_fk {
480 my ($expected, $got) = @_;
481 my $desc = $expected->{display};
0da8b7da 482 is( $got->name, $expected->{name},
827a808f 483 "name parameter correct for '$desc'" );
b1edf9f9 484 is( $got->on_delete, $expected->{on_delete},
827a808f 485 "on_delete parameter correct for '$desc'" );
b1edf9f9 486 is( $got->on_update, $expected->{on_update},
827a808f 487 "on_update parameter correct for '$desc'" );
13de943d 488 is( $got->deferrable, $expected->{deferrable},
827a808f 489 "is_deferrable parameter correct for '$desc'" );
0da8b7da 490
491 my $index = get_index( $got->table, { fields => $expected->{selfcols} } );
9c1f7965 492
493 if ($expected->{noindex}) {
827a808f 494 ok( !defined $index, "index doesn't for '$desc'" );
9c1f7965 495 } else {
827a808f 496 ok( defined $index, "index exists for '$desc'" );
497 is( $index->name, $expected->{index_name}, "index has correct name for '$desc'" );
9c1f7965 498 }
0da8b7da 499}
500
501sub test_unique {
502 my ($expected, $got) = @_;
503 my $desc = $expected->{display};
504 is( $got->name, $expected->{name},
827a808f 505 "name parameter correct for '$desc'" );
637ca936 506}
0fd7e9a3 507
508done_testing;