4 use DBICTest::HelperRels;
6 eval "use SQL::Translator";
7 plan skip_all => 'SQL::Translator required' if $@;
9 # do not taunt happy dave ball
11 my $schema = DBICTest::Schema;
15 my $translator = SQL::Translator->new(
17 'DBIx::Schema' => $schema,
23 $translator->parser('SQL::Translator::Parser::DBIx::Class');
24 $translator->producer('SQLite');
26 my $output = $translator->translate();
30 {'display' => 'twokeys->cd',
31 'selftable' => 'twokeys', 'foreigntable' => 'cd',
32 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
33 'needed' => 1, on_delete => '', on_update => ''},
34 {'display' => 'twokeys->artist',
35 'selftable' => 'twokeys', 'foreigntable' => 'artist',
36 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
37 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
38 {'display' => 'cd_to_producer->cd',
39 'selftable' => 'cd_to_producer', 'foreigntable' => 'cd',
40 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
41 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
42 {'display' => 'cd_to_producer->producer',
43 'selftable' => 'cd_to_producer', 'foreigntable' => 'producer',
44 'selfcols' => ['producer'], 'foreigncols' => ['producerid'],
45 'needed' => 1, on_delete => '', on_update => ''},
46 {'display' => 'self_ref_alias -> self_ref for self_ref',
47 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
48 'selfcols' => ['self_ref'], 'foreigncols' => ['id'],
49 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
50 {'display' => 'self_ref_alias -> self_ref for alias',
51 'selftable' => 'self_ref_alias', 'foreigntable' => 'self_ref',
52 'selfcols' => ['alias'], 'foreigncols' => ['id'],
53 'needed' => 1, on_delete => '', on_update => ''},
54 {'display' => 'cd -> artist',
55 'selftable' => 'cd', 'foreigntable' => 'artist',
56 'selfcols' => ['artist'], 'foreigncols' => ['artistid'],
57 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
58 {'display' => 'artist_undirected_map -> artist for id1',
59 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
60 'selfcols' => ['id1'], 'foreigncols' => ['artistid'],
61 'needed' => 1, on_delete => 'CASCADE', on_update => ''},
62 {'display' => 'artist_undirected_map -> artist for id2',
63 'selftable' => 'artist_undirected_map', 'foreigntable' => 'artist',
64 'selfcols' => ['id2'], 'foreigncols' => ['artistid'],
65 'needed' => 1, on_delete => 'CASCADE', on_update => ''},
66 {'display' => 'track->cd',
67 'selftable' => 'track', 'foreigntable' => 'cd',
68 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
69 'needed' => 2, on_delete => 'CASCADE', on_update => 'CASCADE'},
70 {'display' => 'treelike -> treelike for parent',
71 'selftable' => 'treelike', 'foreigntable' => 'treelike',
72 'selfcols' => ['parent'], 'foreigncols' => ['id'],
73 'needed' => 1, on_delete => '', on_update => ''},
75 # shouldn't this be generated?
77 #{'display' => 'twokeytreelike -> twokeytreelike for parent1,parent2',
78 # 'selftable' => 'twokeytreelike', 'foreigntable' => 'twokeytreelike',
79 # 'selfcols' => ['parent1', 'parent2'], 'foreigncols' => ['id1','id2'],
80 # 'needed' => 1, on_delete => '', on_update => ''},
82 {'display' => 'tags -> cd',
83 'selftable' => 'tags', 'foreigntable' => 'cd',
84 'selfcols' => ['cd'], 'foreigncols' => ['cdid'],
85 'needed' => 1, on_delete => 'CASCADE', on_update => 'CASCADE'},
86 {'display' => 'bookmark -> link',
87 'selftable' => 'bookmark', 'foreigntable' => 'link',
88 'selfcols' => ['link'], 'foreigncols' => ['id'],
89 'needed' => 1, on_delete => '', on_update => ''},
92 my @unique_constraints = (
93 {'display' => 'cd artist and title unique',
94 'table' => 'cd', 'cols' => ['artist', 'title'],
96 {'display' => 'twokeytreelike name unique',
97 'table' => 'twokeytreelike', 'cols' => ['name'],
99 # {'display' => 'employee position and group_id unique',
100 # 'table' => 'employee', cols => ['position', 'group_id'],
104 my $tschema = $translator->schema();
105 for my $table ($tschema->get_tables) {
106 my $table_name = $table->name;
107 for my $c ( $table->get_constraints ) {
108 if ($c->type eq 'FOREIGN KEY') {
109 ok(check_fk($table_name, scalar $c->fields,
110 $c->reference_table, scalar $c->reference_fields,
111 $c->on_delete, $c->on_update), "Foreign key constraint on $table_name matches an expected constraint");
113 elsif ($c->type eq 'UNIQUE') {
114 ok(check_unique($table_name, scalar $c->fields),
115 "Unique constraint on $table_name matches an expected constraint");
120 # Make sure all the foreign keys are done.
122 for ($i = 0; $i <= $#fk_constraints; ++$i) {
123 ok(!$fk_constraints[$i]->{'needed'}, "Constraint $fk_constraints[$i]->{display}");
125 # Make sure all the uniques are done.
126 for ($i = 0; $i <= $#unique_constraints; ++$i) {
127 ok(!$unique_constraints[$i]->{'needed'}, "Constraint $unique_constraints[$i]->{display}");
131 my ($selftable, $selfcol, $foreigntable, $foreigncol, $ondel, $onupd) = @_;
133 $ondel = '' if (!defined($ondel));
134 $onupd = '' if (!defined($onupd));
137 for ($i = 0; $i <= $#fk_constraints; ++$i) {
138 if ($selftable eq $fk_constraints[$i]->{'selftable'} &&
139 $foreigntable eq $fk_constraints[$i]->{'foreigntable'} &&
140 ($ondel eq $fk_constraints[$i]->{on_delete}) &&
141 ($onupd eq $fk_constraints[$i]->{on_update})) {
145 for (my $j = 0; $j <= $#$selfcol; ++$j) {
147 for (my $k = 0; $k <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$k) {
148 if ($selfcol->[$j] eq $fk_constraints[$i]->{'selfcols'}->[$k] &&
149 $foreigncol->[$j] eq $fk_constraints[$i]->{'foreigncols'}->[$k]) {
158 for (my $j = 0; $j <= $#{$fk_constraints[$i]->{'selfcols'}}; ++$j) {
160 for (my $k = 0; $k <= $#$selfcol; ++$k) {
161 if ($selfcol->[$k] eq $fk_constraints[$i]->{'selfcols'}->[$j] &&
162 $foreigncol->[$k] eq $fk_constraints[$i]->{'foreigncols'}->[$j]) {
172 --$fk_constraints[$i]->{needed};
181 my ($selftable, $selfcol) = @_;
183 $ondel = '' if (!defined($ondel));
184 $onupd = '' if (!defined($onupd));
187 for ($i = 0; $i <= $#unique_constraints; ++$i) {
188 if ($selftable eq $unique_constraints[$i]->{'table'}) {
191 for (my $j = 0; $j <= $#$selfcol; ++$j) {
193 for (my $k = 0; $k <= $#{$unique_constraints[$i]->{'cols'}}; ++$k) {
194 if ($selfcol->[$j] eq $unique_constraints[$i]->{'cols'}->[$k]) {
203 for (my $j = 0; $j <= $#{$unique_constraints[$i]->{'cols'}}; ++$j) {
205 for (my $k = 0; $k <= $#$selfcol; ++$k) {
206 if ($selfcol->[$k] eq $unique_constraints[$i]->{'cols'}->[$j]) {
216 --$unique_constraints[$i]->{needed};