Merge 'trunk' into 'DBIx-Class-current'
Brandon L. Black [Mon, 11 Dec 2006 14:24:48 +0000 (14:24 +0000)]
r12251@localhost (orig r2961):  ningu | 2006-12-09 14:39:02 -0600
- die in Storage::DBI::Pg when it can't find the autoinc sequence
r12253@localhost (orig r2963):  ningu | 2006-12-10 10:25:22 -0600
add test for has_many prefetch with adjacent rows with no rel objects
r12254@localhost (orig r2964):  ningu | 2006-12-10 10:26:59 -0600
whoops
r12255@localhost (orig r2965):  blblack | 2006-12-10 19:00:22 -0600
allow pk mutation via column accessor + update
r12316@localhost (orig r2966):  blblack | 2006-12-11 08:24:23 -0600
better fix for pk mutation based on mst irc conversation

lib/DBIx/Class/Row.pm
lib/DBIx/Class/Storage/DBI/Pg.pm
t/69update.t
t/76joins.t
t/94pk_mutation.t

index bed11a0..bd59293 100644 (file)
@@ -97,6 +97,7 @@ sub insert {
   $self->in_storage(1);
   $self->{_dirty_columns} = {};
   $self->{related_resultsets} = {};
+  undef $self->{_orig_ident};
   return $self;
 }
 
@@ -154,7 +155,7 @@ sub update {
   my %to_update = $self->get_dirty_columns;
   return $self unless keys %to_update;
   my $rows = $self->result_source->storage->update(
-               $self->result_source->from, \%to_update, $ident_cond);
+               $self->result_source->from, \%to_update, $self->{_orig_ident} || $ident_cond);
   if ($rows == 0) {
     $self->throw_exception( "Can't update ${self}: row not found" );
   } elsif ($rows > 1) {
@@ -162,6 +163,7 @@ sub update {
   }
   $self->{_dirty_columns} = {};
   $self->{related_resultsets} = {};
+  undef $self->{_orig_ident};
   return $self;
 }
 
@@ -290,6 +292,7 @@ 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);
   my $ret = $self->store_column(@_);
   $self->{_dirty_columns}{$column} = 1
index 0c98f91..bec2a8f 100644 (file)
@@ -21,6 +21,9 @@ sub _dbh_last_insert_id {
 sub last_insert_id {
   my ($self,$source,$col) = @_;
   my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
+  $self->throw_exception("could not fetch primary key for " . $source->name . ", could not "
+    . "get autoinc sequence for $col (check that table and column specifications are correct "
+    . "and in the correct case)") unless defined $seq;
   $self->dbh_do($self->can('_dbh_last_insert_id'), $seq);
 }
 
index b11ebde..4686876 100644 (file)
@@ -9,7 +9,7 @@ my $schema = DBICTest->init_schema();
 
 BEGIN {
         eval "use DBD::SQLite";
-        plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 5);
+        plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 6);
 }                                                                               
 
 my $art = $schema->resultset("Artist")->find(1);
@@ -30,3 +30,7 @@ $art->discard_changes;
 ok($art->update({ artistid => 100 }), 'update allows pk mutation');
 
 is($art->artistid, 100, 'pk mutation applied');
+
+my $art_100 = $schema->resultset("Artist")->find(100);
+$art_100->artistid(101);
+ok($art_100->update(), 'update allows pk mutation via column accessor');
index 806185c..96836fb 100644 (file)
@@ -16,7 +16,7 @@ BEGIN {
     eval "use DBD::SQLite";
     plan $@
         ? ( skip_all => 'needs DBD::SQLite for testing' )
-        : ( tests => 52 );
+        : ( tests => 53 );
 }
 
 # figure out if we've got a version of sqlite that is older than 3.2.6, in
@@ -364,6 +364,12 @@ like( $sql, qr/^SELECT tracks_2\.trackid/, "join not collapsed for search_relate
 $schema->storage->debug($orig_debug);
 $schema->storage->debugobj->callback(undef);
 
+$rs = $schema->resultset('Artist');
+$rs->create({ artistid => 4, name => 'Unknown singer-songwriter' });
+$rs->create({ artistid => 5, name => 'Emo 4ever' });
+@artists = $rs->search(undef, { prefetch => 'cds', order_by => 'artistid' });
+is(scalar @artists, 5, 'has_many prefetch with adjacent empty rows ok');
+
 # -------------
 #
 # Tests for multilevel has_many prefetch
index 4623332..133a27b 100644 (file)
@@ -7,7 +7,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 5;
+plan tests => 10;
 
 my $old_artistid = 1;
 my $new_artistid = $schema->resultset("Artist")->get_column('artistid')->max + 1;
@@ -33,3 +33,30 @@ my $new_artistid = $schema->resultset("Artist")->get_column('artistid')->max + 1
   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');
+}