Commit | Line | Data |
392cebac |
1 | package SQL::Translator::Parser::DBIx::Class; |
b02b20b5 |
2 | |
3 | # AUTHOR: Jess Robinson |
4 | |
1d996af5 |
5 | # Some mistakes the fault of Matt S Trout |
6 | |
c385ecea |
7 | # Others the fault of Ash Berlin |
8 | |
b02b20b5 |
9 | use strict; |
10 | use warnings; |
b7e303a8 |
11 | use vars qw($DEBUG @EXPORT_OK); |
b02b20b5 |
12 | $DEBUG = 0 unless defined $DEBUG; |
b02b20b5 |
13 | |
14 | use Exporter; |
15 | use Data::Dumper; |
16 | use SQL::Translator::Utils qw(debug normalize_name); |
17 | |
18 | use base qw(Exporter); |
19 | |
20 | @EXPORT_OK = qw(parse); |
21 | |
22 | # ------------------------------------------------------------------- |
23 | # parse($tr, $data) |
24 | # |
0e2c6809 |
25 | # setting parser_args => { add_fk_index => 0 } will prevent |
26 | # the auto-generation of an index for each FK. |
27 | # |
880a1a0c |
28 | # Note that $data, in the case of this parser, is not useful. |
b02b20b5 |
29 | # We're working with DBIx::Class Schemas, not data streams. |
30 | # ------------------------------------------------------------------- |
31 | sub parse { |
499adf63 |
32 | my ($tr, $data) = @_; |
33 | my $args = $tr->parser_args; |
b7e303a8 |
34 | my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data; |
35 | $dbicschema ||= $args->{'package'}; |
499adf63 |
36 | my $limit_sources = $args->{'sources'}; |
b02b20b5 |
37 | |
b7e303a8 |
38 | die 'No DBIx::Class::Schema' unless ($dbicschema); |
39 | if (!ref $dbicschema) { |
40 | eval "use $dbicschema;"; |
41 | die "Can't load $dbicschema ($@)" if($@); |
b02b20b5 |
42 | } |
43 | |
44 | my $schema = $tr->schema; |
45 | my $table_no = 0; |
46 | |
b1f9d92e |
47 | $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x')) |
b7e303a8 |
48 | unless ($schema->name); |
38e48163 |
49 | |
50 | my %seen_tables; |
51 | |
b7e303a8 |
52 | my @monikers = sort $dbicschema->sources; |
499adf63 |
53 | if ($limit_sources) { |
54 | my $ref = ref $limit_sources || ''; |
5bdf1b3d |
55 | die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH'; |
499adf63 |
56 | |
57 | # limit monikers to those specified in |
58 | my $sources; |
59 | if ($ref eq 'ARRAY') { |
60 | $sources->{$_} = 1 for (@$limit_sources); |
61 | } else { |
62 | $sources = $limit_sources; |
63 | } |
64 | @monikers = grep { $sources->{$_} } @monikers; |
65 | } |
66 | |
67 | |
454a5a42 |
68 | foreach my $moniker (sort @monikers) |
b02b20b5 |
69 | { |
b7e303a8 |
70 | my $source = $dbicschema->source($moniker); |
90c11f68 |
71 | |
72 | # Skip custom query sources |
73 | next if ref($source->name); |
b02b20b5 |
74 | |
b7e303a8 |
75 | # Its possible to have multiple DBIC source using same table |
38e48163 |
76 | next if $seen_tables{$source->name}++; |
77 | |
b02b20b5 |
78 | my $table = $schema->add_table( |
79 | name => $source->name, |
80 | type => 'TABLE', |
81 | ) || die $schema->error; |
82 | my $colcount = 0; |
83 | foreach my $col ($source->columns) |
84 | { |
d6c79cb3 |
85 | # assuming column_info in dbic is the same as DBI (?) |
b02b20b5 |
86 | # data_type is a number, column_type is text? |
87 | my %colinfo = ( |
88 | name => $col, |
b02b20b5 |
89 | size => 0, |
90 | is_auto_increment => 0, |
91 | is_foreign_key => 0, |
92 | is_nullable => 0, |
93 | %{$source->column_info($col)} |
94 | ); |
0009fa49 |
95 | if ($colinfo{is_nullable}) { |
96 | $colinfo{default} = '' unless exists $colinfo{default}; |
97 | } |
b02b20b5 |
98 | my $f = $table->add_field(%colinfo) || die $table->error; |
99 | } |
100 | $table->primary_key($source->primary_columns); |
101 | |
7b90bb13 |
102 | my @primary = $source->primary_columns; |
103 | my %unique_constraints = $source->unique_constraints; |
b7e303a8 |
104 | foreach my $uniq (sort keys %unique_constraints) { |
de60a93d |
105 | if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) { |
7b90bb13 |
106 | $table->add_constraint( |
107 | type => 'unique', |
a7e65bb5 |
108 | name => $uniq, |
7b90bb13 |
109 | fields => $unique_constraints{$uniq} |
110 | ); |
111 | } |
112 | } |
113 | |
b02b20b5 |
114 | my @rels = $source->relationships(); |
499adf63 |
115 | |
116 | my %created_FK_rels; |
2581038c |
117 | |
118 | # global add_fk_index set in parser_args |
119 | my $add_fk_index = (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) ? 0 : 1; |
499adf63 |
120 | |
454a5a42 |
121 | foreach my $rel (sort @rels) |
b02b20b5 |
122 | { |
123 | my $rel_info = $source->relationship_info($rel); |
637ca936 |
124 | |
637ca936 |
125 | # Ignore any rel cond that isn't a straight hash |
126 | next unless ref $rel_info->{cond} eq 'HASH'; |
127 | |
de60a93d |
128 | my $othertable = $source->related_source($rel); |
129 | my $rel_table = $othertable->name; |
130 | |
e377d723 |
131 | my $reverse_rels = $source->reverse_relationship_info($rel); |
132 | my ($otherrelname, $otherrelationship) = each %{$reverse_rels}; |
133 | |
d1b264d3 |
134 | # Force the order of @cond to match the order of ->add_columns |
135 | my $idx; |
136 | my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $othertable->columns; |
137 | my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); |
138 | |
637ca936 |
139 | # Get the key information, mapping off the foreign/self markers |
637ca936 |
140 | my @refkeys = map {/^\w+\.(\w+)$/} @cond; |
141 | my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond; |
142 | |
e377d723 |
143 | # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to) |
144 | my $fk_constraint; |
de60a93d |
145 | |
e377d723 |
146 | #first it can be specified explicitly |
147 | if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) { |
148 | $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint}; |
149 | } |
150 | # it can not be multi |
069e49b6 |
151 | elsif ( $rel_info->{attrs}{accessor} |
152 | && $rel_info->{attrs}{accessor} eq 'multi' ) { |
e377d723 |
153 | $fk_constraint = 0; |
154 | } |
155 | # if indeed single, check if all self.columns are our primary keys. |
156 | # this is supposed to indicate a has_one/might_have... |
157 | # where's the introspection!!?? :) |
158 | else { |
159 | $fk_constraint = not $source->compare_relationship_keys(\@keys, \@primary); |
160 | } |
de60a93d |
161 | |
e377d723 |
162 | my $cascade; |
163 | for my $c (qw/delete update/) { |
164 | if (exists $rel_info->{attrs}{"on_$c"}) { |
165 | if ($fk_constraint) { |
166 | $cascade->{$c} = $rel_info->{attrs}{"on_$c"}; |
167 | } |
168 | else { |
169 | warn "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. " |
170 | . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n"; |
171 | } |
172 | } |
173 | elsif (defined $otherrelationship and $otherrelationship->{attrs}{$c eq 'update' ? 'cascade_copy' : 'cascade_delete'}) { |
174 | $cascade->{$c} = 'CASCADE'; |
de60a93d |
175 | } |
e377d723 |
176 | } |
177 | |
178 | if($rel_table) |
179 | { |
180 | # Constraints are added only if applicable |
181 | next unless $fk_constraint; |
182 | |
183 | # Make sure we dont create the same foreign key constraint twice |
184 | my $key_test = join("\x00", @keys); |
185 | next if $created_FK_rels{$rel_table}->{$key_test}; |
de60a93d |
186 | |
45f1a484 |
187 | my $is_deferrable = $rel_info->{attrs}{is_deferrable}; |
2581038c |
188 | |
189 | # global parser_args add_fk_index param can be overridden on the rel def |
190 | my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index; |
13de943d |
191 | |
7b5d0b84 |
192 | |
193 | $created_FK_rels{$rel_table}->{$key_test} = 1; |
194 | if (scalar(@keys)) { |
195 | $table->add_constraint( |
0d865134 |
196 | type => 'foreign_key', |
a7e65bb5 |
197 | name => join('_', $table->name, 'fk', @keys), |
0d865134 |
198 | fields => \@keys, |
199 | reference_fields => \@refkeys, |
200 | reference_table => $rel_table, |
555b3385 |
201 | on_delete => uc ($cascade->{delete} || ''), |
202 | on_update => uc ($cascade->{update} || ''), |
e394339b |
203 | (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()), |
7b5d0b84 |
204 | ); |
0d865134 |
205 | |
2581038c |
206 | if ($add_fk_index_rel) { |
0e2c6809 |
207 | my $index = $table->add_index( |
208 | name => join('_', $table->name, 'idx', @keys), |
209 | fields => \@keys, |
210 | type => 'NORMAL', |
211 | ); |
212 | } |
213 | } |
b02b20b5 |
214 | } |
215 | } |
0e2c6809 |
216 | |
880c075b |
217 | $source->_invoke_sqlt_deploy_hook($table); |
b02b20b5 |
218 | } |
d6c79cb3 |
219 | |
b7e303a8 |
220 | if ($dbicschema->can('sqlt_deploy_hook')) { |
221 | $dbicschema->sqlt_deploy_hook($schema); |
d6c79cb3 |
222 | } |
223 | |
637ca936 |
224 | return 1; |
75d07914 |
225 | } |
b02b20b5 |
226 | |
0da8b7da |
227 | 1; |
7232ce07 |
228 | |
229 | =head1 NAME |
230 | |
231 | SQL::Translator::Parser::DBIx::Class - Create a SQL::Translator schema |
232 | from a DBIx::Class::Schema instance |
233 | |
234 | =head1 SYNOPSIS |
235 | |
236 | use MyApp::Schema; |
237 | use SQL::Translator; |
238 | |
239 | my $schema = MyApp::Schema->connect; |
240 | my $trans = SQL::Translator->new ( |
241 | parser => 'SQL::Translator::Parser::DBIx::Class', |
242 | parser_args => { package => $schema }, |
243 | producer => 'SQLite', |
244 | ) or die SQL::Translator->error; |
245 | my $out = $trans->translate() or die $trans->error; |
246 | |
247 | =head1 DESCRIPTION |
248 | |
249 | C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema, |
250 | interrogates the columns, and stuffs it all in an $sqlt_schema object. |
251 | |
252 | =head1 SEE ALSO |
253 | |
254 | SQL::Translator. |
255 | |
256 | =head1 AUTHORS |
257 | |
258 | Jess Robinson |
259 | |
260 | Matt S Trout |
261 | |
262 | Ash Berlin |