From: zby Date: Sun, 25 Jan 2009 00:58:55 +0000 (+0000) Subject: fix for a new record that belongs to an old one; accepting row objects in the updates... X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=264a28fa4c2fdab1244cdba5c9e606ce8c411c0a;p=chaz%2Fp5-DBIx-Class-ResultSet-RecursiveUpdate fix for a new record that belongs to an old one; accepting row objects in the updates structure --- diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index ebdfbf8..a152dc0 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -5,13 +5,30 @@ use version; $VERSION = qv('0.001'); use warnings; use strict; use Carp; +use Scalar::Util qw( blessed ); use base qw(DBIx::Class::ResultSet); sub recursive_update { - my( $self, $updates ) = @_; + my( $self, $updates, $fixed_fields ) = @_; + if( blessed( $updates ) && $updates->isa( 'DBIx::Class::Row' ) ){ + return $updates; + } my $object; - $object = $self->find( $updates, { key => 'primary' } ) || $self->new( {} ); +# warn 'cond: ' . Dumper( $self->{cond} ); use Data::Dumper; +# warn 'where: ' . Dumper( $self->{attrs}{where} ); use Data::Dumper; + my @missing = grep { !exists $updates->{$_} && !exists $fixed_fields->{$_} } $self->result_source->primary_columns; + if( defined $self->{cond} && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $self->{cond} ){ + $self->{cond} = undef; + $self->{attrs}{where} = undef; + if( ! scalar @missing ){ + $object = $self->find( $updates, { key => 'primary' } ); + } + } + else{ + $object = $self->find( $updates, { key => 'primary' } ); + } + $object ||= $self->new( {} ); for my $name ( keys %$updates ){ if($object->can($name)){ @@ -46,6 +63,7 @@ sub recursive_update { $self->_delete_empty_auto_increment($object); # don't allow insert to recurse to related objects - we do the recursion ourselves $object->{_rel_in_storage} = 1; +# warn Dumper( $object->{_column_data} ); $object->update_or_insert; # updating relations that can be done only after the row is inserted into the database @@ -70,7 +88,7 @@ sub recursive_update { } elsif( $object->result_source->has_relationship($name) ){ my $info = $object->result_source->relationship_info( $name ); - # has many case + # has many case (and similar) if( ref $updates->{$name} eq 'ARRAY' ){ for my $sub_updates ( @{$updates->{$name}} ) { my $sub_object = $object->search_related( $name )->recursive_update( $sub_updates ); @@ -171,8 +189,6 @@ sub _master_relation_cond { return; } -# Module implementation here - 1; # Magic true value required at end of module __END__ diff --git a/t/lib/DBSchema/Result/Dvd.pm b/t/lib/DBSchema/Result/Dvd.pm index 4908080..f761f76 100644 --- a/t/lib/DBSchema/Result/Dvd.pm +++ b/t/lib/DBSchema/Result/Dvd.pm @@ -42,14 +42,16 @@ __PACKAGE__->add_columns( }, ); __PACKAGE__->set_primary_key('id'); -__PACKAGE__->belongs_to('owner', 'User', { id => 'owner' }); -__PACKAGE__->belongs_to('current_borrower', 'User', { id => 'current_borrower' }); +__PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', { id => 'owner' }); +__PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', { id => 'current_borrower' }); __PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.id' }); +__PACKAGE__->has_many('viewings', 'Viewing', { 'foreign.dvd_id' => 'self.id' }); __PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag'); __PACKAGE__->might_have( liner_notes => 'DBSchema::Result::LinerNotes', undef, { proxy => [ qw/notes/ ] }, ); +__PACKAGE__->add_relationship('like_has_many', 'DBSchema::Result::Twokeys', { 'foreign.dvd_name' => 'self.name' }, { accessor_name => 'like_has_many' } ); 1; diff --git a/t/lib/DBSchema/Result/Twokeys.pm b/t/lib/DBSchema/Result/Twokeys.pm new file mode 100644 index 0000000..7d71d01 --- /dev/null +++ b/t/lib/DBSchema/Result/Twokeys.pm @@ -0,0 +1,22 @@ +package DBSchema::Result::Twokeys; + +# Created by DBIx::Class::Schema::Loader v0.03000 @ 2006-10-02 08:24:09 + +use strict; +use warnings; + +use base 'DBIx::Class'; + +__PACKAGE__->load_components("PK::Auto", "Core"); +__PACKAGE__->table("twokeys"); +__PACKAGE__->add_columns( + "dvd_name" => { data_type => 'varchar', size => 100 }, + "key2" => { data_type => 'integer' }, +); +__PACKAGE__->set_primary_key("dvd_name", "key2"); + +__PACKAGE__->add_relationship('like_belongs_to', 'DBSchema::Result::Dvd', { 'foreign.name' => 'self.dvd_name' }, ); + + +1; + diff --git a/t/lib/RunTests.pm b/t/lib/RunTests.pm index f3bfbb1..2971bde 100644 --- a/t/lib/RunTests.pm +++ b/t/lib/RunTests.pm @@ -5,14 +5,34 @@ use Exporter 'import'; # gives you Exporter's import() method directly use strict; use Test::More; - sub run_tests{ my $schema = shift; my $dvd_rs = $schema->resultset( 'Dvd' ); - my $owner = $schema->resultset( 'User' )->first; - my $initial_user_count = $schema->resultset( 'User' )->count; + my $user_rs = $schema->resultset( 'User' ); + + my $owner = $user_rs->next; + my $another_owner = $user_rs->next; + my $initial_user_count = $user_rs->count; + + # creating new record linked to some old record + + my $updates; + $updates = { + id => undef, + name => 'Test name 2', + viewings => [ { user_id => $owner->id } ], + owner => { id => $owner->id }, + }; + + my $new_dvd = $dvd_rs->recursive_update( $updates ); +# my $new_dvd = $dvd_rs->create( $updates ); + is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" ); + is ( $new_dvd->name, 'Test name 2', 'Dvd name set' ); + is ( $new_dvd->owner->id, $owner->id, 'Owner set' ); +# is ( $new_dvd->viewing->user_id, $owner->id, 'Viewing created' ); +; # creating new records my $updates = { @@ -20,44 +40,40 @@ sub run_tests{ aaaa => undef, tags => [ '2', { id => '3' } ], name => 'Test name', - # 'creation_date.year' => 2002, - # 'creation_date.month' => 1, - # 'creation_date.day' => 3, - # 'creation_date.hour' => 4, - # 'creation_date.minute' => 33, - # 'creation_date.pm' => 1, - owner => $owner->id, + owner => $owner, current_borrower => { name => 'temp name', username => 'temp name', password => 'temp name', }, liner_notes => { - notes => 'test note', - } + }, + like_has_many => [ + { key2 => 1 } + ], }; my $dvd = $dvd_rs->recursive_update( $updates ); - +; is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" ); is ( $dvd->name, 'Test name', 'Dvd name set' ); is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' ); - #my $value = $dvd->creation_date; - #is( "$value", '2002-01-03T16:33:00', 'Date set'); is ( $dvd->owner->id, $owner->id, 'Owner set' ); is ( $dvd->current_borrower->name, 'temp name', 'Related record created' ); is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' ); - + ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' ); + # changing existing records + my $num_of_users = $user_rs->count; $updates = { id => $dvd->id, aaaa => undef, name => 'Test name', tags => [ ], - 'owner' => $owner->id, + 'owner' => $another_owner->id, current_borrower => { username => 'new name a', name => 'new name a', @@ -68,7 +84,7 @@ sub run_tests{ is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" ); is ( $dvd->name, 'Test name', 'Dvd name set' ); - is ( $dvd->owner->id, $owner->id, 'Owner set' ); + is ( $dvd->owner->id, $another_owner->id, 'Owner updated' ); is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' ); is ( $dvd->tags->count, 0, 'Tags deleted' ); @@ -93,7 +109,6 @@ sub run_tests{ ] }; - my $user_rs = $schema->resultset( 'User' ); my $user = $user_rs->recursive_update( $updates ); my %owned_dvds = map { $_->name => $_ } $user->owned_dvds; is( scalar keys %owned_dvds, 2, 'Has many relations created' ); @@ -101,4 +116,5 @@ sub run_tests{ my @tags = $owned_dvds{'temp name 1'}->tags; is( scalar @tags, 2, 'Tags in has_many related record saved' ); ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' ); + } diff --git a/t/pg.t b/t/pg.t index 8765d41..58ce124 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); -plan tests => 15; +plan tests => 19; my $schema = DBSchema::get_test_schema( $dsn, $user, $pass ); diff --git a/t/sqlite.t b/t/sqlite.t index 835ecd1..45eca31 100644 --- a/t/sqlite.t +++ b/t/sqlite.t @@ -4,7 +4,7 @@ use lib 't/lib'; use DBSchema; use RunTests; use Test::More; -plan tests => 15; +plan tests => 19; my $schema = DBSchema::get_test_schema(); run_tests( $schema ); diff --git a/t/var/dvdzbr.db b/t/var/dvdzbr.db index 3ff59f1..430d2c0 100644 Binary files a/t/var/dvdzbr.db and b/t/var/dvdzbr.db differ