use Carp::Clan qw/^SQL::Translator|^DBIx::Class|^Try::Tiny/;
use Scalar::Util 'weaken';
use Try::Tiny;
+use Devel::Dwarn;
use namespace::clean;
use base qw(Exporter);
}
my %views;
+ my @views = map { $dbicschema->source($_) } keys %view_monikers;
+ my $view_dependencies;
- my @view_sources =
- sort {
- keys %{ $a->deploy_depends_on || {} }
- <=>
- keys %{ $b->deploy_depends_on || {} }
- ||
- $a->source_name cmp $b->source_name
+ ### This is a loop instead of a map because
+ ### passing an object to the sub failed--gave
+ ### $view->name instead of the $view!
+
+ for my $view (@views) {
+ $view_dependencies->{ $view->name } =
+ _resolve_view_deps ( { view => $view }, \%view_monikers );
}
- map { $dbicschema->source($_) }
- keys %view_monikers;
+
+ my @view_sources =
+ sort {
+ keys %{ $view_dependencies->{ $a->name } || {} } <=>
+ keys %{ $view_dependencies->{ $b->name } || {} }
+ || $a->source_name cmp $b->source_name
+ }
+ map { $dbicschema->source($_) }
+ keys %view_monikers;
foreach my $source (@view_sources)
{
return $ret;
}
+sub _resolve_view_deps {
+ my ( $view0, $monikers, $seen ) = @_;
+ my $view = $view0->{view};
+ my $ret = {};
+ $seen ||= {};
+
+ # copy and bump all deps by one (so we can reconstruct the chain)
+ my %seen = map { $_ => $seen->{$_} + 1 } ( keys %$seen );
+ $seen{ $view->source_name } = 1;
+
+ for my $dep ( keys %{ $view->{deploy_depends_on} } ) {
+ if ( $seen->{$dep} ) {
+ return {};
+ }
+ my ($new_source_name) =
+ grep { $view->schema->source($_)->name eq $dep }
+ @{ [ $view->schema->sources ] };
+ my $subdeps = _resolve_view_deps(
+ { view => $view->schema->source($new_source_name) },
+ $monikers, \%seen, );
+ $ret->{$_} += $subdeps->{$_} for ( keys %$subdeps );
+
+ ++$ret->{$dep};
+ }
+ return $ret;
+}
+
1;
=head1 NAME
use Test::Exception;
use lib qw(t/lib);
use ViewDeps;
+use ViewDepsBad;
BEGIN {
use_ok('DBIx::Class::ResultSource::View');
#################### SANITY
-my $view = DBIx::Class::ResultSource::View->new( { name => 'Quux' } );
+my $view = DBIx::Class::ResultSource::View->new;
isa_ok( $view, 'DBIx::Class::ResultSource', 'A new view' );
isa_ok( $view, 'DBIx::Class', 'A new view also' );
= ViewDeps->connect( 'dbi:SQLite::memory:', { quote_char => '"', } );
ok( $schema, 'Connected to ViewDeps schema OK' );
-my $deps_ref = {
- map {
- $schema->resultset($_)->result_source->name =>
- $schema->resultset($_)->result_source->deploy_depends_on
- }
- grep {
- $schema->resultset($_)
- ->result_source->isa('DBIx::Class::ResultSource::View')
- } @{ [ $schema->sources ] }
-};
-
-my @sorted_sources = sort {
- keys %{ $deps_ref->{$a} || {} } <=> keys %{ $deps_ref->{$b} || {} }
- || $a cmp $b
- }
- keys %$deps_ref;
-
#################### DEPLOY
$schema->deploy( { add_drop_table => 1 } );
#################### DOES ORDERING WORK?
-my $tr = $schema->{sqlt};
+my $sqlt_object = $schema->{sqlt};
+
+my @keys = keys %{ $sqlt_object->{views} };
-my @keys = keys %{ $tr->{views} };
+my @sqlt_sources = sort {
+ $sqlt_object->{views}->{$a}->{order}
+ cmp $sqlt_object->{views}->{$b}->{order}
+} @keys;
-my @sqlt_sources
- = sort { $tr->{views}->{$a}->{order} cmp $tr->{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( \@sorted_sources, \@sqlt_sources,
+is_deeply( \@expected, \@sqlt_sources,
"SQLT view order triumphantly matches our order." );
#################### AND WHAT ABOUT USING THE SCHEMA?
->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' );
+
+#################### DEPLOY2
+
+$schema2->deploy( { add_drop_table => 1 } );
+
+#################### 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." );
+
+#################### AND WHAT ABOUT USING THE SCHEMA2?
+
+lives_ok( sub { $schema2->resultset($_)->next }, "Query on $_ succeeds" )
+ for grep {
+ $schema2->resultset($_)
+ ->result_source->isa('DBIx::Class::ResultSource::View')
+ } grep { !/AbaNameArtistsAnd2010CDsWithManyTracks/ }
+ @{ [ $schema2->sources ] };
+
+dies_ok(
+ sub {
+ $schema2->resultset('AbaNameArtistsAnd2010CDsWithManyTracks')->next;
+ },
+ "Query on AbaNameArtistsAnd2010CDsWithManyTracks fails, because of incorrect deploy_depends_on in AbaNameArtists"
+);
+
done_testing;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad;
+## Used in 105view_deps.t
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Schema';
+
+__PACKAGE__->load_namespaces;
+
+sub sqlt_deploy_hook {
+ my $self = shift;
+ $self->{sqlt} = shift;
+}
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::ANameArtists;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
+__PACKAGE__->table('a_name_artists');
+__PACKAGE__->result_source_instance->view_definition(
+ "SELECT id,name FROM artist WHERE name like 'a%'"
+);
+
+__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;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::AbNameArtists;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
+__PACKAGE__->table('ab_name_artists');
+__PACKAGE__->result_source_instance->view_definition(
+ "SELECT id,name FROM a_name_artists WHERE name like 'ab%'"
+);
+__PACKAGE__->result_source_instance->deploy_depends_on(
+ ["ViewDepsBad::Result::ANameArtists"]
+);
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ name => { data_type => 'text' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->has_many( 'cds', 'ViewDepsBad::Result::CD',
+ { "foreign.artist" => "self.id" },
+);
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::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(
+ ["ViewDepsBad::Result::AbNameArtists", "ViewDepsBad::Result::AbaNameArtistsAnd2010CDsWithManyTracks"] );
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ name => { data_type => 'text' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->has_many( 'cds', 'ViewDepsBad::Result::CD',
+ { "foreign.artist" => "self.id" },
+);
+
+1;
--- /dev/null
+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;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::AbaNameArtistsAnd2010CDsWithManyTracks;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
+__PACKAGE__->table('aba_name_artists_and_2010_cds_with_many_tracks');
+__PACKAGE__->result_source_instance->view_definition(
+ "SELECT aba.id,aba.name,cd.title,cd.year,cd.number_tracks FROM aba_name_artists aba JOIN year_2010_cds_with_many_tracks cd on (aba.id = cd.artist)"
+);
+__PACKAGE__->result_source_instance->deploy_depends_on(
+ ["ViewDepsBad::Result::AbNameArtists","ViewDepsBad::Result::Year2010CDsWithManyTracks"] );
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ name => { data_type => 'text' },
+ title => { data_type => 'text' },
+ year => { data_type => 'integer' },
+ number_tracks => { data_type => 'integer' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::Artist;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('artist');
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ name => { data_type => 'text' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->has_many( 'cds', 'ViewDepsBad::Result::CD',
+ { "foreign.artist" => "self.id" },
+);
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::Artwork;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('artwork');
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ cd => { data_type => 'integer' },
+ file => { data_type => 'text' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->belongs_to( 'cd', 'ViewDepsBad::Result::CD',
+ { "foreign.id" => "self.cd" },
+);
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::CD;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('cd');
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ title => { data_type => 'text' },
+ artist => { data_type => 'integer', is_nullable => 0 },
+ year => { data_type => 'integer' },
+ number_tracks => { data_type => 'integer' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->belongs_to( 'artist', 'ViewDepsBad::Result::Artist',
+ { "foreign.id" => "self.artist" },
+);
+
+__PACKAGE__->has_many( 'tracks', 'ViewDepsBad::Result::Track',
+ { "foreign.cd" => "self.id" },
+);
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::Track;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table('track');
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ title => { data_type => 'text' },
+ cd => { data_type => 'integer' },
+ track_number => { data_type => 'integer' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->belongs_to( 'cd', 'ViewDepsBad::Result::CD',
+ { "foreign.id" => "self.cd" },
+);
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::TrackNumberFives;
+
+use strict;
+use warnings;
+use base 'ViewDepsBad::Result::Track';
+
+__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
+__PACKAGE__->table('track_number_fives');
+__PACKAGE__->result_source_instance->view_definition(
+ "SELECT id,title,cd,track_number FROM track WHERE track_number = '5'");
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ title => { data_type => 'text' },
+ cd => { data_type => 'integer' },
+ track_number => { data_type => 'integer' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->belongs_to( 'cd', 'ViewDepsBad::Result::CD',
+ { "foreign.id" => "self.cd" },
+);
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::Year2010CDs;
+
+use strict;
+use warnings;
+use base 'DBIx::Class::Core';
+
+__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
+__PACKAGE__->table('year_2010_cds');
+__PACKAGE__->result_source_instance->view_definition(
+ "SELECT id,title,artist,year,number_tracks FROM cd WHERE year = '2010'");
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ title => { data_type => 'text' },
+ artist => { data_type => 'integer', is_nullable => 0 },
+ year => { data_type => 'integer' },
+ number_tracks => { data_type => 'integer' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->belongs_to( 'artist', 'ViewDepsBad::Result::Artist',
+ { "foreign.id" => "self.artist" },
+);
+
+__PACKAGE__->has_many( 'tracks', 'ViewDepsBad::Result::Track',
+ { "foreign.cd" => "self.id" },
+);
+
+1;
--- /dev/null
+package # hide from PAUSE
+ ViewDepsBad::Result::Year2010CDsWithManyTracks;
+
+use strict;
+use warnings;
+use base 'ViewDepsBad::Result::Year2010CDs';
+
+__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
+__PACKAGE__->table('year_2010_cds_with_many_tracks');
+__PACKAGE__->result_source_instance->view_definition(
+ "SELECT cd.id,cd.title,cd.artist,cd.year,cd.number_tracks,art.file FROM year_2010_cds cd JOIN artwork art on art.cd = cd.id WHERE cd.number_tracks > 10"
+);
+
+__PACKAGE__->result_source_instance->deploy_depends_on(
+ ["ViewDepsBad::Result::Year2010CDs"] );
+
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', is_auto_increment => 1 },
+ title => { data_type => 'text' },
+ artist => { data_type => 'integer', is_nullable => 0 },
+ year => { data_type => 'integer' },
+ number_tracks => { data_type => 'integer' },
+ file => { data_type => 'integer' },
+);
+
+__PACKAGE__->set_primary_key('id');
+
+__PACKAGE__->belongs_to( 'artist', 'ViewDepsBad::Result::Artist',
+ { "foreign.id" => "self.artist" },
+);
+
+__PACKAGE__->has_many( 'tracks', 'ViewDepsBad::Result::Track',
+ { "foreign.cd" => "self.id" },
+);
+
+1;