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