From: Peter Rabbitson Date: Tue, 7 Jun 2011 07:58:05 +0000 (+0200) Subject: Augment shorsighted code change in 5ef76b8b X-Git-Tag: v0.08193~23 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=867f1b28c4934e62ad3545c8607b2bf4c145b34a;p=dbsrgits%2FDBIx-Class.git Augment shorsighted code change in 5ef76b8b The way _column_data_in_storage was unconditionally referenced, would break both id() and ident_values(), which are supposed to construct the identity based on current object state --- diff --git a/lib/DBIx/Class/Ordered.pm b/lib/DBIx/Class/Ordered.pm index 54c6d46..2502e93 100644 --- a/lib/DBIx/Class/Ordered.pm +++ b/lib/DBIx/Class/Ordered.pm @@ -768,9 +768,10 @@ excluding the object you called this method on. sub _siblings { my $self = shift; my $position_column = $self->position_column; - return defined (my $pos = $self->get_column($position_column)) + my $pos; + return defined ($pos = $self->get_column($position_column)) ? $self->_group_rs->search( - { $position_column => { '!=' => $self->get_column($position_column) } }, + { $position_column => { '!=' => $pos } }, ) : $self->_group_rs ; diff --git a/lib/DBIx/Class/PK.pm b/lib/DBIx/Class/PK.pm index 722dcf6..db9b8a1 100644 --- a/lib/DBIx/Class/PK.pm +++ b/lib/DBIx/Class/PK.pm @@ -36,12 +36,12 @@ sub id { } sub _ident_values { - my ($self) = @_; + my ($self, $use_storage_state) = @_; my (@ids, @missing); for ($self->_pri_cols) { - push @ids, exists $self->{_column_data_in_storage}{$_} + push @ids, ($use_storage_state and exists $self->{_column_data_in_storage}{$_}) ? $self->{_column_data_in_storage}{$_} : $self->get_column($_) ; @@ -103,10 +103,18 @@ Produces a condition hash to locate a row based on the primary key(s). =cut sub ident_condition { - my ($self, $alias) = @_; + shift->_mk_ident_cond(@_); +} + +sub _storage_ident_condition { + shift->_mk_ident_cond(shift, 1); +} + +sub _mk_ident_cond { + my ($self, $alias, $use_storage_state) = @_; my @pks = $self->_pri_cols; - my @vals = $self->_ident_values; + my @vals = $self->_ident_values($use_storage_state); my (%cond, @undef); my $prefix = defined $alias ? $alias.'.' : ''; diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 1984d6c..ea6ea09 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -499,7 +499,7 @@ sub update { $self->throw_exception( "Not in database" ) unless $self->in_storage; my $rows = $self->result_source->storage->update( - $self->result_source, \%to_update, $self->ident_condition + $self->result_source, \%to_update, $self->_storage_ident_condition ); if ($rows == 0) { $self->throw_exception( "Can't update ${self}: row not found" ); @@ -561,7 +561,7 @@ sub delete { $self->throw_exception( "Not in database" ) unless $self->in_storage; $self->result_source->storage->delete( - $self->result_source, $self->ident_condition + $self->result_source, $self->_storage_ident_condition ); delete $self->{_column_data_in_storage}; @@ -1370,7 +1370,7 @@ sub get_from_storage { $resultset = $resultset->search(undef, $attrs); } - return $resultset->find($self->ident_condition); + return $resultset->find($self->_storage_ident_condition); } =head2 discard_changes ($attrs?) diff --git a/t/update/ident_cond.t b/t/update/ident_cond.t new file mode 100644 index 0000000..b79d56b --- /dev/null +++ b/t/update/ident_cond.t @@ -0,0 +1,34 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +my $schema = DBICTest->init_schema(); + +my $artist = $schema->resultset('Artist')->next; + +is_deeply( + [ $artist->id, $artist->ident_condition, $artist->_storage_ident_condition ], + [ 1, { artistid => 1 }, { artistid => 1 } ], + 'Correct identity state of freshly retrieved object', +); + +$artist->artistid(888); + +is_deeply( + [ $artist->id, $artist->ident_condition, $artist->_storage_ident_condition ], + [ 888, { artistid => 888 }, { artistid => 1 } ], + 'Correct identity state of object with modified PK', +); + +$artist->update; + +is_deeply( + [ $artist->id, $artist->ident_condition, $artist->_storage_ident_condition ], + [ 888, { artistid => 888 }, { artistid => 888 } ], + 'Correct identity state after storage update', +); + +done_testing;