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