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; |
bf5c3a3f |
14 | use Data::Dumper; |
b02b20b5 |
15 | use Exporter; |
b02b20b5 |
16 | use SQL::Translator::Utils qw(debug normalize_name); |
48850f9a |
17 | use Carp::Clan qw/^SQL::Translator|^DBIx::Class/; |
3bf702eb |
18 | use Scalar::Util (); |
b02b20b5 |
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 { |
3bf702eb |
33 | # this is a hack to prevent schema leaks due to a retarded SQLT implementation |
2ad89bf5 |
34 | # DO NOT REMOVE (until SQLT2 is out, the all of this will be rewritten anyway) |
02730621 |
35 | Scalar::Util::weaken ($_[1]) if ref ($_[1]); |
3bf702eb |
36 | |
499adf63 |
37 | my ($tr, $data) = @_; |
38 | my $args = $tr->parser_args; |
b7e303a8 |
39 | my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data; |
40 | $dbicschema ||= $args->{'package'}; |
499adf63 |
41 | my $limit_sources = $args->{'sources'}; |
48850f9a |
42 | |
eb0bc670 |
43 | croak 'No DBIx::Class::Schema' unless ($dbicschema); |
b7e303a8 |
44 | if (!ref $dbicschema) { |
45 | eval "use $dbicschema;"; |
eb0bc670 |
46 | croak "Can't load $dbicschema ($@)" if($@); |
b02b20b5 |
47 | } |
48 | |
49 | my $schema = $tr->schema; |
50 | my $table_no = 0; |
51 | |
b1f9d92e |
52 | $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x')) |
b7e303a8 |
53 | unless ($schema->name); |
38e48163 |
54 | |
b7e303a8 |
55 | my @monikers = sort $dbicschema->sources; |
499adf63 |
56 | if ($limit_sources) { |
57 | my $ref = ref $limit_sources || ''; |
eb0bc670 |
58 | $dbicschema->throw_exception ("'sources' parameter must be an array or hash ref") |
59 | unless( $ref eq 'ARRAY' || ref eq 'HASH' ); |
499adf63 |
60 | |
61 | # limit monikers to those specified in |
62 | my $sources; |
63 | if ($ref eq 'ARRAY') { |
64 | $sources->{$_} = 1 for (@$limit_sources); |
65 | } else { |
66 | $sources = $limit_sources; |
67 | } |
68 | @monikers = grep { $sources->{$_} } @monikers; |
69 | } |
70 | |
71 | |
a7f4b74c |
72 | my(%table_monikers, %view_monikers); |
1d521afd |
73 | for my $moniker (@monikers){ |
74 | my $source = $dbicschema->source($moniker); |
5f5e87cf |
75 | if ( $source->isa('DBIx::Class::ResultSource::Table') ) { |
a7f4b74c |
76 | $table_monikers{$moniker}++; |
5f5e87cf |
77 | } elsif( $source->isa('DBIx::Class::ResultSource::View') ){ |
bccd177f |
78 | next if $source->is_virtual; |
a7f4b74c |
79 | $view_monikers{$moniker}++; |
1d521afd |
80 | } |
81 | } |
499adf63 |
82 | |
48850f9a |
83 | my %tables; |
a7f4b74c |
84 | foreach my $moniker (sort keys %table_monikers) |
b02b20b5 |
85 | { |
b7e303a8 |
86 | my $source = $dbicschema->source($moniker); |
48850f9a |
87 | my $table_name = $source->name; |
39be4120 |
88 | |
4678e9da |
89 | # FIXME - this isn't the right way to do it, but sqlt does not |
90 | # support quoting properly to be signaled about this |
a8fcfb9f |
91 | $table_name = $$table_name if ref $table_name eq 'SCALAR'; |
b02b20b5 |
92 | |
e3d5a547 |
93 | # It's possible to have multiple DBIC sources using the same table |
48850f9a |
94 | next if $tables{$table_name}; |
38e48163 |
95 | |
48850f9a |
96 | $tables{$table_name}{source} = $source; |
97 | my $table = $tables{$table_name}{object} = SQL::Translator::Schema::Table->new( |
98 | name => $table_name, |
b02b20b5 |
99 | type => 'TABLE', |
48850f9a |
100 | ); |
b02b20b5 |
101 | foreach my $col ($source->columns) |
102 | { |
d6c79cb3 |
103 | # assuming column_info in dbic is the same as DBI (?) |
b02b20b5 |
104 | # data_type is a number, column_type is text? |
105 | my %colinfo = ( |
106 | name => $col, |
b02b20b5 |
107 | size => 0, |
108 | is_auto_increment => 0, |
109 | is_foreign_key => 0, |
110 | is_nullable => 0, |
111 | %{$source->column_info($col)} |
112 | ); |
0009fa49 |
113 | if ($colinfo{is_nullable}) { |
114 | $colinfo{default} = '' unless exists $colinfo{default}; |
115 | } |
eb0bc670 |
116 | my $f = $table->add_field(%colinfo) |
117 | || $dbicschema->throw_exception ($table->error); |
b02b20b5 |
118 | } |
b02b20b5 |
119 | |
7b90bb13 |
120 | my @primary = $source->primary_columns; |
36e9e24f |
121 | |
122 | $table->primary_key(@primary) if @primary; |
123 | |
7b90bb13 |
124 | my %unique_constraints = $source->unique_constraints; |
b7e303a8 |
125 | foreach my $uniq (sort keys %unique_constraints) { |
6d0ee587 |
126 | if (!$source->_compare_relationship_keys($unique_constraints{$uniq}, \@primary)) { |
7b90bb13 |
127 | $table->add_constraint( |
128 | type => 'unique', |
a7e65bb5 |
129 | name => $uniq, |
7b90bb13 |
130 | fields => $unique_constraints{$uniq} |
131 | ); |
132 | } |
133 | } |
134 | |
b02b20b5 |
135 | my @rels = $source->relationships(); |
499adf63 |
136 | |
137 | my %created_FK_rels; |
48850f9a |
138 | |
2581038c |
139 | # global add_fk_index set in parser_args |
a7f4b74c |
140 | my $add_fk_index = (exists $args->{add_fk_index} && ! $args->{add_fk_index}) ? 0 : 1; |
499adf63 |
141 | |
454a5a42 |
142 | foreach my $rel (sort @rels) |
b02b20b5 |
143 | { |
a7f4b74c |
144 | |
b02b20b5 |
145 | my $rel_info = $source->relationship_info($rel); |
637ca936 |
146 | |
637ca936 |
147 | # Ignore any rel cond that isn't a straight hash |
148 | next unless ref $rel_info->{cond} eq 'HASH'; |
149 | |
a7f4b74c |
150 | my $relsource = $source->related_source($rel); |
151 | |
152 | # related sources might be excluded via a {sources} filter or might be views |
153 | next unless exists $table_monikers{$relsource->source_name}; |
154 | |
155 | my $rel_table = $relsource->name; |
4678e9da |
156 | |
157 | # FIXME - this isn't the right way to do it, but sqlt does not |
158 | # support quoting properly to be signaled about this |
159 | $rel_table = $$rel_table if ref $rel_table eq 'SCALAR'; |
de60a93d |
160 | |
e377d723 |
161 | my $reverse_rels = $source->reverse_relationship_info($rel); |
162 | my ($otherrelname, $otherrelationship) = each %{$reverse_rels}; |
163 | |
d1b264d3 |
164 | # Force the order of @cond to match the order of ->add_columns |
165 | my $idx; |
a7f4b74c |
166 | my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $relsource->columns; |
d1b264d3 |
167 | my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); |
48850f9a |
168 | |
637ca936 |
169 | # Get the key information, mapping off the foreign/self markers |
637ca936 |
170 | my @refkeys = map {/^\w+\.(\w+)$/} @cond; |
171 | my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond; |
172 | |
e377d723 |
173 | # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to) |
174 | my $fk_constraint; |
de60a93d |
175 | |
e377d723 |
176 | #first it can be specified explicitly |
177 | if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) { |
178 | $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint}; |
179 | } |
180 | # it can not be multi |
069e49b6 |
181 | elsif ( $rel_info->{attrs}{accessor} |
182 | && $rel_info->{attrs}{accessor} eq 'multi' ) { |
e377d723 |
183 | $fk_constraint = 0; |
184 | } |
185 | # if indeed single, check if all self.columns are our primary keys. |
186 | # this is supposed to indicate a has_one/might_have... |
187 | # where's the introspection!!?? :) |
188 | else { |
6d0ee587 |
189 | $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary); |
e377d723 |
190 | } |
de60a93d |
191 | |
e377d723 |
192 | my $cascade; |
193 | for my $c (qw/delete update/) { |
194 | if (exists $rel_info->{attrs}{"on_$c"}) { |
195 | if ($fk_constraint) { |
196 | $cascade->{$c} = $rel_info->{attrs}{"on_$c"}; |
197 | } |
db79c254 |
198 | elsif ( $rel_info->{attrs}{"on_$c"} ) { |
48850f9a |
199 | carp "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. " |
e377d723 |
200 | . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n"; |
201 | } |
202 | } |
203 | elsif (defined $otherrelationship and $otherrelationship->{attrs}{$c eq 'update' ? 'cascade_copy' : 'cascade_delete'}) { |
204 | $cascade->{$c} = 'CASCADE'; |
de60a93d |
205 | } |
e377d723 |
206 | } |
207 | |
cae467a7 |
208 | if($rel_table) { |
e377d723 |
209 | # Constraints are added only if applicable |
210 | next unless $fk_constraint; |
211 | |
212 | # Make sure we dont create the same foreign key constraint twice |
e3d5a547 |
213 | my $key_test = join("\x00", sort @keys); |
e377d723 |
214 | next if $created_FK_rels{$rel_table}->{$key_test}; |
de60a93d |
215 | |
48850f9a |
216 | if (scalar(@keys)) { |
48850f9a |
217 | $created_FK_rels{$rel_table}->{$key_test} = 1; |
13de943d |
218 | |
48850f9a |
219 | my $is_deferrable = $rel_info->{attrs}{is_deferrable}; |
220 | |
a7f4b74c |
221 | # calculate dependencies: do not consider deferrable constraints and |
222 | # self-references for dependency calculations |
48850f9a |
223 | if (! $is_deferrable and $rel_table ne $table_name) { |
224 | $tables{$table_name}{foreign_table_deps}{$rel_table}++; |
225 | } |
7b5d0b84 |
226 | |
7b5d0b84 |
227 | $table->add_constraint( |
cae467a7 |
228 | type => 'foreign_key', |
229 | name => join('_', $table_name, 'fk', @keys), |
230 | fields => \@keys, |
231 | reference_fields => \@refkeys, |
232 | reference_table => $rel_table, |
233 | on_delete => uc ($cascade->{delete} || ''), |
234 | on_update => uc ($cascade->{update} || ''), |
235 | (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()), |
7b5d0b84 |
236 | ); |
48850f9a |
237 | |
238 | # global parser_args add_fk_index param can be overridden on the rel def |
239 | my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index; |
240 | |
c49ff507 |
241 | # Check that we do not create an index identical to the PK index |
242 | # (some RDBMS croak on this, and it generally doesn't make much sense) |
243 | # NOTE: we do not sort the key columns because the order of |
5930bfc2 |
244 | # columns is important for indexes and two indexes with the |
245 | # same cols but different order are allowed and sometimes |
246 | # needed |
c49ff507 |
247 | next if join("\x00", @keys) eq join("\x00", @primary); |
48850f9a |
248 | |
2581038c |
249 | if ($add_fk_index_rel) { |
0e2c6809 |
250 | my $index = $table->add_index( |
5930bfc2 |
251 | name => join('_', $table_name, 'idx', @keys), |
252 | fields => \@keys, |
253 | type => 'NORMAL', |
254 | ); |
0e2c6809 |
255 | } |
256 | } |
b02b20b5 |
257 | } |
258 | } |
48850f9a |
259 | |
b02b20b5 |
260 | } |
d6c79cb3 |
261 | |
48850f9a |
262 | # attach the tables to the schema in dependency order |
263 | my $dependencies = { |
264 | map { $_ => _resolve_deps ($_, \%tables) } (keys %tables) |
265 | }; |
bf5c3a3f |
266 | |
48850f9a |
267 | for my $table (sort |
268 | { |
269 | keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} } |
270 | || |
271 | $a cmp $b |
272 | } |
273 | (keys %tables) |
274 | ) { |
275 | $schema->add_table ($tables{$table}{object}); |
276 | $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} ); |
48850f9a |
277 | |
7adc2091 |
278 | # the hook might have already removed the table |
279 | if ($schema->get_table($table) && $table =~ /^ \s* \( \s* SELECT \s+/ix) { |
280 | warn <<'EOW'; |
a8fcfb9f |
281 | |
7adc2091 |
282 | Custom SQL through ->name(\'( SELECT ...') is DEPRECATED, for more details see |
283 | "Arbitrary SQL through a custom ResultSource" in DBIx::Class::Manual::Cookbook |
284 | or http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod |
39be4120 |
285 | |
7adc2091 |
286 | EOW |
39be4120 |
287 | |
7adc2091 |
288 | # remove the table as there is no way someone might want to |
289 | # actually deploy this |
290 | $schema->drop_table ($table); |
39be4120 |
291 | } |
292 | } |
48850f9a |
293 | |
294 | my %views; |
bf5c3a3f |
295 | #my @view_sources = |
296 | #sort { |
297 | #(exists $a->deploy_depends_on->{$b->source_name} ? 1 : 0) |
298 | #<=> |
299 | #(exists $b->deploy_depends_on->{$a->source_name} ? 1 : 0) |
300 | #} |
301 | #map { $dbicschema->source($_) } (sort keys %view_monikers); |
302 | |
303 | #my @view_sources = |
304 | #grep { $_->can('view_definition') } # make sure it's a view |
305 | #map { $dbicschema->source($_) } # have to get a source |
306 | #map { $tables{$_}{source}{source_name} } # have to get a sourcename |
307 | #sort { |
308 | #keys %{ $dependencies->{$a} || {} } |
309 | #<=> |
310 | #keys %{ $dependencies->{$b} || {} } |
311 | #|| |
312 | #$a cmp $b |
313 | #} |
314 | #keys %$dependencies; |
315 | |
943538a0 |
316 | my @view_sources = |
bf5c3a3f |
317 | sort { |
318 | keys %{ $dependencies->{$a} || {} } |
319 | <=> |
320 | keys %{ $dependencies->{$b} || {} } |
321 | || |
322 | $a cmp $b |
323 | } |
324 | map { $dbicschema->source($_) } (sort keys %view_monikers); |
325 | |
326 | print STDERR Dumper @view_sources; |
327 | print STDERR Dumper "Dependencies: ", $dependencies; |
1999a918 |
328 | |
943538a0 |
329 | foreach my $source (@view_sources) |
1d521afd |
330 | { |
48850f9a |
331 | my $view_name = $source->name; |
4678e9da |
332 | |
333 | # FIXME - this isn't the right way to do it, but sqlt does not |
334 | # support quoting properly to be signaled about this |
335 | $view_name = $$view_name if ref $view_name eq 'SCALAR'; |
48850f9a |
336 | |
1d521afd |
337 | # Skip custom query sources |
48850f9a |
338 | next if ref $view_name; |
1d521afd |
339 | |
340 | # Its possible to have multiple DBIC source using same table |
48850f9a |
341 | next if $views{$view_name}++; |
1d521afd |
342 | |
ab7e74aa |
343 | $dbicschema->throw_exception ("view $view_name is missing a view_definition") |
344 | unless $source->view_definition; |
8f1617e2 |
345 | |
48850f9a |
346 | my $view = $schema->add_view ( |
347 | name => $view_name, |
1d521afd |
348 | fields => [ $source->columns ], |
f534e33b |
349 | $source->view_definition ? ( 'sql' => $source->view_definition ) : () |
eb0bc670 |
350 | ) || $dbicschema->throw_exception ($schema->error); |
a99b214b |
351 | |
352 | $source->_invoke_sqlt_deploy_hook($view); |
1d521afd |
353 | } |
354 | |
48850f9a |
355 | |
b7e303a8 |
356 | if ($dbicschema->can('sqlt_deploy_hook')) { |
357 | $dbicschema->sqlt_deploy_hook($schema); |
d6c79cb3 |
358 | } |
359 | |
637ca936 |
360 | return 1; |
75d07914 |
361 | } |
b02b20b5 |
362 | |
48850f9a |
363 | # |
364 | # Quick and dirty dependency graph calculator |
365 | # |
366 | sub _resolve_deps { |
367 | my ($table, $tables, $seen) = @_; |
368 | |
369 | my $ret = {}; |
370 | $seen ||= {}; |
371 | |
372 | # copy and bump all deps by one (so we can reconstruct the chain) |
373 | my %seen = map { $_ => $seen->{$_} + 1 } (keys %$seen); |
374 | $seen{$table} = 1; |
375 | |
376 | for my $dep (keys %{$tables->{$table}{foreign_table_deps}} ) { |
377 | |
378 | if ($seen->{$dep}) { |
379 | |
380 | # warn and remove the circular constraint so we don't get flooded with the same warning over and over |
381 | #carp sprintf ("Circular dependency detected, schema may not be deployable:\n%s\n", |
382 | # join (' -> ', (sort { $seen->{$b} <=> $seen->{$a} } (keys %$seen) ), $table, $dep ) |
383 | #); |
384 | #delete $tables->{$table}{foreign_table_deps}{$dep}; |
385 | |
386 | return {}; |
387 | } |
388 | |
389 | my $subdeps = _resolve_deps ($dep, $tables, \%seen); |
390 | $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps ); |
391 | |
392 | ++$ret->{$dep}; |
393 | } |
394 | |
395 | return $ret; |
396 | } |
397 | |
0da8b7da |
398 | 1; |
7232ce07 |
399 | |
400 | =head1 NAME |
401 | |
402 | SQL::Translator::Parser::DBIx::Class - Create a SQL::Translator schema |
403 | from a DBIx::Class::Schema instance |
404 | |
405 | =head1 SYNOPSIS |
406 | |
f26d4b95 |
407 | ## Via DBIx::Class |
408 | use MyApp::Schema; |
409 | my $schema = MyApp::Schema->connect("dbi:SQLite:something.db"); |
410 | $schema->create_ddl_dir(); |
411 | ## or |
412 | $schema->deploy(); |
413 | |
414 | ## Standalone |
7232ce07 |
415 | use MyApp::Schema; |
416 | use SQL::Translator; |
d4daee7b |
417 | |
7232ce07 |
418 | my $schema = MyApp::Schema->connect; |
419 | my $trans = SQL::Translator->new ( |
420 | parser => 'SQL::Translator::Parser::DBIx::Class', |
37cb0de1 |
421 | parser_args => { |
422 | package => $schema, |
cae467a7 |
423 | add_fk_index => 0, |
37cb0de1 |
424 | sources => [qw/ |
425 | Artist |
426 | CD |
427 | /], |
428 | }, |
7232ce07 |
429 | producer => 'SQLite', |
430 | ) or die SQL::Translator->error; |
431 | my $out = $trans->translate() or die $trans->error; |
432 | |
433 | =head1 DESCRIPTION |
434 | |
f26d4b95 |
435 | This class requires L<SQL::Translator> installed to work. |
436 | |
7232ce07 |
437 | C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema, |
438 | interrogates the columns, and stuffs it all in an $sqlt_schema object. |
439 | |
faaba25f |
440 | Its primary use is in deploying database layouts described as a set |
db2b2eb6 |
441 | of L<DBIx::Class> classes, to a database. To do this, see |
442 | L<DBIx::Class::Schema/deploy>. |
f26d4b95 |
443 | |
444 | This can also be achieved by having DBIx::Class export the schema as a |
445 | set of SQL files ready for import into your database, or passed to |
446 | other machines that need to have your application installed but don't |
db2b2eb6 |
447 | have SQL::Translator installed. To do this see |
448 | L<DBIx::Class::Schema/create_ddl_dir>. |
f26d4b95 |
449 | |
cae467a7 |
450 | =head1 PARSER OPTIONS |
451 | |
452 | =head2 add_fk_index |
453 | |
454 | Create an index for each foreign key. |
ad91ed74 |
455 | Enabled by default, as having indexed foreign key columns is normally the |
456 | sensible thing to do. |
cae467a7 |
457 | |
458 | =head2 sources |
459 | |
460 | =over 4 |
461 | |
f0ac764e |
462 | =item Arguments: \@class_names |
cae467a7 |
463 | |
464 | =back |
ad91ed74 |
465 | |
466 | Limit the amount of parsed sources by supplying an explicit list of source names. |
cae467a7 |
467 | |
7232ce07 |
468 | =head1 SEE ALSO |
469 | |
f26d4b95 |
470 | L<SQL::Translator>, L<DBIx::Class::Schema> |
7232ce07 |
471 | |
472 | =head1 AUTHORS |
473 | |
827a808f |
474 | See L<DBIx::Class/CONTRIBUTORS>. |
7232ce07 |
475 | |
827a808f |
476 | =head1 LICENSE |
7232ce07 |
477 | |
827a808f |
478 | You may distribute this code under the same terms as Perl itself. |
7232ce07 |
479 | |
827a808f |
480 | =cut |