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