From ac6bae45084484cdef44619daf60aa6b9eb2d02a Mon Sep 17 00:00:00 2001 From: Alexander Hartmaier Date: Wed, 9 Feb 2011 18:14:46 +0100 Subject: [PATCH] fixed usage with moosified resultsets (RT#64773) --- Changes | 1 + lib/DBIx/Class/ResultSet/RecursiveUpdate.pm | 6 +- t/lib/DBSchema.pm | 61 +-------------------- t/lib/DBSchemaBase.pm | 61 +++++++++++++++++++++ t/lib/DBSchemaMoose.pm | 13 +++++ t/lib/DBSchemaMoose/ResultSet.pm | 7 +++ t/moosified-rs.t | 11 ++++ t/pg.t | 2 +- t/sqlite.t | 2 +- 9 files changed, 99 insertions(+), 65 deletions(-) create mode 100644 t/lib/DBSchemaBase.pm create mode 100644 t/lib/DBSchemaMoose.pm create mode 100644 t/lib/DBSchemaMoose/ResultSet.pm create mode 100644 t/moosified-rs.t diff --git a/Changes b/Changes index 656f3cb..bf46b91 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for DBIx::Class::ResultSet::RecursiveUpdate {{ $NEXT }} - Fixed updating of nullable has_many rels (RT#65561) + - Fixed usage with moosified resultsets (RT#64773) 0.21 2010-10-28 16:56:18 Europe/Vienna - Warn instead of throwing an exception if a key is neither diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index c0db8b4..94b1c07 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -107,7 +107,7 @@ sub recursive_update { $object = $self->find( $updates, { key => 'primary' } ); } - $object = $self->new( {} ) + $object = $self->new_result( {} ) unless defined $object; # warn Dumper( $updates ); use Data::Dumper; @@ -463,7 +463,7 @@ sub is_m2m { if ( $rclass->can('_m2m_metadata') ) { return $rclass->_m2m_metadata->{$relation}; } - my $object = $self->new( {} ); + my $object = $self->new_result( {} ); if ( $object->can($relation) and !$self->result_source->has_relationship($relation) and $object->can( 'set_' . $relation ) ) @@ -484,7 +484,7 @@ sub get_m2m_source { ->related_source( $rclass->_m2m_metadata->{$relation}{foreign_relation} ); } - my $object = $self->new( {} ); + my $object = $self->new_result( {} ); my $r = $object->$relation; return $r->result_source; } diff --git a/t/lib/DBSchema.pm b/t/lib/DBSchema.pm index af80fbf..3fa40d2 100644 --- a/t/lib/DBSchema.pm +++ b/t/lib/DBSchema.pm @@ -1,70 +1,11 @@ package DBSchema; -# Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 - use strict; use warnings; -use base 'DBIx::Class::Schema'; -use DateTime; +use base 'DBSchemaBase'; __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' ); -sub tables_exist { - my ( $dsn, $user, $pass ) = @_; - my $dbh = DBI->connect($dsn, $user, $pass, ); - return $dbh->tables( '%', '%', 'dvd', ); -} - - -sub get_test_schema { - my ( $dsn, $user, $pass ) = @_; - $dsn ||= 'dbi:SQLite:dbname=t/var/dvdzbr.db'; - warn "testing $dsn\n"; - my $schema = __PACKAGE__->connect( $dsn, $user, $pass, {} ); - my $deploy_attrs; - $deploy_attrs->{add_drop_table} = 1 if tables_exist( $dsn, $user, $pass ); - $schema->deploy( $deploy_attrs ); - $schema->populate('Personality', [ - [ qw/user_id / ], - [ '1'], - [ '2' ], - [ '3'], - ] - ); - $schema->populate('User', [ - [ qw/username name password / ], - [ 'jgda', 'Jonas Alves', ''], - [ 'isa' , 'Isa', '', ], - [ 'zby' , 'Zbyszek Lukasiak', ''], - ] - ); - $schema->populate('Tag', [ - [ qw/name file / ], - [ 'comedy', '' ], - [ 'dramat', '' ], - [ 'australian', '' ], - ] - ); - $schema->populate('Dvd', [ - [ qw/name imdb_id owner current_borrower creation_date alter_date / ], - [ 'Picnick under the Hanging Rock', 123, 1, 3, '2003-01-16 23:12:01', undef ], - [ 'The Deerhunter', 1234, 1, 1, undef, undef ], - [ 'Rejs', 1235, 3, 1, undef, undef ], - [ 'Seksmisja', 1236, 3, 1, undef, undef ], - ] - ); - $schema->populate( 'Dvdtag', [ - [ qw/ dvd tag / ], - [ 1, 2 ], - [ 1, 3 ], - [ 3, 1 ], - [ 4, 1 ], - ] - ); - return $schema; -} - - 1; diff --git a/t/lib/DBSchemaBase.pm b/t/lib/DBSchemaBase.pm new file mode 100644 index 0000000..5b19a6e --- /dev/null +++ b/t/lib/DBSchemaBase.pm @@ -0,0 +1,61 @@ +package DBSchemaBase; +use strict; +use warnings; + +use base 'DBIx::Class::Schema'; + +sub tables_exist { + my $dbh = shift; + # assume that all tables exist if table dvd is found + return $dbh->tables( '%', '%', 'dvd' ); +} + +sub get_test_schema { + my ( $class, $dsn, $user, $pass ) = @_; + $dsn ||= 'dbi:SQLite:dbname=t/var/dvdzbr.db'; + warn "testing $dsn\n"; + my $schema = $class->connect( $dsn, $user, $pass, {} ); + my $deploy_attrs; + $deploy_attrs->{add_drop_table} = 1 if tables_exist( $schema->storage->dbh ); + $schema->deploy( $deploy_attrs ); + $schema->populate('Personality', [ + [ qw/user_id / ], + [ '1'], + [ '2' ], + [ '3'], + ] + ); + $schema->populate('User', [ + [ qw/username name password / ], + [ 'jgda', 'Jonas Alves', ''], + [ 'isa' , 'Isa', '', ], + [ 'zby' , 'Zbyszek Lukasiak', ''], + ] + ); + $schema->populate('Tag', [ + [ qw/name file / ], + [ 'comedy', '' ], + [ 'dramat', '' ], + [ 'australian', '' ], + ] + ); + $schema->populate('Dvd', [ + [ qw/name imdb_id owner current_borrower creation_date alter_date / ], + [ 'Picnick under the Hanging Rock', 123, 1, 3, '2003-01-16 23:12:01', undef ], + [ 'The Deerhunter', 1234, 1, 1, undef, undef ], + [ 'Rejs', 1235, 3, 1, undef, undef ], + [ 'Seksmisja', 1236, 3, 1, undef, undef ], + ] + ); + $schema->populate( 'Dvdtag', [ + [ qw/ dvd tag / ], + [ 1, 2 ], + [ 1, 3 ], + [ 3, 1 ], + [ 4, 1 ], + ] + ); + return $schema; +} + +1; diff --git a/t/lib/DBSchemaMoose.pm b/t/lib/DBSchemaMoose.pm new file mode 100644 index 0000000..d662624 --- /dev/null +++ b/t/lib/DBSchemaMoose.pm @@ -0,0 +1,13 @@ +package DBSchemaMoose; + +use strict; +use warnings; + +use base 'DBSchemaBase'; + +__PACKAGE__->load_namespaces( + result_namespace => '+DBSchema::Result', + default_resultset_class => '+DBSchemaMoose::ResultSet', +); + +1; diff --git a/t/lib/DBSchemaMoose/ResultSet.pm b/t/lib/DBSchemaMoose/ResultSet.pm new file mode 100644 index 0000000..6bb411e --- /dev/null +++ b/t/lib/DBSchemaMoose/ResultSet.pm @@ -0,0 +1,7 @@ +package DBSchemaMoose::ResultSet; +use namespace::autoclean; +use Moose; +use MooseX::NonMoose; +extends qw/DBIx::Class::ResultSet::RecursiveUpdate DBIx::Class::ResultSet/; +__PACKAGE__->meta->make_immutable; +1; diff --git a/t/moosified-rs.t b/t/moosified-rs.t new file mode 100644 index 0000000..c902048 --- /dev/null +++ b/t/moosified-rs.t @@ -0,0 +1,11 @@ +# -*- perl -*- + +use lib 't/lib'; +use DBSchemaMoose; +use RunTests; +use Test::More; + +my $schema = DBSchemaMoose->get_test_schema(); + +run_tests( $schema); + diff --git a/t/pg.t b/t/pg.t index 7a2294d..7c4f051 100644 --- a/t/pg.t +++ b/t/pg.t @@ -10,7 +10,7 @@ my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/}; plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test' . ' (note: creates and tables!)' unless ($dsn && $user); -my $schema = DBSchema::get_test_schema( $dsn, $user, $pass ); +my $schema = DBSchema->get_test_schema( $dsn, $user, $pass ); run_tests( $schema ); diff --git a/t/sqlite.t b/t/sqlite.t index ca3e72e..93baaed 100644 --- a/t/sqlite.t +++ b/t/sqlite.t @@ -6,6 +6,6 @@ use RunTests; use Test::More; #unlink 't/var/dvdzbr.db'; -my $schema = DBSchema::get_test_schema(); +my $schema = DBSchema->get_test_schema(); run_tests( $schema ); -- 2.45.2