Add automatic naming of unique constraints
[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 # do not taunt happy dave ball
12
13 my $schema = 'DBICTest::Schema';
14
15 plan tests => 33;
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
30 my @fk_constraints = 
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'],
39    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
40   {'display' => 'cd_to_producer->cd',
41    'selftable' => 'cd_to_producer', 'foreigntable' => 'cd', 
42    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
43    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
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'],
51    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
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'],
59    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
60   {'display' => 'artist_undirected_map -> artist for id1',
61    'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
62    'selfcols'  => ['id1'], 'foreigncols' => ['artistid'],
63    'needed' => 1, on_delete => 'CASCADE', on_update => ''},
64   {'display' => 'artist_undirected_map -> artist for id2',
65    'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist', 
66    'selfcols'  => ['id2'], 'foreigncols' => ['artistid'],
67    'needed' => 1, on_delete => 'CASCADE', on_update => ''},
68   {'display' => 'track->cd',
69    'selftable' => 'track', 'foreigntable' => 'cd', 
70    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
71    'needed' => 2, on_delete => 'CASCADE', on_update => 'CASCADE'},
72   {'display' => 'treelike -> treelike for parent',
73    'selftable' => 'treelike', 'foreigntable' => 'treelike', 
74    'selfcols'  => ['parent'], 'foreigncols' => ['id'],
75    'needed' => 1, on_delete => '', on_update => ''},
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
84   {'display' => 'tags -> cd',
85    'selftable' => 'tags', 'foreigntable' => 'cd', 
86    'selfcols'  => ['cd'], 'foreigncols' => ['cdid'],
87    'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
88   {'display' => 'bookmark -> link',
89    'selftable' => 'bookmark', 'foreigntable' => 'link', 
90    'selfcols'  => ['link'], 'foreigncols' => ['id'],
91    'needed' => 1, on_delete => '', on_update => ''},
92  );
93
94 my @unique_constraints = (
95   {'display' => 'cd artist and title unique',
96    'table' => 'cd', 'cols' => ['artist', 'title'],
97    'needed' => 1},
98   {'display' => 'producer name unique',
99    'table' => 'producer', 'cols' => ['name'],
100    'needed' => 1},
101   {'display' => 'twokeytreelike name unique',
102    'table' => 'twokeytreelike', 'cols'  => ['name'],
103    'needed' => 1},
104 #  {'display' => 'employee position and group_id unique',
105 #   'table' => 'employee', cols => ['position', 'group_id'],
106 #   'needed' => 1},
107 );
108
109 my $tschema = $translator->schema();
110 for my $table ($tschema->get_tables) {
111     my $table_name = $table->name;
112     for my $c ( $table->get_constraints ) {
113         if ($c->type eq 'FOREIGN KEY') {
114             ok(check_fk($table_name, scalar $c->fields, 
115                   $c->reference_table, scalar $c->reference_fields, 
116                   $c->on_delete, $c->on_update), "Foreign key constraint on $table_name matches an expected constraint");
117         }
118         elsif ($c->type eq 'UNIQUE') {
119             ok(check_unique($table_name, scalar $c->fields),
120                   "Unique constraint on $table_name matches an expected constraint");
121         }
122     }
123 }
124
125 # Make sure all the foreign keys are done.
126 my $i;
127 for ($i = 0; $i <= $#fk_constraints; ++$i) {
128  ok(!$fk_constraints[$i]->{'needed'}, "Constraint $fk_constraints[$i]->{display}");
129 }
130 # Make sure all the uniques are done.
131 for ($i = 0; $i <= $#unique_constraints; ++$i) {
132  ok(!$unique_constraints[$i]->{'needed'}, "Constraint $unique_constraints[$i]->{display}");
133 }
134
135 sub check_fk {
136  my ($selftable, $selfcol, $foreigntable, $foreigncol, $ondel, $onupd) = @_;
137
138  $ondel = '' if (!defined($ondel));
139  $onupd = '' if (!defined($onupd));
140
141  my $i;
142  for ($i = 0; $i <= $#fk_constraints; ++$i) {
143      if ($selftable eq $fk_constraints[$i]->{'selftable'} &&
144          $foreigntable eq $fk_constraints[$i]->{'foreigntable'} &&
145          ($ondel eq $fk_constraints[$i]->{on_delete}) &&
146          ($onupd eq $fk_constraints[$i]->{on_update})) {
147          # check columns
148
149          my $found = 0;
150          for (my $j = 0; $j <= $#$selfcol; ++$j) {
151              $found = 0;
152              for (my $k = 0; $k <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$k) {
153                  if ($selfcol->[$j] eq $fk_constraints[$i]->{'selfcols'}->[$k] &&
154                      $foreigncol->[$j] eq $fk_constraints[$i]->{'foreigncols'}->[$k]) {
155                      $found = 1;
156                      last;
157                  }
158              }
159              last unless $found;
160          }
161
162          if ($found) {
163              for (my $j = 0; $j <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$j) {
164                  $found = 0;
165                  for (my $k = 0; $k <= $#$selfcol; ++$k) {
166                      if ($selfcol->[$k] eq $fk_constraints[$i]->{'selfcols'}->[$j] &&
167                          $foreigncol->[$k] eq $fk_constraints[$i]->{'foreigncols'}->[$j]) {
168                          $found = 1;
169                          last;
170                      }
171                  }
172                  last unless $found;
173              }
174          }
175
176          if ($found) {
177              --$fk_constraints[$i]->{needed};
178              return 1;
179          }
180      }
181  }
182  return 0;
183 }
184
185 my( $ondel, $onupd );
186
187 sub check_unique {
188  my ($selftable, $selfcol) = @_;
189
190  $ondel = '' if (!defined($ondel));
191  $onupd = '' if (!defined($onupd));
192
193  my $i;
194  for ($i = 0; $i <= $#unique_constraints; ++$i) {
195      if ($selftable eq $unique_constraints[$i]->{'table'}) {
196
197          my $found = 0;
198          for (my $j = 0; $j <= $#$selfcol; ++$j) {
199              $found = 0;
200              for (my $k = 0; $k <= $#{$unique_constraints[$i]->{'cols'}}; ++$k) {
201                  if ($selfcol->[$j] eq $unique_constraints[$i]->{'cols'}->[$k]) {
202                      $found = 1;
203                      last;
204                  }
205              }
206              last unless $found;
207          }
208
209          if ($found) {
210              for (my $j = 0; $j <= $#{$unique_constraints[$i]->{'cols'}}; ++$j) {
211                  $found = 0;
212                  for (my $k = 0; $k <= $#$selfcol; ++$k) {
213                      if ($selfcol->[$k] eq $unique_constraints[$i]->{'cols'}->[$j]) {
214                          $found = 1;
215                          last;
216                      }
217                  }
218                  last unless $found;
219              }
220          }
221
222          if ($found) {
223              --$unique_constraints[$i]->{needed};
224              return 1;
225          }
226      }
227  }
228  return 0;
229 }