From: Amiri Barksdale at Home Date: Mon, 12 Jul 2010 02:58:41 +0000 (-0700) Subject: View-dep cleanups (really paper overs, code needs a rewrite) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6c97a902a7139d5d77d3001d40ac8219031d4181;p=dbsrgits%2FDBIx-Class-Historic.git View-dep cleanups (really paper overs, code needs a rewrite) Tighter check for ref type in resolver (this was broken but unnoticed) Skip table result classes in view part of resolver. No uninitialized warnings in resolver where it expects some misses (ugh!) Cleanup viewdeps test (and correct incorrect comparison) This should have not merged as is, but time is short to fix it properly. Hopefully next iter. --- diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index 280f7fe..aa22b05 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -15,7 +15,7 @@ $DEBUG = 0 unless defined $DEBUG; use Exporter; use SQL::Translator::Utils qw(debug normalize_name); use Carp::Clan qw/^SQL::Translator|^DBIx::Class|^Try::Tiny/; -use Scalar::Util 'weaken'; +use Scalar::Util qw/weaken blessed/; use Try::Tiny; use namespace::clean; @@ -361,7 +361,9 @@ sub _resolve_deps { # copy and bump all deps by one (so we can reconstruct the chain) my %seen = map { $_ => $seen->{$_} + 1 } ( keys %$seen ); - if ( ref($question) =~ /View/ ) { + if ( blessed($question) + && $question->isa('DBIx::Class::ResultSource::View') ) + { $seen{ $question->result_class } = 1; @deps = keys %{ $question->{deploy_depends_on} }; } @@ -376,10 +378,17 @@ sub _resolve_deps { } my $next_dep; - if ( ref($question) =~ /View/ ) { + if ( blessed($question) + && $question->isa('DBIx::Class::ResultSource::View') ) + { + no warnings 'uninitialized'; my ($next_dep_source_name) = - grep { $question->schema->source($_)->result_class eq $dep } - @{ [ $question->schema->sources ] }; + grep { + $question->schema->source($_)->result_class eq $dep + && !( $question->schema->source($_) + ->isa('DBIx::Class::ResultSource::Table') ) + } @{ [ $question->schema->sources ] }; + return {} unless $next_dep_source_name; $next_dep = $question->schema->source($next_dep_source_name); } else { diff --git a/t/105view_deps.t b/t/105view_deps.t index d056e9d..6a40e20 100644 --- a/t/105view_deps.t +++ b/t/105view_deps.t @@ -7,6 +7,7 @@ use Test::More; use Test::Exception; use Test::Warn; use lib qw(t/lib); +use DBICTest; use ViewDeps; use ViewDepsBad; @@ -29,89 +30,86 @@ isa_ok( $view, 'DBIx::Class', 'A new view also' ); can_ok( $view, $_ ) for qw/new from deploy_depends_on/; #################### DEPS - -my $schema - = ViewDeps->connect( 'dbi:SQLite::memory:', { quote_char => '"', } ); -ok( $schema, 'Connected to ViewDeps schema OK' ); +{ + my $schema + = ViewDeps->connect( DBICTest->_database (quote_char => '"') ); + ok( $schema, 'Connected to ViewDeps schema OK' ); #################### DEPLOY -$schema->deploy( { add_drop_table => 1 } ); + $schema->deploy( { add_drop_table => 1 } ); #################### DOES ORDERING WORK? -my $sqlt_object = $schema->{sqlt}; - -my @keys = keys %{ $sqlt_object->{views} }; - -my @sqlt_sources = sort { - $sqlt_object->{views}->{$a}->{order} - cmp $sqlt_object->{views}->{$b}->{order} -} @keys; - -my @expected - = qw/a_name_artists track_number_fives year_2010_cds ab_name_artists year_2010_cds_with_many_tracks aba_name_artists aba_name_artists_and_2010_cds_with_many_tracks/; - -is_deeply( \@expected, \@sqlt_sources, - "SQLT view order triumphantly matches our order." ); + my $sqlt_object = $schema->{sqlt}; + + is_deeply( + [ map { $_->name } $sqlt_object->get_views ], + [qw/ + a_name_artists + track_number_fives + year_2010_cds + ab_name_artists + year_2010_cds_with_many_tracks + aba_name_artists + aba_name_artists_and_2010_cds_with_many_tracks + /], + "SQLT view order triumphantly matches our order." + ); #################### AND WHAT ABOUT USING THE SCHEMA? -lives_ok( sub { $schema->resultset($_)->next }, "Query on $_ succeeds" ) + lives_ok( sub { $schema->resultset($_)->next }, "Query on $_ succeeds" ) for grep { $schema->resultset($_) - ->result_source->isa('DBIx::Class::ResultSource::View') + ->result_source->isa('DBIx::Class::ResultSource::View') } @{ [ $schema->sources ] }; +} #################### AND WHAT ABOUT A BAD DEPS CHAIN IN A VIEW? -my $schema2 - = ViewDepsBad->connect( 'dbi:SQLite::memory:', { quote_char => '"', } ); -ok( $schema2, 'Connected to ViewDepsBad schema OK' ); +{ + my $schema2 + = ViewDepsBad->connect( DBICTest->_database ( quote_char => '"') ); + ok( $schema2, 'Connected to ViewDepsBad schema OK' ); #################### DEPLOY2 -warnings_exist { $schema2->deploy( { add_drop_table => 1 } ); } -[qr/no such table: main.aba_name_artists/], + warnings_exist { $schema2->deploy( { add_drop_table => 1 } ) } + [qr/no such table: main.aba_name_artists/], "Deploying the bad schema produces a warning: aba_name_artists was not created."; #################### DOES ORDERING WORK 2? -my $sqlt_object2 = $schema2->{sqlt}; - -my @keys2 = keys %{ $sqlt_object->{views} }; - -my @sqlt_sources2 = sort { - $sqlt_object->{views}->{$a}->{order} - cmp $sqlt_object->{views}->{$b}->{order} -} @keys2; - -my @expected2 - = qw/a_name_artists track_number_fives year_2010_cds ab_name_artists year_2010_cds_with_many_tracks aba_name_artists aba_name_artists_and_2010_cds_with_many_tracks/; - -is_deeply( \@expected2, \@sqlt_sources2, - "SQLT view order triumphantly matches our order." ); + my $sqlt_object2 = $schema2->{sqlt}; + + is_deeply( + [ map { $_->name } $sqlt_object2->get_views ], + [qw/ + a_name_artists + track_number_fives + year_2010_cds + ab_name_artists + year_2010_cds_with_many_tracks + aba_name_artists_and_2010_cds_with_many_tracks + aba_name_artists + /], + "SQLT view order triumphantly matches our order." + ); #################### AND WHAT ABOUT USING THE SCHEMA2? -lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" ) + lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" ) for grep { $schema2->resultset($_) - ->result_source->isa('DBIx::Class::ResultSource::View') + ->result_source->isa('DBIx::Class::ResultSource::View') } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ } @{ [ $schema2->sources ] }; -dies_ok( - sub { - $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next; - }, - "Query on AbaNameArtistsAnd2010CDsWithManyTracks dies, because of incorrect deploy_depends_on in AbaNameArtists" -); - -throws_ok { - $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next; + throws_ok { $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next } + qr/no such table: aba_name_artists_and_2010_cds_with_many_tracks/, + "Query on AbaNameArtistsAnd2010CDsWithManyTracks throws, because the table does not exist" + ; } -qr/no such table: aba_name_artists_and_2010_cds_with_many_tracks/, - "Query on AbaNameArtistsAnd2010CDsWithManyTracks throws, because the table does not exist"; done_testing; diff --git a/t/lib/ViewDepsBad/Result/AbaNameArtists.pm~ b/t/lib/ViewDepsBad/Result/AbaNameArtists.pm~ deleted file mode 100644 index fc989f6..0000000 --- a/t/lib/ViewDepsBad/Result/AbaNameArtists.pm~ +++ /dev/null @@ -1,26 +0,0 @@ -package # hide from PAUSE - ViewDeps::Result::AbaNameArtists; - -use strict; -use warnings; -use base 'DBIx::Class::Core'; - -__PACKAGE__->table_class('DBIx::Class::ResultSource::View'); -__PACKAGE__->table('aba_name_artists'); -__PACKAGE__->result_source_instance->view_definition( - "SELECT id,name FROM ab_name_artists WHERE name like 'aba%'" ); -__PACKAGE__->result_source_instance->deploy_depends_on( - ["ViewDeps::Result::AbNameArtists"] ); - -__PACKAGE__->add_columns( - id => { data_type => 'integer', is_auto_increment => 1 }, - name => { data_type => 'text' }, -); - -__PACKAGE__->set_primary_key('id'); - -__PACKAGE__->has_many( 'cds', 'ViewDeps::Result::CD', - { "foreign.artist" => "self.id" }, -); - -1;