From: Peter Rabbitson Date: Sat, 27 Jun 2009 11:59:03 +0000 (+0000) Subject: Adjust sqlt schema parser to add tables in FK dependency order X-Git-Tag: v0.08108~54 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=48850f9af126611240b7843d0f7913c96f30f5ba Adjust sqlt schema parser to add tables in FK dependency order --- diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index 97e333c..d786185 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -14,6 +14,7 @@ $DEBUG = 0 unless defined $DEBUG; use Exporter; use SQL::Translator::Utils qw(debug normalize_name); +use Carp::Clan qw/^SQL::Translator|^DBIx::Class/; use base qw(Exporter); @@ -34,7 +35,7 @@ sub parse { my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data; $dbicschema ||= $args->{'package'}; my $limit_sources = $args->{'sources'}; - + die 'No DBIx::Class::Schema' unless ($dbicschema); if (!ref $dbicschema) { eval "use $dbicschema;"; @@ -47,8 +48,6 @@ sub parse { $schema->name( ref($dbicschema) . " v" . ($dbicschema->schema_version || '1.x')) unless ($schema->name); - my %seen_tables; - my @monikers = sort $dbicschema->sources; if ($limit_sources) { my $ref = ref $limit_sources || ''; @@ -76,21 +75,23 @@ sub parse { } } + my %tables; foreach my $moniker (sort @table_monikers) { my $source = $dbicschema->source($moniker); - + my $table_name = $source->name; + # Skip custom query sources - next if ref($source->name); + next if ref $table_name; - # Its possible to have multiple DBIC source using same table - next if $seen_tables{$source->name}++; + # Its possible to have multiple DBIC sources using the same table + next if $tables{$table_name}; - my $table = $schema->add_table( - name => $source->name, + $tables{$table_name}{source} = $source; + my $table = $tables{$table_name}{object} = SQL::Translator::Schema::Table->new( + name => $table_name, type => 'TABLE', - ) || die $schema->error; - my $colcount = 0; + ); foreach my $col ($source->columns) { # assuming column_info in dbic is the same as DBI (?) @@ -125,7 +126,7 @@ sub parse { my @rels = $source->relationships(); my %created_FK_rels; - + # global add_fk_index set in parser_args my $add_fk_index = (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) ? 0 : 1; @@ -146,7 +147,7 @@ sub parse { my $idx; my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $othertable->columns; my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); - + # Get the key information, mapping off the foreign/self markers my @refkeys = map {/^\w+\.(\w+)$/} @cond; my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond; @@ -177,7 +178,7 @@ sub parse { $cascade->{$c} = $rel_info->{attrs}{"on_$c"}; } else { - warn "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. " + carp "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. " . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n"; } } @@ -195,17 +196,21 @@ sub parse { my $key_test = join("\x00", @keys); next if $created_FK_rels{$rel_table}->{$key_test}; - my $is_deferrable = $rel_info->{attrs}{is_deferrable}; - - # global parser_args add_fk_index param can be overridden on the rel def - my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index; + if (scalar(@keys)) { + + $created_FK_rels{$rel_table}->{$key_test} = 1; + my $is_deferrable = $rel_info->{attrs}{is_deferrable}; + + # do not consider deferrable constraints and self-references + # for dependency calculations + if (! $is_deferrable and $rel_table ne $table_name) { + $tables{$table_name}{foreign_table_deps}{$rel_table}++; + } - $created_FK_rels{$rel_table}->{$key_test} = 1; - if (scalar(@keys)) { $table->add_constraint( type => 'foreign_key', - name => join('_', $table->name, 'fk', @keys), + name => join('_', $table_name, 'fk', @keys), fields => \@keys, reference_fields => \@refkeys, reference_table => $rel_table, @@ -213,10 +218,13 @@ sub parse { on_update => uc ($cascade->{update} || ''), (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()), ); - + + # global parser_args add_fk_index param can be overridden on the rel def + my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index; + if ($add_fk_index_rel) { my $index = $table->add_index( - name => join('_', $table->name, 'idx', @keys), + name => join('_', $table_name, 'idx', @keys), fields => \@keys, type => 'NORMAL', ); @@ -224,31 +232,48 @@ sub parse { } } } - - $source->_invoke_sqlt_deploy_hook($table); + } + # attach the tables to the schema in dependency order + my $dependencies = { + map { $_ => _resolve_deps ($_, \%tables) } (keys %tables) + }; + for my $table (sort + { + keys %{$dependencies->{$a} || {} } <=> keys %{ $dependencies->{$b} || {} } + || + $a cmp $b + } + (keys %tables) + ) { + $schema->add_table ($tables{$table}{object}); + $tables{$table}{source} -> _invoke_sqlt_deploy_hook( $tables{$table}{object} ); + } + + + my %views; foreach my $moniker (sort @view_monikers) { my $source = $dbicschema->source($moniker); + my $view_name = $source->name; + # Skip custom query sources - next if ref($source->name); + next if ref $view_name; # Its possible to have multiple DBIC source using same table - next if $seen_tables{$source->name}++; + next if $views{$view_name}++; - my $view = $schema->add_view( - name => $source->name, + my $view = $schema->add_view ( + name => $view_name, fields => [ $source->columns ], $source->view_definition ? ( 'sql' => $source->view_definition ) : () - ); - if ($source->result_class->can('sqlt_deploy_hook')) { - $source->result_class->sqlt_deploy_hook($view); - } + ) || die $schema->error; $source->_invoke_sqlt_deploy_hook($view); } + if ($dbicschema->can('sqlt_deploy_hook')) { $dbicschema->sqlt_deploy_hook($schema); } @@ -256,6 +281,41 @@ sub parse { return 1; } +# +# Quick and dirty dependency graph calculator +# +sub _resolve_deps { + my ($table, $tables, $seen) = @_; + + my $ret = {}; + $seen ||= {}; + + # copy and bump all deps by one (so we can reconstruct the chain) + my %seen = map { $_ => $seen->{$_} + 1 } (keys %$seen); + $seen{$table} = 1; + + for my $dep (keys %{$tables->{$table}{foreign_table_deps}} ) { + + if ($seen->{$dep}) { + + # warn and remove the circular constraint so we don't get flooded with the same warning over and over + #carp sprintf ("Circular dependency detected, schema may not be deployable:\n%s\n", + # join (' -> ', (sort { $seen->{$b} <=> $seen->{$a} } (keys %$seen) ), $table, $dep ) + #); + #delete $tables->{$table}{foreign_table_deps}{$dep}; + + return {}; + } + + my $subdeps = _resolve_deps ($dep, $tables, \%seen); + $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps ); + + ++$ret->{$dep}; + } + + return $ret; +} + 1; =head1 NAME diff --git a/t/86sqlt.t b/t/86sqlt.t index c033113..467fed3 100644 --- a/t/86sqlt.t +++ b/t/86sqlt.t @@ -45,7 +45,11 @@ my $translator = SQL::Translator->new( ok($output, "SQLT produced someoutput") or diag($translator->error); - like ($warn, qr/^SQLT attribute .+? was supplied for relationship/, 'Warn about dubious on_delete/on_update attributes'); + like ( + $warn, + qr/SQLT attribute .+? was supplied for relationship .+? which does not appear to be a foreign constraint/, + 'Warn about dubious on_delete/on_update attributes', + ); } # Note that the constraints listed here are the only ones that are tested -- if