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