From: Brandon L. Black Date: Mon, 11 Dec 2006 14:24:23 +0000 (+0000) Subject: better fix for pk mutation based on mst irc conversation X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=729b29ae7135017f7d4f3204c691ec3a0c3120dc;p=dbsrgits%2FDBIx-Class-Historic.git better fix for pk mutation based on mst irc conversation --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index ee4e84c..73500bd 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -79,6 +79,7 @@ sub insert { $self->in_storage(1); $self->{_dirty_columns} = {}; $self->{related_resultsets} = {}; + undef $self->{_orig_ident}; return $self; } @@ -125,6 +126,7 @@ sub update { } $self->{_dirty_columns} = {}; $self->{related_resultsets} = {}; + undef $self->{_orig_ident}; return $self; } @@ -241,19 +243,8 @@ the column is marked as dirty for when you next call $obj->update. sub set_column { my $self = shift; my ($column) = @_; + $self->{_orig_ident} ||= $self->ident_condition; my $old = $self->get_column($column); - - # save our original ident condition if - # they modify any part of the PK - if(!$self->{_orig_ident}) { - foreach ($self->primary_columns) { - if($_ eq $column) { - $self->{_orig_ident} = $self->ident_condition; - last; - } - } - } - my $ret = $self->store_column(@_); $self->{_dirty_columns}{$column} = 1 if (defined $old ^ defined $ret) || (defined $old && $old ne $ret); diff --git a/t/94pk_mutation.t b/t/94pk_mutation.t new file mode 100644 index 0000000..133a27b --- /dev/null +++ b/t/94pk_mutation.t @@ -0,0 +1,62 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +my $schema = DBICTest->init_schema(); + +plan tests => 10; + +my $old_artistid = 1; +my $new_artistid = $schema->resultset("Artist")->get_column('artistid')->max + 1; + +# Update the PK +{ + my $artist = $schema->resultset("Artist")->find($old_artistid); + ok(defined $artist, 'found an artist with the new PK'); + + $artist->update({ artistid => $new_artistid }); + is($artist->artistid, $new_artistid, 'artist ID matches'); +} + +# Look for the old PK +{ + my $artist = $schema->resultset("Artist")->find($old_artistid); + ok(!defined $artist, 'no artist found with the old PK'); +} + +# Look for the new PK +{ + my $artist = $schema->resultset("Artist")->find($new_artistid); + ok(defined $artist, 'found an artist with the new PK'); + is($artist->artistid, $new_artistid, 'artist ID matches'); +} + +# Do it all over again, using a different methodology: +$old_artistid = $new_artistid; +$new_artistid++; + +# Update the PK +{ + my $artist = $schema->resultset("Artist")->find($old_artistid); + ok(defined $artist, 'found an artist with the new PK'); + + $artist->artistid($new_artistid); + $artist->update; + is($artist->artistid, $new_artistid, 'artist ID matches'); +} + +# Look for the old PK +{ + my $artist = $schema->resultset("Artist")->find($old_artistid); + ok(!defined $artist, 'no artist found with the old PK'); +} + +# Look for the new PK +{ + my $artist = $schema->resultset("Artist")->find($new_artistid); + ok(defined $artist, 'found an artist with the new PK'); + is($artist->artistid, $new_artistid, 'artist ID matches'); +}