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();
28 ok($output, "SQLT produced someoutput")
29 or diag($translator->error);
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
37 my %fk_constraints = (
42 'display' => 'twokeys->cd',
43 'selftable' => 'twokeys', 'foreigntable' => 'cd',
44 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
45 on_delete => '', on_update => '',
48 'display' => 'twokeys->artist',
49 'selftable' => 'twokeys', 'foreigntable' => 'artist',
50 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
51 on_delete => 'CASCADE', on_update => 'CASCADE',
56 fourkeys_to_twokeys => [
58 'display' => 'fourkeys_to_twokeys->twokeys',
59 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'twokeys',
60 'selfcols' => ['t_artist', 't_cd'], 'foreigncols' => ['artist', 'cd'],
61 on_delete => 'CASCADE', on_update => 'CASCADE',
64 'display' => 'fourkeys_to_twokeys->fourkeys',
65 'selftable' => 'fourkeys_to_twokeys', 'foreigntable' => 'fourkeys',
66 'selfcols' => [qw(f_foo f_bar f_hello f_goodbye)],
67 'foreigncols' => [qw(foo bar hello goodbye)],
68 on_delete => 'CASCADE', on_update => 'CASCADE',
75 'display' => 'cd_to_producer->cd',
76 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
77 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
78 on_delete => 'CASCADE', on_update => 'CASCADE',
81 'display' => 'cd_to_producer->producer',
82 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
83 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
84 on_delete => '', on_update => '',
91 'display' => 'self_ref_alias->self_ref for self_ref',
92 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
93 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
94 on_delete => 'CASCADE', on_update => 'CASCADE',
97 'display' => 'self_ref_alias->self_ref for alias',
98 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
99 'selfcols' => ['alias'], 'foreigncols' => ['id'],
100 on_delete => '', on_update => '',
107 'display' => 'cd->artist',
108 'selftable' => 'cd', 'foreigntable' => 'artist',
109 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
110 on_delete => 'CASCADE', on_update => 'CASCADE',
114 # Artist_undirected_map
115 artist_undirected_map => [
117 'display' => 'artist_undirected_map->artist for id1',
118 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
119 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
120 on_delete => 'CASCADE', on_update => '',
123 'display' => 'artist_undirected_map->artist for id2',
124 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
125 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
126 on_delete => 'CASCADE', on_update => '',
133 'display' => 'track->cd',
134 'selftable' => 'track', 'foreigntable' => 'cd',
135 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
136 on_delete => 'CASCADE', on_update => 'CASCADE',
143 'display' => 'treelike->treelike for parent',
144 'selftable' => 'treelike', 'foreigntable' => 'treelike',
145 'selfcols' => ['parent'], 'foreigncols' => ['id'],
146 on_delete => 'CASCADE', on_update => 'CASCADE',
153 'display' => 'twokeytreelike->twokeytreelike for parent1,parent2',
154 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
155 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
156 on_delete => '', on_update => '',
163 'display' => 'tags->cd',
164 'selftable' => 'tags', 'foreigntable' => 'cd',
165 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
166 on_delete => 'CASCADE', on_update => 'CASCADE',
173 'display' => 'bookmark->link',
174 'selftable' => 'bookmark', 'foreigntable' => 'link',
175 'selfcols' => ['link'], 'foreigncols' => ['id'],
176 on_delete => '', on_update => '',
181 my %unique_constraints = (
185 'display' => 'cd artist and title unique',
186 'table' => 'cd', 'cols' => ['artist', 'title'],
193 'display' => 'producer name unique',
194 'table' => 'producer', 'cols' => ['name'],
201 'display' => 'twokeytreelike name unique',
202 'table' => 'twokeytreelike', 'cols' => ['name'],
207 # Constraint is commented out in DBICTest/Schema/Employee.pm
210 # 'display' => 'employee position and group_id unique',
211 # 'table' => 'employee', cols => ['position', 'group_id'],
216 my $tschema = $translator->schema();
218 # Test that nonexistent constraints are not found
219 my $constraint = get_constraint('FOREIGN KEY', 'cd', ['title'], 'cd', ['year']);
220 ok( !defined($constraint), 'nonexistent FOREIGN KEY constraint not found' );
221 $constraint = get_constraint('UNIQUE', 'cd', ['artist']);
222 ok( !defined($constraint), 'nonexistent UNIQUE constraint not found' );
224 for my $expected_constraints (keys %fk_constraints) {
225 for my $expected_constraint (@{ $fk_constraints{$expected_constraints} }) {
226 my $desc = $expected_constraint->{display};
227 my $constraint = get_constraint(
229 $expected_constraint->{selftable}, $expected_constraint->{selfcols},
230 $expected_constraint->{foreigntable}, $expected_constraint->{foreigncols},
232 ok( defined($constraint), "FOREIGN KEY constraint matching `$desc' found" );
233 test_fk($expected_constraint, $constraint);
237 for my $expected_constraints (keys %unique_constraints) {
238 for my $expected_constraint (@{ $unique_constraints{$expected_constraints} }) {
239 my $desc = $expected_constraint->{display};
240 my $constraint = get_constraint(
241 'UNIQUE', $expected_constraint->{table}, $expected_constraint->{cols},
243 ok( defined($constraint), "UNIQUE constraint matching `$desc' found" );
247 # Returns the Constraint object for the specified constraint type, table and
248 # columns from the SQL::Translator schema, or undef if no matching constraint
251 # NB: $type is either 'FOREIGN KEY' or 'UNIQUE'. In UNIQUE constraints the last
252 # two parameters are not used.
254 my ($type, $table_name, $cols, $f_table, $f_cols) = @_;
255 $f_table ||= ''; # For UNIQUE constraints, reference_table is ''
258 my $table = $tschema->get_table($table_name);
260 my %fields = map { $_ => 1 } @$cols;
261 my %f_fields = map { $_ => 1 } @$f_cols;
264 for my $constraint ( $table->get_constraints ) {
265 next unless $constraint->type eq $type;
266 next unless $constraint->reference_table eq $f_table;
268 my %rev_fields = map { $_ => 1 } $constraint->fields;
269 my %rev_f_fields = map { $_ => 1 } $constraint->reference_fields;
271 # Check that the given fields are a subset of the constraint's fields
272 for my $field ($constraint->fields) {
273 next CONSTRAINT unless $fields{$field};
275 if ($type eq 'FOREIGN KEY') {
276 for my $f_field ($constraint->reference_fields) {
277 next CONSTRAINT unless $f_fields{$f_field};
281 # Check that the constraint's fields are a subset of the given fields
282 for my $field (@$cols) {
283 next CONSTRAINT unless $rev_fields{$field};
285 if ($type eq 'FOREIGN KEY') {
286 for my $f_field (@$f_cols) {
287 next CONSTRAINT unless $rev_f_fields{$f_field};
291 return $constraint; # everything passes, found the constraint
293 return undef; # didn't find a matching constraint
296 # Test parameters in a FOREIGN KEY constraint other than columns
298 my ($expected, $got) = @_;
299 my $desc = $expected->{display};
300 is( $got->on_delete, $expected->{on_delete},
301 "on_delete parameter correct for `$desc'" );
302 is( $got->on_update, $expected->{on_update},
303 "on_update parameter correct for `$desc'" );