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