added branch no_duplicate_indexes_for_pk_cols with test and fix
[dbsrgits/DBIx-Class.git] / lib / SQL / Translator / Parser / DBIx / Class.pm
1 package SQL::Translator::Parser::DBIx::Class;
2
3 # AUTHOR: Jess Robinson
4
5 # Some mistakes the fault of Matt S Trout
6
7 # Others the fault of Ash Berlin
8
9 use strict;
10 use warnings;
11 use vars qw($DEBUG $VERSION @EXPORT_OK);
12 $VERSION = '1.10';
13 $DEBUG = 0 unless defined $DEBUG;
14
15 use Exporter;
16 use SQL::Translator::Utils qw(debug normalize_name);
17 use Carp::Clan qw/^SQL::Translator|^DBIx::Class/;
18 use Scalar::Util ();
19
20 use base qw(Exporter);
21
22 @EXPORT_OK = qw(parse);
23
24 # -------------------------------------------------------------------
25 # parse($tr, $data)
26 #
27 # setting parser_args => { add_fk_index => 0 } will prevent
28 # the auto-generation of an index for each FK.
29 #
30 # Note that $data, in the case of this parser, is not useful.
31 # We're working with DBIx::Class Schemas, not data streams.
32 # -------------------------------------------------------------------
33 sub parse {
34     # this is a hack to prevent schema leaks due to a retarded SQLT implementation
35     # DO NOT REMOVE (until SQLT2 is out, the all of this will be rewritten anyway)
36     Scalar::Util::weaken ($_[1]);
37
38     my ($tr, $data)   = @_;
39     my $args          = $tr->parser_args;
40     my $dbicschema    = $args->{'DBIx::Class::Schema'} ||  $args->{"DBIx::Schema"} ||$data;
41     $dbicschema     ||= $args->{'package'};
42     my $limit_sources = $args->{'sources'};
43
44     croak 'No DBIx::Class::Schema' unless ($dbicschema);
45     if (!ref $dbicschema) {
46       eval "use $dbicschema;";
47       croak "Can't load $dbicschema ($@)" if($@);
48     }
49
50     my $schema      = $tr->schema;
51     my $table_no    = 0;
52
53     $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x'))
54       unless ($schema->name);
55
56     my @monikers = sort $dbicschema->sources;
57     if ($limit_sources) {
58         my $ref = ref $limit_sources || '';
59         $dbicschema->throw_exception ("'sources' parameter must be an array or hash ref")
60           unless( $ref eq 'ARRAY' || ref eq 'HASH' );
61
62         # limit monikers to those specified in 
63         my $sources;
64         if ($ref eq 'ARRAY') {
65             $sources->{$_} = 1 for (@$limit_sources);
66         } else {
67             $sources = $limit_sources;
68         }
69         @monikers = grep { $sources->{$_} } @monikers;
70     }
71
72
73     my(%table_monikers, %view_monikers);
74     for my $moniker (@monikers){
75       my $source = $dbicschema->source($moniker);
76        if ( $source->isa('DBIx::Class::ResultSource::Table') ) {
77          $table_monikers{$moniker}++;
78       } elsif( $source->isa('DBIx::Class::ResultSource::View') ){
79           next if $source->is_virtual;
80          $view_monikers{$moniker}++;
81       }
82     }
83
84     my %tables;
85     foreach my $moniker (sort keys %table_monikers)
86     {
87         my $source = $dbicschema->source($moniker);
88         my $table_name = $source->name;
89
90         # FIXME - this isn't the right way to do it, but sqlt does not
91         # support quoting properly to be signaled about this
92         $table_name = $$table_name if ref $table_name eq 'SCALAR';
93
94         # It's possible to have multiple DBIC sources using the same table
95         next if $tables{$table_name};
96
97         $tables{$table_name}{source} = $source;
98         my $table = $tables{$table_name}{object} = SQL::Translator::Schema::Table->new(
99                                        name => $table_name,
100                                        type => 'TABLE',
101                                        );
102         foreach my $col ($source->columns)
103         {
104             # assuming column_info in dbic is the same as DBI (?)
105             # data_type is a number, column_type is text?
106             my %colinfo = (
107               name => $col,
108               size => 0,
109               is_auto_increment => 0,
110               is_foreign_key => 0,
111               is_nullable => 0,
112               %{$source->column_info($col)}
113             );
114             if ($colinfo{is_nullable}) {
115               $colinfo{default} = '' unless exists $colinfo{default};
116             }
117             my $f = $table->add_field(%colinfo)
118               || $dbicschema->throw_exception ($table->error);
119         }
120
121         my @primary = $source->primary_columns;
122
123         $table->primary_key(@primary) if @primary;
124
125         my %unique_constraints = $source->unique_constraints;
126         foreach my $uniq (sort keys %unique_constraints) {
127             if (!$source->_compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
128                 $table->add_constraint(
129                             type             => 'unique',
130                             name             => $uniq,
131                             fields           => $unique_constraints{$uniq}
132                 );
133             }
134         }
135
136         my @rels = $source->relationships();
137
138         my %created_FK_rels;
139
140         # global add_fk_index set in parser_args
141         my $add_fk_index = (exists $args->{add_fk_index} && ! $args->{add_fk_index}) ? 0 : 1;
142
143         foreach my $rel (sort @rels)
144         {
145
146             my $rel_info = $source->relationship_info($rel);
147
148             # Ignore any rel cond that isn't a straight hash
149             next unless ref $rel_info->{cond} eq 'HASH';
150
151             my $relsource = $source->related_source($rel);
152
153             # related sources might be excluded via a {sources} filter or might be views
154             next unless exists $table_monikers{$relsource->source_name};
155
156             my $rel_table = $relsource->name;
157
158             # FIXME - this isn't the right way to do it, but sqlt does not
159             # support quoting properly to be signaled about this
160             $rel_table = $$rel_table if ref $rel_table eq 'SCALAR';
161
162             my $reverse_rels = $source->reverse_relationship_info($rel);
163             my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
164
165             # Force the order of @cond to match the order of ->add_columns
166             my $idx;
167             my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $relsource->columns;
168             my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); 
169
170             # Get the key information, mapping off the foreign/self markers
171             my @refkeys = map {/^\w+\.(\w+)$/} @cond;
172             my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
173
174             # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to)
175             my $fk_constraint;
176
177             #first it can be specified explicitly
178             if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) {
179                 $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint};
180             }
181             # it can not be multi
182             elsif ( $rel_info->{attrs}{accessor}
183                     && $rel_info->{attrs}{accessor} eq 'multi' ) {
184                 $fk_constraint = 0;
185             }
186             # if indeed single, check if all self.columns are our primary keys.
187             # this is supposed to indicate a has_one/might_have...
188             # where's the introspection!!?? :)
189             else {
190                 $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary);
191             }
192
193             my $cascade;
194             for my $c (qw/delete update/) {
195                 if (exists $rel_info->{attrs}{"on_$c"}) {
196                     if ($fk_constraint) {
197                         $cascade->{$c} = $rel_info->{attrs}{"on_$c"};
198                     }
199                     elsif ( $rel_info->{attrs}{"on_$c"} ) {
200                         carp "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. "
201                             . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n";
202                     }
203                 }
204                 elsif (defined $otherrelationship and $otherrelationship->{attrs}{$c eq 'update' ? 'cascade_copy' : 'cascade_delete'}) {
205                     $cascade->{$c} = 'CASCADE';
206                 }
207             }
208
209             if($rel_table)
210             {
211                 # Constraints are added only if applicable
212                 next unless $fk_constraint;
213                 
214                 # Make sure we dont create the same foreign key constraint twice
215                 my $key_test = join("\x00", sort @keys);
216                 next if $created_FK_rels{$rel_table}->{$key_test};
217                 
218                 # Make sure we dont create additional indexes for the
219                 # primary columns
220                 my $pk_test = join("\x00", sort @primary);
221                 next if $key_test eq $pk_test;
222
223                 if (scalar(@keys)) {
224
225                   $created_FK_rels{$rel_table}->{$key_test} = 1;
226
227                   my $is_deferrable = $rel_info->{attrs}{is_deferrable};
228
229                   # calculate dependencies: do not consider deferrable constraints and
230                   # self-references for dependency calculations
231                   if (! $is_deferrable and $rel_table ne $table_name) {
232                     $tables{$table_name}{foreign_table_deps}{$rel_table}++;
233                   }
234
235                   $table->add_constraint(
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 ) : ()),
244                   );
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
249                   if ($add_fk_index_rel) {
250                       my $index = $table->add_index(
251                                                     name   => join('_', $table_name, 'idx', @keys),
252                                                     fields => \@keys,
253                                                     type   => 'NORMAL',
254                                                     );
255                   }
256               }
257             }
258         }
259
260     }
261
262     # attach the tables to the schema in dependency order
263     my $dependencies = {
264       map { $_ => _resolve_deps ($_, \%tables) } (keys %tables)
265     };
266     for my $table (sort
267       {
268         keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} }
269           ||
270         $a cmp $b
271       }
272       (keys %tables)
273     ) {
274       $schema->add_table ($tables{$table}{object});
275       $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} );
276
277       # the hook might have already removed the table
278       if ($schema->get_table($table) && $table =~ /^ \s* \( \s* SELECT \s+/ix) {
279         warn <<'EOW';
280
281 Custom SQL through ->name(\'( SELECT ...') is DEPRECATED, for more details see
282 "Arbitrary SQL through a custom ResultSource" in DBIx::Class::Manual::Cookbook
283 or http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod
284
285 EOW
286
287         # remove the table as there is no way someone might want to
288         # actually deploy this
289         $schema->drop_table ($table);
290       }
291     }
292
293     my %views;
294     foreach my $moniker (sort keys %view_monikers)
295     {
296         my $source = $dbicschema->source($moniker);
297         my $view_name = $source->name;
298
299         # FIXME - this isn't the right way to do it, but sqlt does not
300         # support quoting properly to be signaled about this
301         $view_name = $$view_name if ref $view_name eq 'SCALAR';
302
303         # Skip custom query sources
304         next if ref $view_name;
305
306         # Its possible to have multiple DBIC source using same table
307         next if $views{$view_name}++;
308
309         $dbicschema->throw_exception ("view $view_name is missing a view_definition")
310             unless $source->view_definition;
311
312         my $view = $schema->add_view (
313           name => $view_name,
314           fields => [ $source->columns ],
315           $source->view_definition ? ( 'sql' => $source->view_definition ) : ()
316         ) || $dbicschema->throw_exception ($schema->error);
317
318         $source->_invoke_sqlt_deploy_hook($view);
319     }
320
321
322     if ($dbicschema->can('sqlt_deploy_hook')) {
323       $dbicschema->sqlt_deploy_hook($schema);
324     }
325
326     return 1;
327 }
328
329 #
330 # Quick and dirty dependency graph calculator
331 #
332 sub _resolve_deps {
333   my ($table, $tables, $seen) = @_;
334
335   my $ret = {};
336   $seen ||= {};
337
338   # copy and bump all deps by one (so we can reconstruct the chain)
339   my %seen = map { $_ => $seen->{$_} + 1 } (keys %$seen);
340   $seen{$table} = 1;
341
342   for my $dep (keys %{$tables->{$table}{foreign_table_deps}} ) {
343
344     if ($seen->{$dep}) {
345
346       # warn and remove the circular constraint so we don't get flooded with the same warning over and over
347       #carp sprintf ("Circular dependency detected, schema may not be deployable:\n%s\n",
348       #  join (' -> ', (sort { $seen->{$b} <=> $seen->{$a} } (keys %$seen) ), $table, $dep )
349       #);
350       #delete $tables->{$table}{foreign_table_deps}{$dep};
351
352       return {};
353     }
354
355     my $subdeps = _resolve_deps ($dep, $tables, \%seen);
356     $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps );
357
358     ++$ret->{$dep};
359   }
360
361   return $ret;
362 }
363
364 1;
365
366 =head1 NAME
367
368 SQL::Translator::Parser::DBIx::Class - Create a SQL::Translator schema
369 from a DBIx::Class::Schema instance
370
371 =head1 SYNOPSIS
372
373  ## Via DBIx::Class
374  use MyApp::Schema;
375  my $schema = MyApp::Schema->connect("dbi:SQLite:something.db");
376  $schema->create_ddl_dir();
377  ## or
378  $schema->deploy();
379
380  ## Standalone
381  use MyApp::Schema;
382  use SQL::Translator;
383
384  my $schema = MyApp::Schema->connect;
385  my $trans  = SQL::Translator->new (
386       parser      => 'SQL::Translator::Parser::DBIx::Class',
387       parser_args => {
388           package => $schema,
389           # to explicitly specify which ResultSources are to be parsed
390           sources => [qw/
391             Artist
392             CD
393           /],
394       },
395       producer    => 'SQLite',
396      ) or die SQL::Translator->error;
397  my $out = $trans->translate() or die $trans->error;
398
399 =head1 DESCRIPTION
400
401 This class requires L<SQL::Translator> installed to work.
402
403 C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema,
404 interrogates the columns, and stuffs it all in an $sqlt_schema object.
405
406 Its primary use is in deploying database layouts described as a set
407 of L<DBIx::Class> classes, to a database. To do this, see
408 L<DBIx::Class::Schema/deploy>.
409
410 This can also be achieved by having DBIx::Class export the schema as a
411 set of SQL files ready for import into your database, or passed to
412 other machines that need to have your application installed but don't
413 have SQL::Translator installed. To do this see
414 L<DBIx::Class::Schema/create_ddl_dir>.
415
416 =head1 SEE ALSO
417
418 L<SQL::Translator>, L<DBIx::Class::Schema>
419
420 =head1 AUTHORS
421
422 Jess Robinson
423
424 Matt S Trout
425
426 Ash Berlin