From: Amiri Barksdale Date: Tue, 1 Jun 2010 15:21:35 +0000 (+0000) Subject: Change ViewDeps schema to use meaningful tables; use the number of views a view depen... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7364d776a7926a47d74fb503ca546bec719e88f3;p=dbsrgits%2FDBIx-Class-Historic.git Change ViewDeps schema to use meaningful tables; use the number of views a view depends_on to do the sorting. --- diff --git a/lib/DBIx/Class/ResultSource/View.pm b/lib/DBIx/Class/ResultSource/View.pm index 1757924..cb20f2c 100644 --- a/lib/DBIx/Class/ResultSource/View.pm +++ b/lib/DBIx/Class/ResultSource/View.pm @@ -135,27 +135,11 @@ syntaxes. "MyDB::Schema::Result::Year","MyDB::Schema::Result::CD" ); -Specify the result classes that comprise this view. Pass this -method a list. +Specify the result classes or other views that comprise this view. +Pass this method an array reference. =head1 OVERRIDDEN METHODS -=head2 new - -The constructor. This is a private method, as only other DBIC modules -should call this. - -=cut - -sub new { - my ( $self, @args ) = @_; - my $new = $self->next::method(@args); - $new->{deploy_depends_on} - = { map { $_->result_source_instance->name => 1 } @{ $new->{deploy_depends_on}||[] } } - unless ref $new->{deploy_depends_on} eq 'HASH'; - return $new; -} - =head2 from Returns the FROM entry for the table (i.e. the view name) @@ -169,14 +153,14 @@ sub from { return $self->name; } -=head1 PRIVATE METHODS - -=head2 deploy_depends_on - -An internal method for the construction of a hashref of the view's -superclasses, e.g., the sources that comprise it. - -=cut +sub new { + my ( $self, @args ) = @_; + my $new = $self->next::method(@args); + $new->{deploy_depends_on} + = { map { $_->result_source_instance->name => 1 } @{ $new->{deploy_depends_on}||[] } } + unless ref $new->{deploy_depends_on} eq 'HASH'; + return $new; +} 1; diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index 073a45c..433a27f 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -301,11 +301,11 @@ EOW my @view_sources = sort { - keys %{ $dependencies->{$a} || {} } + keys %{ $a->deploy_depends_on || {} } <=> - keys %{ $dependencies->{$b} || {} } + keys %{ $b->deploy_depends_on || {} } || - $a cmp $b + $a->source_name cmp $b->source_name } map { $dbicschema->source($_) } keys %view_monikers; diff --git a/t/105view_deps.t b/t/105view_deps.t index e55d2a6..4900bf6 100644 --- a/t/105view_deps.t +++ b/t/105view_deps.t @@ -7,6 +7,8 @@ use Test::More; use Test::Exception; use lib qw(t/lib); use ViewDeps; +use Devel::Dwarn; +use Data::Dumper; BEGIN { use_ok('DBIx::Class::ResultSource::View'); @@ -26,43 +28,46 @@ can_ok( $view, $_ ) for qw/new from deploy_depends_on/; my $schema = ViewDeps->connect; ok( $schema, 'Connected to ViewDeps schema OK' ); - -my $bar_rs = $schema->resultset('Bar'); - -my @bar_deps - = keys %{ $schema->resultset('Bar')->result_source->deploy_depends_on }; - -my @foo_deps - = keys %{ $schema->resultset('Foo')->result_source->deploy_depends_on }; - -isa_ok( $schema->resultset('Bar')->result_source, - 'DBIx::Class::ResultSource::View', 'Bar' ); - -is( $bar_deps[0], 'baz', 'which is reported to depend on baz...' ); -is( $bar_deps[1], 'mixin', 'and on mixin.' ); -is( $foo_deps[0], undef, 'Foo has no declared dependencies...' ); - - - -isa_ok( - $schema->resultset('Foo')->result_source, - 'DBIx::Class::ResultSource::View', - 'though Foo' -); -isa_ok( - $schema->resultset('Baz')->result_source, - 'DBIx::Class::ResultSource::Table', - "Baz on the other hand" -); -dies_ok { - ViewDeps::Result::Baz->result_source_instance - ->deploy_depends_on("ViewDeps::Result::Mixin"); -} -"...and you cannot use deploy_depends_on with that"; - -is(ViewDeps->source('Foo')->view_definition, $schema->resultset('Bar')->result_source->view_definition, "Package Foo's view definition is equivalent to resultset Bar's view definition"); +my $deps_ref = { + map { + $schema->resultset($_)->result_source->source_name => + $schema->resultset($_)->result_source->deploy_depends_on + } + grep { + $schema->resultset($_) + ->result_source->isa('DBIx::Class::ResultSource::View') + } @{ [ $schema->sources ] } +}; + +diag( Dwarn $deps_ref); + + +#isa_ok( $schema->resultset('Bar')->result_source, +#'DBIx::Class::ResultSource::View', 'Bar' ); + +#is( $bar_deps[0], 'baz', 'which is reported to depend on baz...' ); +#is( $bar_deps[1], 'mixin', 'and on mixin.' ); +#is( $foo_deps[0], undef, 'Foo has no declared dependencies...' ); + +#isa_ok( +#$schema->resultset('Foo')->result_source, +#'DBIx::Class::ResultSource::View', +#'though Foo' +#); +#isa_ok( +#$schema->resultset('Baz')->result_source, +#'DBIx::Class::ResultSource::Table', +#"Baz on the other hand" +#); +#dies_ok { +#ViewDeps::Result::Baz->result_source_instance +#->deploy_depends_on("ViewDeps::Result::Mixin"); +#} +#"...and you cannot use deploy_depends_on with that"; + +### DEPLOY my $dir = "t/sql"; -$schema->create_ddl_dir( ['PostgreSQL','SQLite'], 0.1, $dir ); +$schema->create_ddl_dir( [ 'PostgreSQL', 'SQLite' ], 0.1, $dir ); done_testing; diff --git a/t/lib/ViewDeps.pm b/t/lib/ViewDeps.pm index 05fea95..aa3f588 100644 --- a/t/lib/ViewDeps.pm +++ b/t/lib/ViewDeps.pm @@ -4,7 +4,7 @@ package # hide from PAUSE use strict; use warnings; -use parent qw(DBIx::Class::Schema); +use base 'DBIx::Class::Schema'; __PACKAGE__->load_namespaces; diff --git a/t/lib/ViewDeps/Result/ANameArtists.pm b/t/lib/ViewDeps/Result/ANameArtists.pm new file mode 100644 index 0000000..e6fbb0f --- /dev/null +++ b/t/lib/ViewDeps/Result/ANameArtists.pm @@ -0,0 +1,25 @@ +package # hide from PAUSE + ViewDeps::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; diff --git a/t/lib/ViewDeps/Result/AbNameArtists.pm b/t/lib/ViewDeps/Result/AbNameArtists.pm new file mode 100644 index 0000000..3651a5b --- /dev/null +++ b/t/lib/ViewDeps/Result/AbNameArtists.pm @@ -0,0 +1,28 @@ +package # hide from PAUSE + ViewDeps::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( + ["ViewDeps::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', 'ViewDeps::Result::CD', + { "foreign.artist" => "self.id" }, +); + +1; diff --git a/t/lib/ViewDeps/Result/AbaNameArtists.pm b/t/lib/ViewDeps/Result/AbaNameArtists.pm new file mode 100644 index 0000000..fc989f6 --- /dev/null +++ b/t/lib/ViewDeps/Result/AbaNameArtists.pm @@ -0,0 +1,26 @@ +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; diff --git a/t/lib/ViewDeps/Result/AbaNameArtistsAnd2010CDsWithManyTracks.pm b/t/lib/ViewDeps/Result/AbaNameArtistsAnd2010CDsWithManyTracks.pm new file mode 100644 index 0000000..83c651b --- /dev/null +++ b/t/lib/ViewDeps/Result/AbaNameArtistsAnd2010CDsWithManyTracks.pm @@ -0,0 +1,26 @@ +package # hide from PAUSE + ViewDeps::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( + ["ViewDeps::Result::AbNameArtists","ViewDeps::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; diff --git a/t/lib/ViewDeps/Result/Artist.pm b/t/lib/ViewDeps/Result/Artist.pm new file mode 100644 index 0000000..276288d --- /dev/null +++ b/t/lib/ViewDeps/Result/Artist.pm @@ -0,0 +1,21 @@ +package # hide from PAUSE + ViewDeps::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', 'ViewDeps::Result::CD', + { "foreign.artist" => "self.id" }, +); + +1; diff --git a/t/lib/ViewDeps/Result/Artwork.pm b/t/lib/ViewDeps/Result/Artwork.pm new file mode 100644 index 0000000..056bdb8 --- /dev/null +++ b/t/lib/ViewDeps/Result/Artwork.pm @@ -0,0 +1,22 @@ +package # hide from PAUSE + ViewDeps::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', 'ViewDeps::Result::CD', + { "foreign.id" => "self.cd" }, +); + +1; diff --git a/t/lib/ViewDeps/Result/CD.pm b/t/lib/ViewDeps/Result/CD.pm new file mode 100644 index 0000000..c69f4b3 --- /dev/null +++ b/t/lib/ViewDeps/Result/CD.pm @@ -0,0 +1,28 @@ +package # hide from PAUSE + ViewDeps::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', 'ViewDeps::Result::Artist', + { "foreign.id" => "self.artist" }, +); + +__PACKAGE__->has_many( 'tracks', 'ViewDeps::Result::Track', + { "foreign.cd" => "self.id" }, +); + +1; diff --git a/t/lib/ViewDeps/Result/Track.pm b/t/lib/ViewDeps/Result/Track.pm new file mode 100644 index 0000000..62b1b99 --- /dev/null +++ b/t/lib/ViewDeps/Result/Track.pm @@ -0,0 +1,23 @@ +package # hide from PAUSE + ViewDeps::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', 'ViewDeps::Result::CD', + { "foreign.id" => "self.cd" }, +); + +1; diff --git a/t/lib/ViewDeps/Result/TrackNumberFives.pm b/t/lib/ViewDeps/Result/TrackNumberFives.pm new file mode 100644 index 0000000..fc4a47b --- /dev/null +++ b/t/lib/ViewDeps/Result/TrackNumberFives.pm @@ -0,0 +1,26 @@ +package # hide from PAUSE + ViewDeps::Result::TrackNumberFives; + +use strict; +use warnings; +use base 'ViewDeps::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', 'ViewDeps::Result::CD', + { "foreign.id" => "self.cd" }, +); + +1; diff --git a/t/lib/ViewDeps/Result/Year2010CDs.pm b/t/lib/ViewDeps/Result/Year2010CDs.pm new file mode 100644 index 0000000..2706fae --- /dev/null +++ b/t/lib/ViewDeps/Result/Year2010CDs.pm @@ -0,0 +1,31 @@ +package # hide from PAUSE + ViewDeps::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', 'ViewDeps::Result::Artist', + { "foreign.id" => "self.artist" }, +); + +__PACKAGE__->has_many( 'tracks', 'ViewDeps::Result::Track', + { "foreign.cd" => "self.id" }, +); + +1; diff --git a/t/lib/ViewDeps/Result/Year2010CDsWithManyTracks.pm b/t/lib/ViewDeps/Result/Year2010CDsWithManyTracks.pm new file mode 100644 index 0000000..990fea7 --- /dev/null +++ b/t/lib/ViewDeps/Result/Year2010CDsWithManyTracks.pm @@ -0,0 +1,36 @@ +package # hide from PAUSE + ViewDeps::Result::Year2010CDsWithManyTracks; + +use strict; +use warnings; +use base 'ViewDeps::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,artwork.id FROM year_2010_cds cd,artwork artwork WHERE cd.number_tracks > 10 AND artwork.cd = cd.id" +); + +__PACKAGE__->result_source_instance->deploy_depends_on( + ["ViewDeps::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' }, + artwork => { data_type => 'integer' }, +); + +__PACKAGE__->set_primary_key('id'); + +__PACKAGE__->belongs_to( 'artist', 'ViewDeps::Result::Artist', + { "foreign.id" => "self.artist" }, +); + +__PACKAGE__->has_many( 'tracks', 'ViewDeps::Result::Track', + { "foreign.cd" => "self.id" }, +); + +1;