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