From: Peter Rabbitson Date: Sat, 19 Dec 2009 18:41:42 +0000 (+0000) Subject: Fix RT52812 X-Git-Tag: v0.08116~83 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a7f4b74cf7a4e82dcf4ef3847c5881f6dad361c9;p=dbsrgits%2FDBIx-Class.git Fix RT52812 --- diff --git a/Changes b/Changes index c6015bd..569803f 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,8 @@ Revision history for DBIx::Class - might_have/has_one now warn if applied calling class's column has is_nullable set to true. + - Fixed regression in deploy() with a {sources} table limit applied + (RT#52812) - Cookbook POD fix for add_drop_table instead of add_drop_tables - Views without a view_definition will throw an exception when parsed by SQL::Translator::Parser::DBIx::Class diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index c8235e3..e760580 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -65,19 +65,19 @@ sub parse { } - my(@table_monikers, @view_monikers); + my(%table_monikers, %view_monikers); for my $moniker (@monikers){ my $source = $dbicschema->source($moniker); if ( $source->isa('DBIx::Class::ResultSource::Table') ) { - push(@table_monikers, $moniker); + $table_monikers{$moniker}++; } elsif( $source->isa('DBIx::Class::ResultSource::View') ){ next if $source->is_virtual; - push(@view_monikers, $moniker); + $view_monikers{$moniker}++; } } my %tables; - foreach my $moniker (sort @table_monikers) + foreach my $moniker (sort keys %table_monikers) { my $source = $dbicschema->source($moniker); my $table_name = $source->name; @@ -131,18 +131,22 @@ sub parse { 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; + my $add_fk_index = (exists $args->{add_fk_index} && ! $args->{add_fk_index}) ? 0 : 1; foreach my $rel (sort @rels) { + my $rel_info = $source->relationship_info($rel); # Ignore any rel cond that isn't a straight hash next unless ref $rel_info->{cond} eq 'HASH'; - my $othertable = $source->related_source($rel); - next if $othertable->isa('DBIx::Class::ResultSource::View'); # can't define constraints referencing a view - my $rel_table = $othertable->name; + my $relsource = $source->related_source($rel); + + # related sources might be excluded via a {sources} filter or might be views + next unless exists $table_monikers{$relsource->source_name}; + + my $rel_table = $relsource->name; # FIXME - this isn't the right way to do it, but sqlt does not # support quoting properly to be signaled about this @@ -153,7 +157,7 @@ sub parse { # Force the order of @cond to match the order of ->add_columns my $idx; - my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $othertable->columns; + my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $relsource->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 @@ -210,11 +214,12 @@ sub parse { my $is_deferrable = $rel_info->{attrs}{is_deferrable}; - # do not consider deferrable constraints and self-references - # for dependency calculations + # calculate dependencies: 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}++; } + $table->add_constraint( type => 'foreign_key', name => join('_', $table_name, 'fk', @keys), @@ -274,7 +279,7 @@ EOW } my %views; - foreach my $moniker (sort @view_monikers) + foreach my $moniker (sort keys %view_monikers) { my $source = $dbicschema->source($moniker); my $view_name = $source->name; diff --git a/t/99dbic_sqlt_parser.t b/t/99dbic_sqlt_parser.t index 86ddf76..f8b88c3 100644 --- a/t/99dbic_sqlt_parser.t +++ b/t/99dbic_sqlt_parser.t @@ -84,6 +84,24 @@ my @sources = grep 'parser detects views with a view_definition'; } +lives_ok (sub { + my $sqlt_schema = create_schema ({ + schema => $schema, + args => { + parser_args => { + sources => ['CD'] + }, + }, + }); + + is_deeply ( + [$sqlt_schema->get_tables ], + ['cd'], + 'sources limitng with relationships works', + ); + +}); + done_testing; sub create_schema {