Skip to content

Commit

Permalink
Augment shorsighted code change in 5ef76b8
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ribasushi committed Jun 7, 2011
1 parent 7077c96 commit 867f1b2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/DBIx/Class/Ordered.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
;
Expand Down
16 changes: 12 additions & 4 deletions lib/DBIx/Class/PK.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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($_)
;
Expand Down Expand Up @@ -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.'.' : '';
Expand Down
6 changes: 3 additions & 3 deletions lib/DBIx/Class/Row.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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?)
Expand Down
34 changes: 34 additions & 0 deletions t/update/ident_cond.t
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 867f1b2

Please sign in to comment.