if there is a sequence, set it correctly
John Napiorkowski [Thu, 13 Sep 2012 19:15:56 +0000 (15:15 -0400)]
Changes
lib/DBIx/Class/Fixtures.pm

diff --git a/Changes b/Changes
index a73508a..1154bad 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,9 +1,10 @@
 Revision history for DBIx-Class-Fixtures
 
 1.001016
-- Support more Pg types for datetime_relative (added TIME, DATE, INTERVAL,
-  TIMESTAMP)
-- experimental support for PG SERIAL columns and sequences.
+- Support more Postgresql types for datetime_relative (added TIME, DATE,
+  INTERVAL, TIMESTAMP)
+- If $result_source->column_info defines a sequence, make sure we properly
+  set that sequence to whatever the max value currently is.
 
 1.001015
 - ::External::File makes the path if its missing
index 4b1f137..5e0ae7b 100644 (file)
@@ -1397,11 +1397,31 @@ sub populate {
           }
         }
         $rs->populate(\@rows) if scalar(@rows);
+
+        ## Now we need to do some db specific cleanup
+        ## this probably belongs in a more isolated space.  Right now this is
+        ## to just handle postgresql SERIAL types that use Sequences
+
+        my $table = $rs->result_source->name;
+        for my $column(my @columns =  $rs->result_source->columns) {
+          my $info = $rs->result_source->column_info($column);
+          if(my $sequence = $info->{sequence}) {
+             $self->msg("- updating sequence $sequence");
+            $rs->result_source->storage->dbh_do(sub {
+              my ($storage, $dbh, @cols) = @_;
+              $self->msg(my $sql = "SELECT setval('${sequence}', (SELECT max($column) FROM ${table}));");
+              my $sth = $dbh->prepare($sql);
+              my $rv = $sth->execute or die $sth->errstr;
+              $self->msg("- $sql");
+            });
+          }
+        }
+
       }
     });
   });
   $self->do_post_ddl( {
-    schema=>$schema, 
+    schema=>$schema,
     post_ddl=>$params->{post_ddl}
   } ) if $params->{post_ddl};