don't sort the key columns because the order of columns is important for indexes
[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;
b02b20b5 14
15use Exporter;
b02b20b5 16use SQL::Translator::Utils qw(debug normalize_name);
48850f9a 17use Carp::Clan qw/^SQL::Translator|^DBIx::Class/;
3bf702eb 18use Scalar::Util ();
b02b20b5 19
20use base qw(Exporter);
21
22@EXPORT_OK = qw(parse);
23
24# -------------------------------------------------------------------
25# parse($tr, $data)
26#
0e2c6809 27# setting parser_args => { add_fk_index => 0 } will prevent
28# the auto-generation of an index for each FK.
29#
880a1a0c 30# Note that $data, in the case of this parser, is not useful.
b02b20b5 31# We're working with DBIx::Class Schemas, not data streams.
32# -------------------------------------------------------------------
33sub parse {
3bf702eb 34 # this is a hack to prevent schema leaks due to a retarded SQLT implementation
2ad89bf5 35 # DO NOT REMOVE (until SQLT2 is out, the all of this will be rewritten anyway)
3bf702eb 36 Scalar::Util::weaken ($_[1]);
37
499adf63 38 my ($tr, $data) = @_;
39 my $args = $tr->parser_args;
b7e303a8 40 my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data;
41 $dbicschema ||= $args->{'package'};
499adf63 42 my $limit_sources = $args->{'sources'};
48850f9a 43
eb0bc670 44 croak 'No DBIx::Class::Schema' unless ($dbicschema);
b7e303a8 45 if (!ref $dbicschema) {
46 eval "use $dbicschema;";
eb0bc670 47 croak "Can't load $dbicschema ($@)" if($@);
b02b20b5 48 }
49
50 my $schema = $tr->schema;
51 my $table_no = 0;
52
b1f9d92e 53 $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x'))
b7e303a8 54 unless ($schema->name);
38e48163 55
b7e303a8 56 my @monikers = sort $dbicschema->sources;
499adf63 57 if ($limit_sources) {
58 my $ref = ref $limit_sources || '';
eb0bc670 59 $dbicschema->throw_exception ("'sources' parameter must be an array or hash ref")
60 unless( $ref eq 'ARRAY' || ref eq 'HASH' );
499adf63 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
a7f4b74c 73 my(%table_monikers, %view_monikers);
1d521afd 74 for my $moniker (@monikers){
75 my $source = $dbicschema->source($moniker);
5f5e87cf 76 if ( $source->isa('DBIx::Class::ResultSource::Table') ) {
a7f4b74c 77 $table_monikers{$moniker}++;
5f5e87cf 78 } elsif( $source->isa('DBIx::Class::ResultSource::View') ){
bccd177f 79 next if $source->is_virtual;
a7f4b74c 80 $view_monikers{$moniker}++;
1d521afd 81 }
82 }
499adf63 83
48850f9a 84 my %tables;
a7f4b74c 85 foreach my $moniker (sort keys %table_monikers)
b02b20b5 86 {
b7e303a8 87 my $source = $dbicschema->source($moniker);
48850f9a 88 my $table_name = $source->name;
39be4120 89
4678e9da 90 # FIXME - this isn't the right way to do it, but sqlt does not
91 # support quoting properly to be signaled about this
a8fcfb9f 92 $table_name = $$table_name if ref $table_name eq 'SCALAR';
b02b20b5 93
e3d5a547 94 # It's possible to have multiple DBIC sources using the same table
48850f9a 95 next if $tables{$table_name};
38e48163 96
48850f9a 97 $tables{$table_name}{source} = $source;
98 my $table = $tables{$table_name}{object} = SQL::Translator::Schema::Table->new(
99 name => $table_name,
b02b20b5 100 type => 'TABLE',
48850f9a 101 );
b02b20b5 102 foreach my $col ($source->columns)
103 {
d6c79cb3 104 # assuming column_info in dbic is the same as DBI (?)
b02b20b5 105 # data_type is a number, column_type is text?
106 my %colinfo = (
107 name => $col,
b02b20b5 108 size => 0,
109 is_auto_increment => 0,
110 is_foreign_key => 0,
111 is_nullable => 0,
112 %{$source->column_info($col)}
113 );
0009fa49 114 if ($colinfo{is_nullable}) {
115 $colinfo{default} = '' unless exists $colinfo{default};
116 }
eb0bc670 117 my $f = $table->add_field(%colinfo)
118 || $dbicschema->throw_exception ($table->error);
b02b20b5 119 }
b02b20b5 120
7b90bb13 121 my @primary = $source->primary_columns;
36e9e24f 122
123 $table->primary_key(@primary) if @primary;
124
7b90bb13 125 my %unique_constraints = $source->unique_constraints;
b7e303a8 126 foreach my $uniq (sort keys %unique_constraints) {
6d0ee587 127 if (!$source->_compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 128 $table->add_constraint(
129 type => 'unique',
a7e65bb5 130 name => $uniq,
7b90bb13 131 fields => $unique_constraints{$uniq}
132 );
133 }
134 }
135
b02b20b5 136 my @rels = $source->relationships();
499adf63 137
138 my %created_FK_rels;
48850f9a 139
2581038c 140 # global add_fk_index set in parser_args
a7f4b74c 141 my $add_fk_index = (exists $args->{add_fk_index} && ! $args->{add_fk_index}) ? 0 : 1;
499adf63 142
454a5a42 143 foreach my $rel (sort @rels)
b02b20b5 144 {
a7f4b74c 145
b02b20b5 146 my $rel_info = $source->relationship_info($rel);
637ca936 147
637ca936 148 # Ignore any rel cond that isn't a straight hash
149 next unless ref $rel_info->{cond} eq 'HASH';
150
a7f4b74c 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;
4678e9da 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';
de60a93d 161
e377d723 162 my $reverse_rels = $source->reverse_relationship_info($rel);
163 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
164
d1b264d3 165 # Force the order of @cond to match the order of ->add_columns
166 my $idx;
a7f4b74c 167 my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $relsource->columns;
d1b264d3 168 my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}});
48850f9a 169
637ca936 170 # Get the key information, mapping off the foreign/self markers
637ca936 171 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
172 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
173
e377d723 174 # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to)
175 my $fk_constraint;
de60a93d 176
e377d723 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
069e49b6 182 elsif ( $rel_info->{attrs}{accessor}
183 && $rel_info->{attrs}{accessor} eq 'multi' ) {
e377d723 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 {
6d0ee587 190 $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary);
e377d723 191 }
de60a93d 192
e377d723 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 }
db79c254 199 elsif ( $rel_info->{attrs}{"on_$c"} ) {
48850f9a 200 carp "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. "
e377d723 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';
de60a93d 206 }
e377d723 207 }
208
209 if($rel_table)
210 {
211 # Constraints are added only if applicable
212 next unless $fk_constraint;
620df7e8 213
e377d723 214 # Make sure we dont create the same foreign key constraint twice
e3d5a547 215 my $key_test = join("\x00", sort @keys);
e377d723 216 next if $created_FK_rels{$rel_table}->{$key_test};
620df7e8 217
48850f9a 218 if (scalar(@keys)) {
219
220 $created_FK_rels{$rel_table}->{$key_test} = 1;
13de943d 221
48850f9a 222 my $is_deferrable = $rel_info->{attrs}{is_deferrable};
223
a7f4b74c 224 # calculate dependencies: do not consider deferrable constraints and
225 # self-references for dependency calculations
48850f9a 226 if (! $is_deferrable and $rel_table ne $table_name) {
227 $tables{$table_name}{foreign_table_deps}{$rel_table}++;
228 }
a7f4b74c 229
7b5d0b84 230 $table->add_constraint(
0d865134 231 type => 'foreign_key',
48850f9a 232 name => join('_', $table_name, 'fk', @keys),
0d865134 233 fields => \@keys,
234 reference_fields => \@refkeys,
235 reference_table => $rel_table,
555b3385 236 on_delete => uc ($cascade->{delete} || ''),
237 on_update => uc ($cascade->{update} || ''),
e394339b 238 (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
7b5d0b84 239 );
48850f9a 240
241 # global parser_args add_fk_index param can be overridden on the rel def
242 my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index;
5930bfc2 243 # Make sure we don't create another index with the same
244 # order of columns twice
245
246 # WARNING: don't sort the key columns because the order of
247 # columns is important for indexes and two indexes with the
248 # same cols but different order are allowed and sometimes
249 # needed
250 my $key_idx_test = join("\x00", @keys);
251 my $pk_idx_test = join("\x00", @primary);
252 next if $key_idx_test eq $pk_idx_test;
48850f9a 253
2581038c 254 if ($add_fk_index_rel) {
0e2c6809 255 my $index = $table->add_index(
5930bfc2 256 name => join('_', $table_name, 'idx', @keys),
257 fields => \@keys,
258 type => 'NORMAL',
259 );
0e2c6809 260 }
261 }
b02b20b5 262 }
263 }
48850f9a 264
b02b20b5 265 }
d6c79cb3 266
48850f9a 267 # attach the tables to the schema in dependency order
268 my $dependencies = {
269 map { $_ => _resolve_deps ($_, \%tables) } (keys %tables)
270 };
271 for my $table (sort
272 {
273 keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} }
274 ||
275 $a cmp $b
276 }
277 (keys %tables)
278 ) {
279 $schema->add_table ($tables{$table}{object});
280 $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} );
48850f9a 281
7adc2091 282 # the hook might have already removed the table
283 if ($schema->get_table($table) && $table =~ /^ \s* \( \s* SELECT \s+/ix) {
284 warn <<'EOW';
a8fcfb9f 285
7adc2091 286Custom SQL through ->name(\'( SELECT ...') is DEPRECATED, for more details see
287"Arbitrary SQL through a custom ResultSource" in DBIx::Class::Manual::Cookbook
288or http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod
39be4120 289
7adc2091 290EOW
39be4120 291
7adc2091 292 # remove the table as there is no way someone might want to
293 # actually deploy this
294 $schema->drop_table ($table);
39be4120 295 }
296 }
48850f9a 297
298 my %views;
a7f4b74c 299 foreach my $moniker (sort keys %view_monikers)
1d521afd 300 {
301 my $source = $dbicschema->source($moniker);
48850f9a 302 my $view_name = $source->name;
4678e9da 303
304 # FIXME - this isn't the right way to do it, but sqlt does not
305 # support quoting properly to be signaled about this
306 $view_name = $$view_name if ref $view_name eq 'SCALAR';
48850f9a 307
1d521afd 308 # Skip custom query sources
48850f9a 309 next if ref $view_name;
1d521afd 310
311 # Its possible to have multiple DBIC source using same table
48850f9a 312 next if $views{$view_name}++;
1d521afd 313
ab7e74aa 314 $dbicschema->throw_exception ("view $view_name is missing a view_definition")
315 unless $source->view_definition;
8f1617e2 316
48850f9a 317 my $view = $schema->add_view (
318 name => $view_name,
1d521afd 319 fields => [ $source->columns ],
f534e33b 320 $source->view_definition ? ( 'sql' => $source->view_definition ) : ()
eb0bc670 321 ) || $dbicschema->throw_exception ($schema->error);
a99b214b 322
323 $source->_invoke_sqlt_deploy_hook($view);
1d521afd 324 }
325
48850f9a 326
b7e303a8 327 if ($dbicschema->can('sqlt_deploy_hook')) {
328 $dbicschema->sqlt_deploy_hook($schema);
d6c79cb3 329 }
330
637ca936 331 return 1;
75d07914 332}
b02b20b5 333
48850f9a 334#
335# Quick and dirty dependency graph calculator
336#
337sub _resolve_deps {
338 my ($table, $tables, $seen) = @_;
339
340 my $ret = {};
341 $seen ||= {};
342
343 # copy and bump all deps by one (so we can reconstruct the chain)
344 my %seen = map { $_ => $seen->{$_} + 1 } (keys %$seen);
345 $seen{$table} = 1;
346
347 for my $dep (keys %{$tables->{$table}{foreign_table_deps}} ) {
348
349 if ($seen->{$dep}) {
350
351 # warn and remove the circular constraint so we don't get flooded with the same warning over and over
352 #carp sprintf ("Circular dependency detected, schema may not be deployable:\n%s\n",
353 # join (' -> ', (sort { $seen->{$b} <=> $seen->{$a} } (keys %$seen) ), $table, $dep )
354 #);
355 #delete $tables->{$table}{foreign_table_deps}{$dep};
356
357 return {};
358 }
359
360 my $subdeps = _resolve_deps ($dep, $tables, \%seen);
361 $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps );
362
363 ++$ret->{$dep};
364 }
365
366 return $ret;
367}
368
0da8b7da 3691;
7232ce07 370
371=head1 NAME
372
373SQL::Translator::Parser::DBIx::Class - Create a SQL::Translator schema
374from a DBIx::Class::Schema instance
375
376=head1 SYNOPSIS
377
f26d4b95 378 ## Via DBIx::Class
379 use MyApp::Schema;
380 my $schema = MyApp::Schema->connect("dbi:SQLite:something.db");
381 $schema->create_ddl_dir();
382 ## or
383 $schema->deploy();
384
385 ## Standalone
7232ce07 386 use MyApp::Schema;
387 use SQL::Translator;
d4daee7b 388
7232ce07 389 my $schema = MyApp::Schema->connect;
390 my $trans = SQL::Translator->new (
391 parser => 'SQL::Translator::Parser::DBIx::Class',
37cb0de1 392 parser_args => {
393 package => $schema,
394 # to explicitly specify which ResultSources are to be parsed
395 sources => [qw/
396 Artist
397 CD
398 /],
399 },
7232ce07 400 producer => 'SQLite',
401 ) or die SQL::Translator->error;
402 my $out = $trans->translate() or die $trans->error;
403
404=head1 DESCRIPTION
405
f26d4b95 406This class requires L<SQL::Translator> installed to work.
407
7232ce07 408C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema,
409interrogates the columns, and stuffs it all in an $sqlt_schema object.
410
faaba25f 411Its primary use is in deploying database layouts described as a set
db2b2eb6 412of L<DBIx::Class> classes, to a database. To do this, see
413L<DBIx::Class::Schema/deploy>.
f26d4b95 414
415This can also be achieved by having DBIx::Class export the schema as a
416set of SQL files ready for import into your database, or passed to
417other machines that need to have your application installed but don't
db2b2eb6 418have SQL::Translator installed. To do this see
419L<DBIx::Class::Schema/create_ddl_dir>.
f26d4b95 420
7232ce07 421=head1 SEE ALSO
422
f26d4b95 423L<SQL::Translator>, L<DBIx::Class::Schema>
7232ce07 424
425=head1 AUTHORS
426
427Jess Robinson
428
429Matt S Trout
430
431Ash Berlin