Adjust sqlt schema parser to add tables in FK dependency order
[dbsrgits/DBIx-Class-Historic.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/;
b02b20b5 18
19use 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# -------------------------------------------------------------------
32sub parse {
499adf63 33 my ($tr, $data) = @_;
34 my $args = $tr->parser_args;
b7e303a8 35 my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data;
36 $dbicschema ||= $args->{'package'};
499adf63 37 my $limit_sources = $args->{'sources'};
48850f9a 38
b7e303a8 39 die 'No DBIx::Class::Schema' unless ($dbicschema);
40 if (!ref $dbicschema) {
41 eval "use $dbicschema;";
42 die "Can't load $dbicschema ($@)" if($@);
b02b20b5 43 }
44
45 my $schema = $tr->schema;
46 my $table_no = 0;
47
b1f9d92e 48 $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x'))
b7e303a8 49 unless ($schema->name);
38e48163 50
b7e303a8 51 my @monikers = sort $dbicschema->sources;
499adf63 52 if ($limit_sources) {
53 my $ref = ref $limit_sources || '';
5bdf1b3d 54 die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH';
499adf63 55
56 # limit monikers to those specified in
57 my $sources;
58 if ($ref eq 'ARRAY') {
59 $sources->{$_} = 1 for (@$limit_sources);
60 } else {
61 $sources = $limit_sources;
62 }
63 @monikers = grep { $sources->{$_} } @monikers;
64 }
65
66
1d521afd 67 my(@table_monikers, @view_monikers);
68 for my $moniker (@monikers){
69 my $source = $dbicschema->source($moniker);
5f5e87cf 70 if ( $source->isa('DBIx::Class::ResultSource::Table') ) {
1d521afd 71 push(@table_monikers, $moniker);
5f5e87cf 72 } elsif( $source->isa('DBIx::Class::ResultSource::View') ){
bccd177f 73 next if $source->is_virtual;
1d521afd 74 push(@view_monikers, $moniker);
75 }
76 }
499adf63 77
48850f9a 78 my %tables;
1d521afd 79 foreach my $moniker (sort @table_monikers)
b02b20b5 80 {
b7e303a8 81 my $source = $dbicschema->source($moniker);
48850f9a 82 my $table_name = $source->name;
83
90c11f68 84 # Skip custom query sources
48850f9a 85 next if ref $table_name;
b02b20b5 86
48850f9a 87 # Its possible to have multiple DBIC sources using the same table
88 next if $tables{$table_name};
38e48163 89
48850f9a 90 $tables{$table_name}{source} = $source;
91 my $table = $tables{$table_name}{object} = SQL::Translator::Schema::Table->new(
92 name => $table_name,
b02b20b5 93 type => 'TABLE',
48850f9a 94 );
b02b20b5 95 foreach my $col ($source->columns)
96 {
d6c79cb3 97 # assuming column_info in dbic is the same as DBI (?)
b02b20b5 98 # data_type is a number, column_type is text?
99 my %colinfo = (
100 name => $col,
b02b20b5 101 size => 0,
102 is_auto_increment => 0,
103 is_foreign_key => 0,
104 is_nullable => 0,
105 %{$source->column_info($col)}
106 );
0009fa49 107 if ($colinfo{is_nullable}) {
108 $colinfo{default} = '' unless exists $colinfo{default};
109 }
b02b20b5 110 my $f = $table->add_field(%colinfo) || die $table->error;
111 }
112 $table->primary_key($source->primary_columns);
113
7b90bb13 114 my @primary = $source->primary_columns;
115 my %unique_constraints = $source->unique_constraints;
b7e303a8 116 foreach my $uniq (sort keys %unique_constraints) {
6d0ee587 117 if (!$source->_compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 118 $table->add_constraint(
119 type => 'unique',
a7e65bb5 120 name => $uniq,
7b90bb13 121 fields => $unique_constraints{$uniq}
122 );
123 }
124 }
125
b02b20b5 126 my @rels = $source->relationships();
499adf63 127
128 my %created_FK_rels;
48850f9a 129
2581038c 130 # global add_fk_index set in parser_args
131 my $add_fk_index = (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) ? 0 : 1;
499adf63 132
454a5a42 133 foreach my $rel (sort @rels)
b02b20b5 134 {
135 my $rel_info = $source->relationship_info($rel);
637ca936 136
637ca936 137 # Ignore any rel cond that isn't a straight hash
138 next unless ref $rel_info->{cond} eq 'HASH';
139
de60a93d 140 my $othertable = $source->related_source($rel);
141 my $rel_table = $othertable->name;
142
e377d723 143 my $reverse_rels = $source->reverse_relationship_info($rel);
144 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
145
d1b264d3 146 # Force the order of @cond to match the order of ->add_columns
147 my $idx;
148 my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $othertable->columns;
149 my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}});
48850f9a 150
637ca936 151 # Get the key information, mapping off the foreign/self markers
637ca936 152 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
153 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
154
e377d723 155 # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to)
156 my $fk_constraint;
de60a93d 157
e377d723 158 #first it can be specified explicitly
159 if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) {
160 $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint};
161 }
162 # it can not be multi
069e49b6 163 elsif ( $rel_info->{attrs}{accessor}
164 && $rel_info->{attrs}{accessor} eq 'multi' ) {
e377d723 165 $fk_constraint = 0;
166 }
167 # if indeed single, check if all self.columns are our primary keys.
168 # this is supposed to indicate a has_one/might_have...
169 # where's the introspection!!?? :)
170 else {
6d0ee587 171 $fk_constraint = not $source->_compare_relationship_keys(\@keys, \@primary);
e377d723 172 }
de60a93d 173
e377d723 174 my $cascade;
175 for my $c (qw/delete update/) {
176 if (exists $rel_info->{attrs}{"on_$c"}) {
177 if ($fk_constraint) {
178 $cascade->{$c} = $rel_info->{attrs}{"on_$c"};
179 }
180 else {
48850f9a 181 carp "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. "
e377d723 182 . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n";
183 }
184 }
185 elsif (defined $otherrelationship and $otherrelationship->{attrs}{$c eq 'update' ? 'cascade_copy' : 'cascade_delete'}) {
186 $cascade->{$c} = 'CASCADE';
de60a93d 187 }
e377d723 188 }
189
190 if($rel_table)
191 {
192 # Constraints are added only if applicable
193 next unless $fk_constraint;
194
195 # Make sure we dont create the same foreign key constraint twice
196 my $key_test = join("\x00", @keys);
197 next if $created_FK_rels{$rel_table}->{$key_test};
de60a93d 198
48850f9a 199 if (scalar(@keys)) {
200
201 $created_FK_rels{$rel_table}->{$key_test} = 1;
13de943d 202
48850f9a 203 my $is_deferrable = $rel_info->{attrs}{is_deferrable};
204
205 # do not consider deferrable constraints and self-references
206 # for dependency calculations
207 if (! $is_deferrable and $rel_table ne $table_name) {
208 $tables{$table_name}{foreign_table_deps}{$rel_table}++;
209 }
7b5d0b84 210
7b5d0b84 211 $table->add_constraint(
0d865134 212 type => 'foreign_key',
48850f9a 213 name => join('_', $table_name, 'fk', @keys),
0d865134 214 fields => \@keys,
215 reference_fields => \@refkeys,
216 reference_table => $rel_table,
555b3385 217 on_delete => uc ($cascade->{delete} || ''),
218 on_update => uc ($cascade->{update} || ''),
e394339b 219 (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
7b5d0b84 220 );
48850f9a 221
222 # global parser_args add_fk_index param can be overridden on the rel def
223 my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index;
224
2581038c 225 if ($add_fk_index_rel) {
0e2c6809 226 my $index = $table->add_index(
48850f9a 227 name => join('_', $table_name, 'idx', @keys),
0e2c6809 228 fields => \@keys,
229 type => 'NORMAL',
230 );
231 }
232 }
b02b20b5 233 }
234 }
48850f9a 235
b02b20b5 236 }
d6c79cb3 237
48850f9a 238 # attach the tables to the schema in dependency order
239 my $dependencies = {
240 map { $_ => _resolve_deps ($_, \%tables) } (keys %tables)
241 };
242 for my $table (sort
243 {
244 keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} }
245 ||
246 $a cmp $b
247 }
248 (keys %tables)
249 ) {
250 $schema->add_table ($tables{$table}{object});
251 $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} );
252 }
253
254
255 my %views;
1d521afd 256 foreach my $moniker (sort @view_monikers)
257 {
258 my $source = $dbicschema->source($moniker);
48850f9a 259 my $view_name = $source->name;
260
1d521afd 261 # Skip custom query sources
48850f9a 262 next if ref $view_name;
1d521afd 263
264 # Its possible to have multiple DBIC source using same table
48850f9a 265 next if $views{$view_name}++;
1d521afd 266
48850f9a 267 my $view = $schema->add_view (
268 name => $view_name,
1d521afd 269 fields => [ $source->columns ],
f534e33b 270 $source->view_definition ? ( 'sql' => $source->view_definition ) : ()
48850f9a 271 ) || die $schema->error;
a99b214b 272
273 $source->_invoke_sqlt_deploy_hook($view);
1d521afd 274 }
275
48850f9a 276
b7e303a8 277 if ($dbicschema->can('sqlt_deploy_hook')) {
278 $dbicschema->sqlt_deploy_hook($schema);
d6c79cb3 279 }
280
637ca936 281 return 1;
75d07914 282}
b02b20b5 283
48850f9a 284#
285# Quick and dirty dependency graph calculator
286#
287sub _resolve_deps {
288 my ($table, $tables, $seen) = @_;
289
290 my $ret = {};
291 $seen ||= {};
292
293 # copy and bump all deps by one (so we can reconstruct the chain)
294 my %seen = map { $_ => $seen->{$_} + 1 } (keys %$seen);
295 $seen{$table} = 1;
296
297 for my $dep (keys %{$tables->{$table}{foreign_table_deps}} ) {
298
299 if ($seen->{$dep}) {
300
301 # warn and remove the circular constraint so we don't get flooded with the same warning over and over
302 #carp sprintf ("Circular dependency detected, schema may not be deployable:\n%s\n",
303 # join (' -> ', (sort { $seen->{$b} <=> $seen->{$a} } (keys %$seen) ), $table, $dep )
304 #);
305 #delete $tables->{$table}{foreign_table_deps}{$dep};
306
307 return {};
308 }
309
310 my $subdeps = _resolve_deps ($dep, $tables, \%seen);
311 $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps );
312
313 ++$ret->{$dep};
314 }
315
316 return $ret;
317}
318
0da8b7da 3191;
7232ce07 320
321=head1 NAME
322
323SQL::Translator::Parser::DBIx::Class - Create a SQL::Translator schema
324from a DBIx::Class::Schema instance
325
326=head1 SYNOPSIS
327
f26d4b95 328 ## Via DBIx::Class
329 use MyApp::Schema;
330 my $schema = MyApp::Schema->connect("dbi:SQLite:something.db");
331 $schema->create_ddl_dir();
332 ## or
333 $schema->deploy();
334
335 ## Standalone
7232ce07 336 use MyApp::Schema;
337 use SQL::Translator;
338
339 my $schema = MyApp::Schema->connect;
340 my $trans = SQL::Translator->new (
341 parser => 'SQL::Translator::Parser::DBIx::Class',
342 parser_args => { package => $schema },
343 producer => 'SQLite',
344 ) or die SQL::Translator->error;
345 my $out = $trans->translate() or die $trans->error;
346
347=head1 DESCRIPTION
348
f26d4b95 349This class requires L<SQL::Translator> installed to work.
350
7232ce07 351C<SQL::Translator::Parser::DBIx::Class> reads a DBIx::Class schema,
352interrogates the columns, and stuffs it all in an $sqlt_schema object.
353
f26d4b95 354It's primary use is in deploying database layouts described as a set
db2b2eb6 355of L<DBIx::Class> classes, to a database. To do this, see
356L<DBIx::Class::Schema/deploy>.
f26d4b95 357
358This can also be achieved by having DBIx::Class export the schema as a
359set of SQL files ready for import into your database, or passed to
360other machines that need to have your application installed but don't
db2b2eb6 361have SQL::Translator installed. To do this see
362L<DBIx::Class::Schema/create_ddl_dir>.
f26d4b95 363
7232ce07 364=head1 SEE ALSO
365
f26d4b95 366L<SQL::Translator>, L<DBIx::Class::Schema>
7232ce07 367
368=head1 AUTHORS
369
370Jess Robinson
371
372Matt S Trout
373
374Ash Berlin