Remove the length limit on identifiers - it doesn't belong in DBIx::Class
[dbsrgits/DBIx-Class.git] / t / 86sqlt.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 eval "use SQL::Translator";
9 plan skip_all => 'SQL::Translator required' if $@;
10
11 my $schema = DBICTest->init_schema;
12
13 plan tests => 131;
14
15 my $translator = SQL::Translator->new( 
16   parser_args => {
17     'DBIx::Schema' => $schema,
18   },
19   producer_args => {},
20 );
21
22 $translator->parser('SQL::Translator::Parser::DBIx::Class');
23 $translator->producer('SQLite');
24
25 my $output = $translator->translate();
26
27
28 ok($output, "SQLT produced someoutput")
29   or diag($translator->error);
30
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
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
36
37 my %fk_constraints = (
38
39   # TwoKeys
40   twokeys => [
41     {
42       'display' => 'twokeys->cd',
43       'name' => 'twokeys_fk_cd', 'index_name' => 'twokeys_idx_cd',
44       'selftable' => 'twokeys', 'foreigntable' => 'cd', 
45       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'], 
46       on_delete => '', on_update => '', deferrable => 0,
47     },
48     {
49       'display' => 'twokeys->artist',
50       'name' => 'twokeys_fk_artist', 'index_name' => 'twokeys_idx_artist',
51       'selftable' => 'twokeys', 'foreigntable' => 'artist', 
52       'selfcols'  => ['artist'], 'foreigncols' => ['artistid'],
53       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
54     },
55   ],
56
57   # FourKeys_to_TwoKeys
58   fourkeys_to_twokeys => [
59     {
60       'display' => 'fourkeys_to_twokeys->twokeys',
61       'name' => 'fourkeys_to_twokeys_fk_t_cd_t_artist', 'index_name' => 'fourkeys_to_twokeys_idx_t_cd_t_artist',
62       'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys', 
63       'selfcols'  => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'], 
64       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
65     },
66     {
67       'display' => 'fourkeys_to_twokeys->fourkeys', 'index_name' => 'fourkeys_to_twokeys_idx_f_foo_f_goodbye_f_hello_f_bar',
68       'name' => 'fourkeys_to_twokeys_fk_f_foo_f_goodbye_f_hello_f_bar',
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)], 
72       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
73     },
74   ],
75
76   # CD_to_Producer
77   cd_to_producer => [
78     {
79       'display' => 'cd_to_producer->cd',
80       'name' => 'cd_to_producer_fk_cd', 'index_name' => 'cd_to_producer_idx_cd',
81       'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', 
82       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
83       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
84     },
85     {
86       'display' => 'cd_to_producer->producer',
87       'name' => 'cd_to_producer_fk_producer', 'index_name' => 'cd_to_producer_idx_producer',
88       'selftable' => 'cd_to_producer', 'foreigntable' => 'producer', 
89       'selfcols'  => ['producer'], 'foreigncols' => ['producerid'],
90       on_delete => '', on_update => '', deferrable => 1,
91     },
92   ],
93
94   # Self_ref_alias
95   self_ref_alias => [
96     {
97       'display' => 'self_ref_alias->self_ref for self_ref',
98       'name' => 'self_ref_alias_fk_self_ref', 'index_name' => 'self_ref_alias_idx_self_ref',
99       'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 
100       'selfcols'  => ['self_ref'], 'foreigncols' => ['id'],
101       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
102     },
103     {
104       'display' => 'self_ref_alias->self_ref for alias',
105       'name' => 'self_ref_alias_fk_alias', 'index_name' => 'self_ref_alias_idx_alias',
106       'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', 
107       'selfcols'  => ['alias'], 'foreigncols' => ['id'],
108       on_delete => '', on_update => '', deferrable => 1,
109     },
110   ],
111
112   # CD
113   cd => [
114     {
115       'display' => 'cd->artist',
116       'name' => 'cd_fk_artist', 'index_name' => 'cd_idx_artist',
117       'selftable' => 'cd', 'foreigntable' => 'artist', 
118       'selfcols'  => ['artist'], 'foreigncols' => ['artistid'],
119       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
120     },
121   ],
122
123   # Artist_undirected_map
124   artist_undirected_map => [
125     {
126       'display' => 'artist_undirected_map->artist for id1',
127       'name' => 'artist_undirected_map_fk_id1', 'index_name' => 'artist_undirected_map_idx_id1',
128       'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
129       'selfcols'  => ['id1'], 'foreigncols' => ['artistid'],
130       on_delete => 'CASCADE', on_update => '', deferrable => 1,
131     },
132     {
133       'display' => 'artist_undirected_map->artist for id2',
134       'name' => 'artist_undirected_map_fk_id2', 'index_name' => 'artist_undirected_map_idx_id2',
135       'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
136       'selfcols'  => ['id2'], 'foreigncols' => ['artistid'],
137       on_delete => 'CASCADE', on_update => '', deferrable => 1,
138     },
139   ],
140
141   # Track
142   track => [
143     {
144       'display' => 'track->cd',
145       'name' => 'track_fk_cd', 'index_name' => 'track_idx_cd',
146       'selftable' => 'track', 'foreigntable' => 'cd', 
147       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
148       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
149     },
150   ],
151
152   # TreeLike
153   treelike => [
154     {
155       'display' => 'treelike->treelike for parent',
156       'name' => 'treelike_fk_parent', 'index_name' => 'treelike_idx_parent',
157       'selftable' => 'treelike', 'foreigntable' => 'treelike', 
158       'selfcols'  => ['parent'], 'foreigncols' => ['id'],
159       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
160     },
161   ],
162
163   # TwoKeyTreeLike
164   twokeytreelike => [
165     {
166       'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
167       'name' => 'twokeytreelike_fk_parent1_parent2', 'index_name' => 'twokeytreelike_idx_parent1_parent2',
168       'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike', 
169       'selfcols'  => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
170       on_delete => '', on_update => '', deferrable => 1,
171     },
172   ],
173
174   # Tags
175   tags => [
176     {
177       'display' => 'tags->cd',
178       'name' => 'tags_fk_cd', 'index_name' => 'tags_idx_cd',
179       'selftable' => 'tags', 'foreigntable' => 'cd', 
180       'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
181       on_delete => 'CASCADE', on_update => 'CASCADE', deferrable => 1,
182     },
183   ],
184
185   # Bookmark
186   bookmark => [
187     {
188       'display' => 'bookmark->link',
189       'name' => 'bookmark_fk_link', 'index_name' => 'bookmark_idx_link',
190       'selftable' => 'bookmark', 'foreigntable' => 'link', 
191       'selfcols'  => ['link'], 'foreigncols' => ['id'],
192       on_delete => '', on_update => '', deferrable => 1,
193     },
194   ],
195   # ForceForeign
196   forceforeign => [
197     {
198       'display' => 'forceforeign->artist',
199       'name' => 'forceforeign_fk_artist', 'index_name' => 'forceforeign_idx_artist',
200       'selftable' => 'forceforeign', 'foreigntable' => 'artist', 
201       'selfcols'  => ['artist'], 'foreigncols' => ['artist_id'], 
202       on_delete => '', on_update => '', deferrable => 1,
203     },
204   ],
205
206 );
207
208 my %unique_constraints = (
209   # CD
210   cd => [
211     {
212       'display' => 'cd artist and title unique',
213       'name' => 'cd_artist_title',
214       'table' => 'cd', 'cols' => ['artist', 'title'],
215     },
216   ],
217
218   # Producer
219   producer => [
220     {
221       'display' => 'producer name unique',
222       'name' => 'prod_name', # explicit name
223       'table' => 'producer', 'cols' => ['name'],
224     },
225   ],
226
227   # TwoKeyTreeLike
228   twokeytreelike => [
229     {
230       'display' => 'twokeytreelike name unique',
231       'name' => 'tktlnameunique', # explicit name
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',
241 #      'name' => 'position_group',
242 #      'table' => 'employee', cols => ['position', 'group_id'],
243 #    },
244 #  ],
245 );
246
247 my %indexes = (
248   artist => [
249     {
250       'fields' => ['name']
251     },
252   ]
253 );
254
255 my $tschema = $translator->schema();
256 # Test that the $schema->sqlt_deploy_hook was called okay and that it removed
257 # the 'dummy' table
258 ok( !defined($tschema->get_table('dummy')), "Dummy table was removed by hook");
259
260 # Test that nonexistent constraints are not found
261 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
262 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
263 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
264 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
265 $constraint = get_constraint('FOREIGN KEY', 'forceforeign', ['cd'], 'cd', ['cdid']);
266 ok( !defined($constraint), 'forced nonexistent FOREIGN KEY constraint not found' );
267
268 for 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);
278   }
279 }
280
281 for 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" );
288     test_unique($expected_constraint, $constraint);
289   }
290 }
291
292 for my $table_index (keys %indexes) {
293   for my $expected_index ( @{ $indexes{$table_index} } ) {
294
295     ok ( get_index($table_index, $expected_index), "Got a matching index on $table_index table");
296   }
297 }
298
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.
305 sub 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
315   die "No $table_name" unless $table;
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};
331       }
332     }
333
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};
341       }
342     }
343
344     return $constraint; # everything passes, found the constraint
345   }
346   return undef; # didn't find a matching constraint
347 }
348
349 sub 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
377 # Test parameters in a FOREIGN KEY constraint other than columns
378 sub test_fk {
379   my ($expected, $got) = @_;
380   my $desc = $expected->{display};
381   is( $got->name, $expected->{name},
382       "name parameter correct for `$desc'" );
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'" );
387   is( $got->deferrable, $expected->{deferrable},
388       "is_deferrable parameter correct for `$desc'" );
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
395 sub test_unique {
396   my ($expected, $got) = @_;
397   my $desc = $expected->{display};
398   is( $got->name, $expected->{name},
399       "name parameter correct for `$desc'" );
400 }