Stop accepting foreign_values => undef/rowobj in the resolver
[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;
f9cc85ce 11our ($DEBUG, $VERSION, @EXPORT_OK);
4a9f6cdc 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);
70c28808 17use DBIx::Class::Carp qw/^SQL::Translator|^DBIx::Class|^Try::Tiny/;
e2741c7f 18use DBIx::Class::_Util qw( dbic_internal_try dbic_internal_catch );
63a18cfe 19use Class::C3::Componentised;
7536c92b 20use Scalar::Util 'blessed';
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 {
499adf63 37 my ($tr, $data) = @_;
38 my $args = $tr->parser_args;
a07ae2aa 39
40 my $dbicschema = $data || $args->{dbic_schema};
41
42 for (qw(DBIx::Class::Schema DBIx::Schema package)) {
43 if (defined (my $s = delete $args->{$_} )) {
44 carp_unique("Supplying a schema via ... parser_args => { '$_' => \$schema } is deprecated. Please use parser_args => { dbic_schema => \$schema } instead");
45
46 # move it from the deprecated to the proper $args slot
47 unless ($dbicschema) {
48 $args->{dbic_schema} = $dbicschema = $s;
49 }
50 }
51 }
48850f9a 52
70c28808 53 DBIx::Class::Exception->throw('No DBIx::Class::Schema') unless ($dbicschema);
a07ae2aa 54
b7e303a8 55 if (!ref $dbicschema) {
ddcc02d1 56 dbic_internal_try {
63a18cfe 57 Class::C3::Componentised->ensure_class_loaded($dbicschema)
e2741c7f 58 }
59 dbic_internal_catch {
63a18cfe 60 DBIx::Class::Exception->throw("Can't load $dbicschema: $_");
61 }
b02b20b5 62 }
63
31399b48 64 if (
65 ref $args->{dbic_schema}
66 and
67 $args->{dbic_schema}->storage
68 ) {
69 # we have a storage-holding $schema instance in $args
70 # we need to dissociate it from that $storage
71 # otherwise SQLT insanity may ensue due to how some
72 # serializing producers treat $args (crazy crazy shit)
73 local $args->{dbic_schema}{storage};
74 $args->{dbic_schema} = $args->{dbic_schema}->clone;
75 }
76
b02b20b5 77 my $schema = $tr->schema;
78 my $table_no = 0;
79
b1f9d92e 80 $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x'))
b7e303a8 81 unless ($schema->name);
38e48163 82
b7e303a8 83 my @monikers = sort $dbicschema->sources;
a07ae2aa 84 if (my $limit_sources = $args->{'sources'}) {
499adf63 85 my $ref = ref $limit_sources || '';
eb0bc670 86 $dbicschema->throw_exception ("'sources' parameter must be an array or hash ref")
87 unless( $ref eq 'ARRAY' || ref eq 'HASH' );
499adf63 88
fd323bf1 89 # limit monikers to those specified in
499adf63 90 my $sources;
91 if ($ref eq 'ARRAY') {
92 $sources->{$_} = 1 for (@$limit_sources);
93 } else {
94 $sources = $limit_sources;
95 }
96 @monikers = grep { $sources->{$_} } @monikers;
97 }
98
99
a7f4b74c 100 my(%table_monikers, %view_monikers);
1d521afd 101 for my $moniker (@monikers){
102 my $source = $dbicschema->source($moniker);
5f5e87cf 103 if ( $source->isa('DBIx::Class::ResultSource::Table') ) {
a7f4b74c 104 $table_monikers{$moniker}++;
5f5e87cf 105 } elsif( $source->isa('DBIx::Class::ResultSource::View') ){
bccd177f 106 next if $source->is_virtual;
a7f4b74c 107 $view_monikers{$moniker}++;
1d521afd 108 }
109 }
499adf63 110
48850f9a 111 my %tables;
a7f4b74c 112 foreach my $moniker (sort keys %table_monikers)
b02b20b5 113 {
b7e303a8 114 my $source = $dbicschema->source($moniker);
48850f9a 115 my $table_name = $source->name;
39be4120 116
4678e9da 117 # FIXME - this isn't the right way to do it, but sqlt does not
118 # support quoting properly to be signaled about this
a8fcfb9f 119 $table_name = $$table_name if ref $table_name eq 'SCALAR';
b02b20b5 120
e3d5a547 121 # It's possible to have multiple DBIC sources using the same table
48850f9a 122 next if $tables{$table_name};
38e48163 123
48850f9a 124 $tables{$table_name}{source} = $source;
125 my $table = $tables{$table_name}{object} = SQL::Translator::Schema::Table->new(
126 name => $table_name,
b02b20b5 127 type => 'TABLE',
48850f9a 128 );
b83736a7 129
130 my $ci = $source->columns_info;
131
132 # same order as add_columns
b02b20b5 133 foreach my $col ($source->columns)
134 {
d6c79cb3 135 # assuming column_info in dbic is the same as DBI (?)
b02b20b5 136 # data_type is a number, column_type is text?
137 my %colinfo = (
138 name => $col,
b02b20b5 139 size => 0,
140 is_auto_increment => 0,
141 is_foreign_key => 0,
142 is_nullable => 0,
b83736a7 143 %{$ci->{$col} || {}}
b02b20b5 144 );
0009fa49 145 if ($colinfo{is_nullable}) {
146 $colinfo{default} = '' unless exists $colinfo{default};
147 }
eb0bc670 148 my $f = $table->add_field(%colinfo)
149 || $dbicschema->throw_exception ($table->error);
b02b20b5 150 }
b02b20b5 151
7b90bb13 152 my @primary = $source->primary_columns;
36e9e24f 153
154 $table->primary_key(@primary) if @primary;
155
7b90bb13 156 my %unique_constraints = $source->unique_constraints;
b7e303a8 157 foreach my $uniq (sort keys %unique_constraints) {
6d0ee587 158 if (!$source->_compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 159 $table->add_constraint(
160 type => 'unique',
a7e65bb5 161 name => $uniq,
7b90bb13 162 fields => $unique_constraints{$uniq}
163 );
164 }
165 }
166
b02b20b5 167 my @rels = $source->relationships();
499adf63 168
169 my %created_FK_rels;
48850f9a 170
2581038c 171 # global add_fk_index set in parser_args
a7f4b74c 172 my $add_fk_index = (exists $args->{add_fk_index} && ! $args->{add_fk_index}) ? 0 : 1;
499adf63 173
e089c417 174 REL:
175 foreach my $rel (sort @rels) {
a7f4b74c 176
b02b20b5 177 my $rel_info = $source->relationship_info($rel);
637ca936 178
637ca936 179 # Ignore any rel cond that isn't a straight hash
180 next unless ref $rel_info->{cond} eq 'HASH';
181
ddcc02d1 182 my $relsource = dbic_internal_try { $source->related_source($rel) };
5b9ecfcc 183 unless ($relsource) {
10e37567 184 carp "Ignoring relationship '$rel' on '$moniker' - related resultsource '$rel_info->{class}' is not registered with this schema\n";
5b9ecfcc 185 next;
186 };
a7f4b74c 187
188 # related sources might be excluded via a {sources} filter or might be views
189 next unless exists $table_monikers{$relsource->source_name};
190
191 my $rel_table = $relsource->name;
4678e9da 192
193 # FIXME - this isn't the right way to do it, but sqlt does not
194 # support quoting properly to be signaled about this
195 $rel_table = $$rel_table if ref $rel_table eq 'SCALAR';
de60a93d 196
d1b264d3 197 # Force the order of @cond to match the order of ->add_columns
198 my $idx;
a7f4b74c 199 my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $relsource->columns;
e089c417 200
201 for ( keys %{$rel_info->{cond}} ) {
202 unless (exists $other_columns_idx{$_}) {
10e37567 203 carp "Ignoring relationship '$rel' on '$moniker' - related resultsource '@{[ $relsource->source_name ]}' does not contain one of the specified columns: '$_'\n";
e089c417 204 next REL;
205 }
206 }
207
208 my @cond = sort { $other_columns_idx{$a} <=> $other_columns_idx{$b} } keys(%{$rel_info->{cond}});
48850f9a 209
637ca936 210 # Get the key information, mapping off the foreign/self markers
637ca936 211 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
212 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
213
e377d723 214 # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to)
215 my $fk_constraint;
de60a93d 216
e377d723 217 #first it can be specified explicitly
218 if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) {
219 $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint};
220 }
221 # it can not be multi
069e49b6 222 elsif ( $rel_info->{attrs}{accessor}
223 && $rel_info->{attrs}{accessor} eq 'multi' ) {
e377d723 224 $fk_constraint = 0;
225 }
226 # if indeed single, check if all self.columns are our primary keys.
227 # this is supposed to indicate a has_one/might_have...
228 # where's the introspection!!?? :)
229 else {
6d0ee587 230 $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary);
e377d723 231 }
de60a93d 232
2f077f92 233
e377d723 234 my $cascade;
a4e4185f 235 CASCADE_TYPE:
e377d723 236 for my $c (qw/delete update/) {
237 if (exists $rel_info->{attrs}{"on_$c"}) {
238 if ($fk_constraint) {
239 $cascade->{$c} = $rel_info->{attrs}{"on_$c"};
240 }
db79c254 241 elsif ( $rel_info->{attrs}{"on_$c"} ) {
48850f9a 242 carp "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. "
e377d723 243 . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n";
244 }
245 }
a4e4185f 246 else {
247 for my $revrelinfo (values %{ $source->reverse_relationship_info($rel) } ) {
248 ( ( $cascade->{$c} = 'CASCADE' ), next CASCADE_TYPE ) if (
249 $revrelinfo->{attrs}
250 ->{ ($c eq 'update')
251 ? 'cascade_copy'
252 : 'cascade_delete'
253 }
254 );
255 }
de60a93d 256 }
e377d723 257 }
258
cae467a7 259 if($rel_table) {
e377d723 260 # Constraints are added only if applicable
261 next unless $fk_constraint;
262
ac86dfe1 263 # Make sure we don't create the same foreign key constraint twice
e3d5a547 264 my $key_test = join("\x00", sort @keys);
e377d723 265 next if $created_FK_rels{$rel_table}->{$key_test};
de60a93d 266
48850f9a 267 if (scalar(@keys)) {
48850f9a 268 $created_FK_rels{$rel_table}->{$key_test} = 1;
13de943d 269
48850f9a 270 my $is_deferrable = $rel_info->{attrs}{is_deferrable};
271
a7f4b74c 272 # calculate dependencies: do not consider deferrable constraints and
273 # self-references for dependency calculations
48850f9a 274 if (! $is_deferrable and $rel_table ne $table_name) {
275 $tables{$table_name}{foreign_table_deps}{$rel_table}++;
276 }
7b5d0b84 277
0e14d918 278 # trim schema before generating constraint/index names
279 (my $table_abbrev = $table_name) =~ s/ ^ [^\.]+ \. //x;
280
7b5d0b84 281 $table->add_constraint(
cae467a7 282 type => 'foreign_key',
0e14d918 283 name => join('_', $table_abbrev, 'fk', @keys),
cae467a7 284 fields => \@keys,
285 reference_fields => \@refkeys,
286 reference_table => $rel_table,
287 on_delete => uc ($cascade->{delete} || ''),
288 on_update => uc ($cascade->{update} || ''),
289 (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
7b5d0b84 290 );
48850f9a 291
292 # global parser_args add_fk_index param can be overridden on the rel def
293 my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index;
294
c49ff507 295 # Check that we do not create an index identical to the PK index
296 # (some RDBMS croak on this, and it generally doesn't make much sense)
297 # NOTE: we do not sort the key columns because the order of
5930bfc2 298 # columns is important for indexes and two indexes with the
299 # same cols but different order are allowed and sometimes
300 # needed
c49ff507 301 next if join("\x00", @keys) eq join("\x00", @primary);
48850f9a 302
2581038c 303 if ($add_fk_index_rel) {
0e14d918 304 (my $idx_name = $table_name) =~ s/ ^ [^\.]+ \. //x;
0e2c6809 305 my $index = $table->add_index(
0e14d918 306 name => join('_', $table_abbrev, 'idx', @keys),
5930bfc2 307 fields => \@keys,
308 type => 'NORMAL',
309 );
0e2c6809 310 }
311 }
b02b20b5 312 }
313 }
48850f9a 314
b02b20b5 315 }
d6c79cb3 316
48850f9a 317 # attach the tables to the schema in dependency order
318 my $dependencies = {
319 map { $_ => _resolve_deps ($_, \%tables) } (keys %tables)
320 };
9485509b 321
48850f9a 322 for my $table (sort
323 {
324 keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} }
325 ||
326 $a cmp $b
327 }
328 (keys %tables)
329 ) {
330 $schema->add_table ($tables{$table}{object});
331 $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} );
48850f9a 332
7adc2091 333 # the hook might have already removed the table
334 if ($schema->get_table($table) && $table =~ /^ \s* \( \s* SELECT \s+/ix) {
5b9ecfcc 335 carp <<'EOW';
a8fcfb9f 336
7adc2091 337Custom SQL through ->name(\'( SELECT ...') is DEPRECATED, for more details see
338"Arbitrary SQL through a custom ResultSource" in DBIx::Class::Manual::Cookbook
339or http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod
39be4120 340
7adc2091 341EOW
39be4120 342
7adc2091 343 # remove the table as there is no way someone might want to
344 # actually deploy this
345 $schema->drop_table ($table);
39be4120 346 }
347 }
48850f9a 348
349 my %views;
51b31bbe 350 my @views = map { $dbicschema->source($_) } keys %view_monikers;
9485509b 351
c418c5cc 352 my $view_dependencies = {
353 map {
a8cbaf38 354 $_ => _resolve_deps( $dbicschema->source($_), \%view_monikers )
c418c5cc 355 } ( keys %view_monikers )
356 };
51b31bbe 357
358 my @view_sources =
359 sort {
c418c5cc 360 keys %{ $view_dependencies->{ $a->source_name } || {} } <=>
361 keys %{ $view_dependencies->{ $b->source_name } || {} }
51b31bbe 362 || $a->source_name cmp $b->source_name
363 }
364 map { $dbicschema->source($_) }
365 keys %view_monikers;
1999a918 366
943538a0 367 foreach my $source (@view_sources)
1d521afd 368 {
48850f9a 369 my $view_name = $source->name;
4678e9da 370
371 # FIXME - this isn't the right way to do it, but sqlt does not
372 # support quoting properly to be signaled about this
373 $view_name = $$view_name if ref $view_name eq 'SCALAR';
48850f9a 374
1d521afd 375 # Skip custom query sources
48850f9a 376 next if ref $view_name;
1d521afd 377
378 # Its possible to have multiple DBIC source using same table
48850f9a 379 next if $views{$view_name}++;
1d521afd 380
ab7e74aa 381 $dbicschema->throw_exception ("view $view_name is missing a view_definition")
382 unless $source->view_definition;
8f1617e2 383
48850f9a 384 my $view = $schema->add_view (
385 name => $view_name,
1d521afd 386 fields => [ $source->columns ],
f534e33b 387 $source->view_definition ? ( 'sql' => $source->view_definition ) : ()
eb0bc670 388 ) || $dbicschema->throw_exception ($schema->error);
a99b214b 389
390 $source->_invoke_sqlt_deploy_hook($view);
1d521afd 391 }
392
48850f9a 393
b7e303a8 394 if ($dbicschema->can('sqlt_deploy_hook')) {
395 $dbicschema->sqlt_deploy_hook($schema);
d6c79cb3 396 }
397
637ca936 398 return 1;
75d07914 399}
b02b20b5 400
48850f9a 401#
402# Quick and dirty dependency graph calculator
403#
404sub _resolve_deps {
a8cbaf38 405 my ( $question, $answers, $seen ) = @_;
c418c5cc 406 my $ret = {};
51b31bbe 407 $seen ||= {};
a8cbaf38 408 my @deps;
51b31bbe 409
410 # copy and bump all deps by one (so we can reconstruct the chain)
411 my %seen = map { $_ => $seen->{$_} + 1 } ( keys %$seen );
6c97a902 412 if ( blessed($question)
413 && $question->isa('DBIx::Class::ResultSource::View') )
414 {
a8cbaf38 415 $seen{ $question->result_class } = 1;
416 @deps = keys %{ $question->{deploy_depends_on} };
417 }
418 else {
419 $seen{$question} = 1;
420 @deps = keys %{ $answers->{$question}{foreign_table_deps} };
421 }
422
423 for my $dep (@deps) {
51b31bbe 424 if ( $seen->{$dep} ) {
425 return {};
426 }
a8cbaf38 427 my $next_dep;
51b31bbe 428
6c97a902 429 if ( blessed($question)
430 && $question->isa('DBIx::Class::ResultSource::View') )
431 {
432 no warnings 'uninitialized';
a8cbaf38 433 my ($next_dep_source_name) =
6c97a902 434 grep {
435 $question->schema->source($_)->result_class eq $dep
436 && !( $question->schema->source($_)
437 ->isa('DBIx::Class::ResultSource::Table') )
438 } @{ [ $question->schema->sources ] };
439 return {} unless $next_dep_source_name;
a8cbaf38 440 $next_dep = $question->schema->source($next_dep_source_name);
441 }
442 else {
443 $next_dep = $dep;
444 }
445 my $subdeps = _resolve_deps( $next_dep, $answers, \%seen );
446 $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps );
51b31bbe 447 ++$ret->{$dep};
448 }
449 return $ret;
450}
451
0da8b7da 4521;
7232ce07 453
454=head1 NAME
455
456SQL::Translator::Parser::DBIx::Class - Create a SQL::Translator schema
457from a DBIx::Class::Schema instance
458
459=head1 SYNOPSIS
460
f26d4b95 461 ## Via DBIx::Class
462 use MyApp::Schema;
463 my $schema = MyApp::Schema->connect("dbi:SQLite:something.db");
464 $schema->create_ddl_dir();
465 ## or
466 $schema->deploy();
467
468 ## Standalone
7232ce07 469 use MyApp::Schema;
470 use SQL::Translator;
d4daee7b 471
7232ce07 472 my $schema = MyApp::Schema->connect;
473 my $trans = SQL::Translator->new (
474 parser => 'SQL::Translator::Parser::DBIx::Class',
37cb0de1 475 parser_args => {
a07ae2aa 476 dbic_schema => $schema,
cae467a7 477 add_fk_index => 0,
37cb0de1 478 sources => [qw/
479 Artist
480 CD
481 /],
482 },
7232ce07 483 producer => 'SQLite',
484 ) or die SQL::Translator->error;
485 my $out = $trans->translate() or die $trans->error;
486
487=head1 DESCRIPTION
488
f26d4b95 489This class requires L<SQL::Translator> installed to work.
490
7232ce07 491C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema,
492interrogates the columns, and stuffs it all in an $sqlt_schema object.
493
faaba25f 494Its primary use is in deploying database layouts described as a set
db2b2eb6 495of L<DBIx::Class> classes, to a database. To do this, see
496L<DBIx::Class::Schema/deploy>.
f26d4b95 497
498This can also be achieved by having DBIx::Class export the schema as a
499set of SQL files ready for import into your database, or passed to
500other machines that need to have your application installed but don't
db2b2eb6 501have SQL::Translator installed. To do this see
502L<DBIx::Class::Schema/create_ddl_dir>.
f26d4b95 503
cae467a7 504=head1 PARSER OPTIONS
505
a07ae2aa 506=head2 dbic_schema
507
508The DBIx::Class schema (either an instance or a class name) to be parsed.
509This argument is in fact optional - instead one can supply it later at
510translation time as an argument to L<SQL::Translator/translate>. In
511other words both of the following invocations are valid and will produce
512conceptually identical output:
513
514 my $yaml = SQL::Translator->new(
515 parser => 'SQL::Translator::Parser::DBIx::Class',
516 parser_args => {
517 dbic_schema => $schema,
518 },
519 producer => 'SQL::Translator::Producer::YAML',
520 )->translate;
521
522 my $yaml = SQL::Translator->new(
523 parser => 'SQL::Translator::Parser::DBIx::Class',
524 producer => 'SQL::Translator::Producer::YAML',
525 )->translate(data => $schema);
526
cae467a7 527=head2 add_fk_index
528
529Create an index for each foreign key.
ad91ed74 530Enabled by default, as having indexed foreign key columns is normally the
531sensible thing to do.
cae467a7 532
533=head2 sources
534
535=over 4
536
f0ac764e 537=item Arguments: \@class_names
cae467a7 538
539=back
ad91ed74 540
541Limit the amount of parsed sources by supplying an explicit list of source names.
cae467a7 542
7232ce07 543=head1 SEE ALSO
544
f26d4b95 545L<SQL::Translator>, L<DBIx::Class::Schema>
7232ce07 546
a2bd3796 547=head1 FURTHER QUESTIONS?
7232ce07 548
a2bd3796 549Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
7232ce07 550
a2bd3796 551=head1 COPYRIGHT AND LICENSE
7232ce07 552
a2bd3796 553This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
554by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
555redistribute it and/or modify it under the same terms as the
556L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.