Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
637ca936 |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
637ca936 |
7 | |
8 | eval "use SQL::Translator"; |
9 | plan skip_all => 'SQL::Translator required' if $@; |
10 | |
8e6df6ae |
11 | # do not taunt happy dave ball |
12 | |
58d387fe |
13 | my $schema = 'DBICTest::Schema'; |
637ca936 |
14 | |
ae515736 |
15 | plan tests => 31; |
637ca936 |
16 | |
17 | my $translator = SQL::Translator->new( |
18 | parser_args => { |
19 | 'DBIx::Schema' => $schema, |
20 | }, |
21 | producer_args => { |
22 | }, |
23 | ); |
24 | |
25 | $translator->parser('SQL::Translator::Parser::DBIx::Class'); |
26 | $translator->producer('SQLite'); |
27 | |
28 | my $output = $translator->translate(); |
29 | |
7b90bb13 |
30 | my @fk_constraints = |
637ca936 |
31 | ( |
32 | {'display' => 'twokeys->cd', |
33 | 'selftable' => 'twokeys', 'foreigntable' => 'cd', |
34 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
35 | 'needed' => 1, on_delete => '', on_update => ''}, |
36 | {'display' => 'twokeys->artist', |
37 | 'selftable' => 'twokeys', 'foreigntable' => 'artist', |
38 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
de60a93d |
39 | 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'}, |
637ca936 |
40 | {'display' => 'cd_to_producer->cd', |
41 | 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', |
42 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
de60a93d |
43 | 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'}, |
637ca936 |
44 | {'display' => 'cd_to_producer->producer', |
45 | 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer', |
46 | 'selfcols' => ['producer'], 'foreigncols' => ['producerid'], |
47 | 'needed' => 1, on_delete => '', on_update => ''}, |
48 | {'display' => 'self_ref_alias -> self_ref for self_ref', |
49 | 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', |
50 | 'selfcols' => ['self_ref'], 'foreigncols' => ['id'], |
de60a93d |
51 | 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'}, |
637ca936 |
52 | {'display' => 'self_ref_alias -> self_ref for alias', |
53 | 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref', |
54 | 'selfcols' => ['alias'], 'foreigncols' => ['id'], |
55 | 'needed' => 1, on_delete => '', on_update => ''}, |
56 | {'display' => 'cd -> artist', |
57 | 'selftable' => 'cd', 'foreigntable' => 'artist', |
58 | 'selfcols' => ['artist'], 'foreigncols' => ['artistid'], |
de60a93d |
59 | 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'}, |
637ca936 |
60 | {'display' => 'artist_undirected_map -> artist for id1', |
61 | 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', |
62 | 'selfcols' => ['id1'], 'foreigncols' => ['artistid'], |
de60a93d |
63 | 'needed' => 1, on_delete => 'CASCADE', on_update => ''}, |
637ca936 |
64 | {'display' => 'artist_undirected_map -> artist for id2', |
65 | 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', |
66 | 'selfcols' => ['id2'], 'foreigncols' => ['artistid'], |
de60a93d |
67 | 'needed' => 1, on_delete => 'CASCADE', on_update => ''}, |
637ca936 |
68 | {'display' => 'track->cd', |
69 | 'selftable' => 'track', 'foreigntable' => 'cd', |
70 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
de60a93d |
71 | 'needed' => 2, on_delete => 'CASCADE', on_update => 'CASCADE'}, |
637ca936 |
72 | {'display' => 'treelike -> treelike for parent', |
73 | 'selftable' => 'treelike', 'foreigntable' => 'treelike', |
74 | 'selfcols' => ['parent'], 'foreigncols' => ['id'], |
75 | 'needed' => 1, on_delete => '', on_update => ''}, |
ae515736 |
76 | |
77 | # shouldn't this be generated? |
78 | # |
79 | #{'display' => 'twokeytreelike -> twokeytreelike for parent1,parent2', |
80 | # 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike', |
81 | # 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'], |
82 | # 'needed' => 1, on_delete => '', on_update => ''}, |
83 | |
637ca936 |
84 | {'display' => 'tags -> cd', |
85 | 'selftable' => 'tags', 'foreigntable' => 'cd', |
86 | 'selfcols' => ['cd'], 'foreigncols' => ['cdid'], |
de60a93d |
87 | 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'}, |
81155549 |
88 | {'display' => 'bookmark -> link', |
89 | 'selftable' => 'bookmark', 'foreigntable' => 'link', |
90 | 'selfcols' => ['link'], 'foreigncols' => ['id'], |
91 | 'needed' => 1, on_delete => '', on_update => ''}, |
637ca936 |
92 | ); |
93 | |
7b90bb13 |
94 | my @unique_constraints = ( |
95 | {'display' => 'cd artist and title unique', |
96 | 'table' => 'cd', 'cols' => ['artist', 'title'], |
97 | 'needed' => 1}, |
98 | {'display' => 'twokeytreelike name unique', |
99 | 'table' => 'twokeytreelike', 'cols' => ['name'], |
100 | 'needed' => 1}, |
78145e31 |
101 | # {'display' => 'employee position and group_id unique', |
102 | # 'table' => 'employee', cols => ['position', 'group_id'], |
103 | # 'needed' => 1}, |
7b90bb13 |
104 | ); |
105 | |
637ca936 |
106 | my $tschema = $translator->schema(); |
107 | for my $table ($tschema->get_tables) { |
108 | my $table_name = $table->name; |
109 | for my $c ( $table->get_constraints ) { |
7b90bb13 |
110 | if ($c->type eq 'FOREIGN KEY') { |
111 | ok(check_fk($table_name, scalar $c->fields, |
112 | $c->reference_table, scalar $c->reference_fields, |
113 | $c->on_delete, $c->on_update), "Foreign key constraint on $table_name matches an expected constraint"); |
114 | } |
115 | elsif ($c->type eq 'UNIQUE') { |
116 | ok(check_unique($table_name, scalar $c->fields), |
117 | "Unique constraint on $table_name matches an expected constraint"); |
118 | } |
637ca936 |
119 | } |
120 | } |
121 | |
7b90bb13 |
122 | # Make sure all the foreign keys are done. |
637ca936 |
123 | my $i; |
7b90bb13 |
124 | for ($i = 0; $i <= $#fk_constraints; ++$i) { |
125 | ok(!$fk_constraints[$i]->{'needed'}, "Constraint $fk_constraints[$i]->{display}"); |
126 | } |
127 | # Make sure all the uniques are done. |
128 | for ($i = 0; $i <= $#unique_constraints; ++$i) { |
129 | ok(!$unique_constraints[$i]->{'needed'}, "Constraint $unique_constraints[$i]->{display}"); |
637ca936 |
130 | } |
131 | |
7b90bb13 |
132 | sub check_fk { |
637ca936 |
133 | my ($selftable, $selfcol, $foreigntable, $foreigncol, $ondel, $onupd) = @_; |
134 | |
135 | $ondel = '' if (!defined($ondel)); |
136 | $onupd = '' if (!defined($onupd)); |
137 | |
138 | my $i; |
7b90bb13 |
139 | for ($i = 0; $i <= $#fk_constraints; ++$i) { |
140 | if ($selftable eq $fk_constraints[$i]->{'selftable'} && |
141 | $foreigntable eq $fk_constraints[$i]->{'foreigntable'} && |
142 | ($ondel eq $fk_constraints[$i]->{on_delete}) && |
143 | ($onupd eq $fk_constraints[$i]->{on_update})) { |
637ca936 |
144 | # check columns |
145 | |
146 | my $found = 0; |
147 | for (my $j = 0; $j <= $#$selfcol; ++$j) { |
148 | $found = 0; |
7b90bb13 |
149 | for (my $k = 0; $k <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$k) { |
150 | if ($selfcol->[$j] eq $fk_constraints[$i]->{'selfcols'}->[$k] && |
151 | $foreigncol->[$j] eq $fk_constraints[$i]->{'foreigncols'}->[$k]) { |
152 | $found = 1; |
153 | last; |
154 | } |
155 | } |
156 | last unless $found; |
157 | } |
158 | |
159 | if ($found) { |
160 | for (my $j = 0; $j <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$j) { |
161 | $found = 0; |
162 | for (my $k = 0; $k <= $#$selfcol; ++$k) { |
163 | if ($selfcol->[$k] eq $fk_constraints[$i]->{'selfcols'}->[$j] && |
164 | $foreigncol->[$k] eq $fk_constraints[$i]->{'foreigncols'}->[$j]) { |
165 | $found = 1; |
166 | last; |
167 | } |
168 | } |
169 | last unless $found; |
170 | } |
171 | } |
172 | |
173 | if ($found) { |
174 | --$fk_constraints[$i]->{needed}; |
175 | return 1; |
176 | } |
177 | } |
178 | } |
179 | return 0; |
180 | } |
181 | |
58d387fe |
182 | my( $ondel, $onupd ); |
183 | |
7b90bb13 |
184 | sub check_unique { |
185 | my ($selftable, $selfcol) = @_; |
186 | |
187 | $ondel = '' if (!defined($ondel)); |
188 | $onupd = '' if (!defined($onupd)); |
189 | |
190 | my $i; |
191 | for ($i = 0; $i <= $#unique_constraints; ++$i) { |
192 | if ($selftable eq $unique_constraints[$i]->{'table'}) { |
193 | |
194 | my $found = 0; |
195 | for (my $j = 0; $j <= $#$selfcol; ++$j) { |
196 | $found = 0; |
197 | for (my $k = 0; $k <= $#{$unique_constraints[$i]->{'cols'}}; ++$k) { |
198 | if ($selfcol->[$j] eq $unique_constraints[$i]->{'cols'}->[$k]) { |
637ca936 |
199 | $found = 1; |
200 | last; |
201 | } |
202 | } |
203 | last unless $found; |
204 | } |
205 | |
206 | if ($found) { |
7b90bb13 |
207 | for (my $j = 0; $j <= $#{$unique_constraints[$i]->{'cols'}}; ++$j) { |
637ca936 |
208 | $found = 0; |
209 | for (my $k = 0; $k <= $#$selfcol; ++$k) { |
7b90bb13 |
210 | if ($selfcol->[$k] eq $unique_constraints[$i]->{'cols'}->[$j]) { |
637ca936 |
211 | $found = 1; |
212 | last; |
213 | } |
214 | } |
215 | last unless $found; |
216 | } |
217 | } |
218 | |
219 | if ($found) { |
7b90bb13 |
220 | --$unique_constraints[$i]->{needed}; |
637ca936 |
221 | return 1; |
222 | } |
223 | } |
224 | } |
225 | return 0; |
226 | } |