8 eval "use SQL::Translator";
9 plan skip_all => 'SQL::Translator required' if $@;
11 my $schema = DBICTest->init_schema;
15 my $translator = SQL::Translator->new(
17 'DBIx::Schema' => $schema,
22 $translator->parser('SQL::Translator::Parser::DBIx::Class');
23 $translator->producer('SQLite');
25 my $output = $translator->translate();
27 # Note that the constraints listed here are the only ones that are tested -- if
28 # more exist in the Schema than are listed here and all listed constraints are
29 # correct, the test will still pass. If you add a class to DBICTest::Schema,
30 # add tests here if you think the existing test coverage is not sufficient
32 my %fk_constraints = (
37 'display' => 'twokeys->cd',
38 'selftable' => 'twokeys', 'foreigntable' => 'cd',
39 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
40 on_delete => '', on_update => '',
43 'display' => 'twokeys->artist',
44 'selftable' => 'twokeys', 'foreigntable' => 'artist',
45 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
46 on_delete => 'CASCADE', on_update => 'CASCADE',
51 fourkeys_to_twokeys => [
53 'display' => 'fourkeys_to_twokeys->twokeys',
54 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
55 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
56 on_delete => 'CASCADE', on_update => 'CASCADE',
59 'display' => 'fourkeys_to_twokeys->fourkeys',
60 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
61 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
62 'foreigncols' => [qw(foo bar hello goodbye)],
63 on_delete => 'CASCADE', on_update => 'CASCADE',
70 'display' => 'cd_to_producer->cd',
71 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
72 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
73 on_delete => 'CASCADE', on_update => 'CASCADE',
76 'display' => 'cd_to_producer->producer',
77 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
78 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
79 on_delete => '', on_update => '',
86 'display' => 'self_ref_alias->self_ref for self_ref',
87 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
88 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
89 on_delete => 'CASCADE', on_update => 'CASCADE',
92 'display' => 'self_ref_alias->self_ref for alias',
93 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
94 'selfcols' => ['alias'], 'foreigncols' => ['id'],
95 on_delete => '', on_update => '',
102 'display' => 'cd->artist',
103 'selftable' => 'cd', 'foreigntable' => 'artist',
104 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
105 on_delete => 'CASCADE', on_update => 'CASCADE',
109 # Artist_undirected_map
110 artist_undirected_map => [
112 'display' => 'artist_undirected_map->artist for id1',
113 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
114 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
115 on_delete => 'CASCADE', on_update => '',
118 'display' => 'artist_undirected_map->artist for id2',
119 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
120 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
121 on_delete => 'CASCADE', on_update => '',
128 'display' => 'track->cd',
129 'selftable' => 'track', 'foreigntable' => 'cd',
130 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
131 on_delete => 'CASCADE', on_update => 'CASCADE',
138 'display' => 'treelike->treelike for parent',
139 'selftable' => 'treelike', 'foreigntable' => 'treelike',
140 'selfcols' => ['parent'], 'foreigncols' => ['id'],
141 on_delete => '', on_update => '',
148 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
149 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
150 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
151 on_delete => '', on_update => '',
158 'display' => 'tags->cd',
159 'selftable' => 'tags', 'foreigntable' => 'cd',
160 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
161 on_delete => 'CASCADE', on_update => 'CASCADE',
168 'display' => 'bookmark->link',
169 'selftable' => 'bookmark', 'foreigntable' => 'link',
170 'selfcols' => ['link'], 'foreigncols' => ['id'],
171 on_delete => '', on_update => '',
176 my %unique_constraints = (
180 'display' => 'cd artist and title unique',
181 'table' => 'cd', 'cols' => ['artist', 'title'],
188 'display' => 'producer name unique',
189 'table' => 'producer', 'cols' => ['name'],
196 'display' => 'twokeytreelike name unique',
197 'table' => 'twokeytreelike', 'cols' => ['name'],
202 # Constraint is commented out in DBICTest/Schema/Employee.pm
205 # 'display' => 'employee position and group_id unique',
206 # 'table' => 'employee', cols => ['position', 'group_id'],
211 my $tschema = $translator->schema();
213 # Test that nonexistent constraints are not found
214 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
215 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
216 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
217 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
219 for my $expected_constraints (keys %fk_constraints) {
220 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
221 my $desc = $expected_constraint->{display};
222 my $constraint = get_constraint(
224 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
225 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
227 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
228 test_fk($expected_constraint, $constraint);
232 for my $expected_constraints (keys %unique_constraints) {
233 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
234 my $desc = $expected_constraint->{display};
235 my $constraint = get_constraint(
236 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
238 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
242 # Returns the Constraint object for the specified constraint type, table and
243 # columns from the SQL::Translator schema, or undef if no matching constraint
246 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
247 # two parameters are not used.
249 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
250 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
253 my $table = $tschema->get_table($table_name);
255 my %fields = map { $_ => 1 } @$cols;
256 my %f_fields = map { $_ => 1 } @$f_cols;
259 for my $constraint ( $table->get_constraints ) {
260 next unless $constraint->type eq $type;
261 next unless $constraint->reference_table eq $f_table;
263 my %rev_fields = map { $_ => 1 } $constraint->fields;
264 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
266 # Check that the given fields are a subset of the constraint's fields
267 for my $field ($constraint->fields) {
268 next CONSTRAINT unless $fields{$field};
270 if ($type eq 'FOREIGN KEY') {
271 for my $f_field ($constraint->reference_fields) {
272 next CONSTRAINT unless $f_fields{$f_field};
276 # Check that the constraint's fields are a subset of the given fields
277 for my $field (@$cols) {
278 next CONSTRAINT unless $rev_fields{$field};
280 if ($type eq 'FOREIGN KEY') {
281 for my $f_field (@$f_cols) {
282 next CONSTRAINT unless $rev_f_fields{$f_field};
286 return $constraint; # everything passes, found the constraint
288 return undef; # didn't find a matching constraint
291 # Test parameters in a FOREIGN KEY constraint other than columns
293 my ($expected, $got) = @_;
294 my $desc = $expected->{display};
295 is( $got->on_delete, $expected->{on_delete},
296 "on_delete parameter correct for `$desc'" );
297 is( $got->on_update, $expected->{on_update},
298 "on_update parameter correct for `$desc'" );