added db specific insertion code
Luke Saunders [Fri, 18 Jul 2008 16:36:02 +0000 (16:36 +0000)]
MANIFEST
lib/DBIx/Class/Fixtures.pm
lib/DBIx/Class/Fixtures/DBI.pm [new file with mode: 0644]
lib/DBIx/Class/Fixtures/DBI/Pg.pm [new file with mode: 0644]
lib/DBIx/Class/Fixtures/DBI/mysql.pm [new file with mode: 0644]
t/var/DBIxClass.db

index 9cc86ad..4686182 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -11,6 +11,8 @@ inc/Module/Install/Metadata.pm
 inc/Module/Install/Win32.pm
 inc/Module/Install/WriteAll.pm
 lib/DBIx/Class/Fixtures.pm
+lib/DBIx/Class/Fixtures/DBI.pm
+lib/DBIx/Class/Fixtures/DBI/Pg.pm
 lib/DBIx/Class/Fixtures/Schema.pm
 lib/DBIx/Class/Fixtures/SchemaVersioned.pm
 lib/DBIx/Class/Fixtures/Versioned.pm
@@ -27,8 +29,11 @@ t/05-dump-rules.t
 t/06-dump-date.t
 t/07-dump-all.t
 t/07-dump-includes.t
+t/08-dump-includes.t
+t/09-dump-scalar-ref.t
 t/12-populate-basic.t
 t/13populate-two-dbs.t
+t/14-populate-post.t
 t/lib/DBICTest.pm
 t/lib/DBICTest/Schema.pm
 t/lib/DBICTest/Schema/Artist.pm
@@ -42,12 +47,23 @@ t/lib/DBICTest/Schema2/Artist.pm
 t/lib/DBICTest/Schema2/CD.pm
 t/lib/DBICTest/Schema2/Friend.pm
 t/lib/mysql.sql
+t/lib/post_sqlite.sql
 t/lib/sqlite.sql
 t/lib/sqlite_different.sql
 t/var/configs/date.json
 t/var/configs/fetch.json
+t/var/configs/includes.json
 t/var/configs/quantity.json
 t/var/configs/rules.json
 t/var/configs/sample.json
+t/var/configs/scalar_ref.json
 t/var/configs/simple.json
-t/var/configs/includes.json
+t/var/DBIxClass.db
+t/var/DBIxClassDifferent.db
+t/var/fixtures/_dumper_version
+t/var/fixtures/artist/1.fix
+t/var/fixtures/artist/2.fix
+t/var/fixtures/cd/2.fix
+t/var/fixtures/cd/5.fix
+t/var/fixtures/track/14.fix
+t/var/fixtures/track/5.fix
index e85a545..20120d8 100644 (file)
@@ -339,9 +339,12 @@ Same as with L</SET ATTRIBUTES>
 
 =back
 
-Returns a new DBIx::Class::Fixture object. %attrs has only two valid keys at the
-moment - 'debug' which determines whether to be verbose and 'config_dir' which is required and much contain a valid path to
-the directory in which your .json configs reside.
+Returns a new DBIx::Class::Fixture object. %attrs can have the following parameters:
+
+- config_dir: required. must contain a valid path to the directory in which your .json configs reside.
+- debug: determines whether to be verbose
+- ignore_sql_errors: ignore errors on import of DDL etc
+
 
   my $fixtures = DBIx::Class::Fixtures->new({ config_dir => '/home/me/app/fixture_configs' });
 
@@ -367,7 +370,8 @@ sub new {
   my $self = {
               config_dir => $config_dir,
               _inherited_attributes => [qw/datetime_relative might_have rules/],
-              debug => $params->{debug}
+              debug => $params->{debug},
+              ignore_sql_errors => $params->{ignore_sql_errors}
   };
 
   bless $self, $class;
@@ -709,7 +713,7 @@ sub _generate_schema {
   my $data = _read_sql($ddl_file);
   foreach (@$data) {
     eval { $dbh->do($_) or warn "SQL was:\n $_"};
-         if ($@) { die "SQL was:\n $_\n$@"; }
+         if ($@ && !$self->{ignore_sql_errors}) { die "SQL was:\n $_\n$@"; }
   }
   $self->msg("- finished importing DDL into DB");
 
@@ -817,8 +821,6 @@ sub populate {
   $self->msg("- creating temp dir");
   dircopy(dir($fixture_dir, $schema->source($_)->from), dir($tmp_fixture_dir, $schema->source($_)->from)) for grep { -e dir($fixture_dir, $schema->source($_)->from) } $schema->sources;
 
-  eval { $schema->storage->dbh->do('SET foreign_key_checks=0') };
-
   my $fixup_visitor;
   my $formatter= $schema->storage->datetime_parser;
   unless ($@ || !$formatter) {
@@ -835,24 +837,37 @@ sub populate {
     $callbacks{object} ||= "visit_ref";        
     $fixup_visitor = new Data::Visitor::Callback(%callbacks);
   }
-  foreach my $source (sort $schema->sources) {
-    $self->msg("- adding " . $source);
-    my $rs = $schema->resultset($source);
-    my $source_dir = dir($tmp_fixture_dir, lc($rs->result_source->from));
-    next unless (-e $source_dir);
-    my @rows;
-    while (my $file = $source_dir->next) {
-      next unless ($file =~ /\.fix$/);
-      next if $file->is_dir;
-      my $contents = $file->slurp;
-      my $HASH1;
-      eval($contents);
-      $HASH1 = $fixup_visitor->visit($HASH1) if $fixup_visitor;
-      push(@rows, $HASH1);
-    }
-    $rs->populate(\@rows);
+
+  my $db = $schema->storage->dbh->{Driver}->{Name};
+  my $dbi_class = "DBIx::Class::Fixtures::DBI::$db";
+
+  eval "require $dbi_class";
+  if ($@) {
+    $dbi_class = "DBIx::Class::Fixtures::DBI";
+    eval "require $dbi_class";
+    die $@ if $@;
   }
 
+  $dbi_class->do_insert($schema, sub {
+    foreach my $source (sort $schema->sources) {
+      $self->msg("- adding " . $source);
+      my $rs = $schema->resultset($source);
+      my $source_dir = dir($tmp_fixture_dir, lc($rs->result_source->from));
+      next unless (-e $source_dir);
+      my @rows;
+      while (my $file = $source_dir->next) {
+        next unless ($file =~ /\.fix$/);
+        next if $file->is_dir;
+        my $contents = $file->slurp;
+        my $HASH1;
+        eval($contents);
+        $HASH1 = $fixup_visitor->visit($HASH1) if $fixup_visitor;
+        push(@rows, $HASH1);
+      }
+      $rs->populate(\@rows);
+    }
+  });
+
   $self->do_post_ddl({schema=>$schema, post_ddl=>$params->{post_ddl}}) if $params->{post_ddl};
 
   $self->msg("- fixtures imported");
@@ -870,7 +885,7 @@ sub do_post_ddl {
   my $data = _read_sql($params->{post_ddl});
   foreach (@$data) {
     eval { $schema->storage->dbh->do($_) or warn "SQL was:\n $_"};
-         if ($@) { die "SQL was:\n $_\n$@"; }
+         if ($@ && !$self->{ignore_sql_errors}) { die "SQL was:\n $_\n$@"; }
   }
   $self->msg("- finished importing post-populate DDL into DB");
 }
diff --git a/lib/DBIx/Class/Fixtures/DBI.pm b/lib/DBIx/Class/Fixtures/DBI.pm
new file mode 100644 (file)
index 0000000..b0ddf71
--- /dev/null
@@ -0,0 +1,12 @@
+package DBIx::Class::Fixtures::DBI;
+
+use strict;
+use warnings;
+
+sub do_insert {
+  my ($class, $schema, $sub) = @_;
+
+  $schema->txn_do($sub);
+}
+
+1;
diff --git a/lib/DBIx/Class/Fixtures/DBI/Pg.pm b/lib/DBIx/Class/Fixtures/DBI/Pg.pm
new file mode 100644 (file)
index 0000000..ecff87b
--- /dev/null
@@ -0,0 +1,18 @@
+package DBIx::Class::Fixtures::DBI::Pg;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class::Fixtures::DBI/;
+
+sub do_insert {
+  my ($class, $schema, $sub) = @_;
+
+  $schema->txn_do(
+    sub {
+      eval { $schema->storage->dbh->do('SET CONSTRAINTS ALL DEFERRED') };
+      $sub->();
+    }
+  );
+}
+
+1;
diff --git a/lib/DBIx/Class/Fixtures/DBI/mysql.pm b/lib/DBIx/Class/Fixtures/DBI/mysql.pm
new file mode 100644 (file)
index 0000000..538fa36
--- /dev/null
@@ -0,0 +1,19 @@
+package DBIx::Class::Fixtures::DBI::mysql;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class::Fixtures::DBI/;
+
+sub do_insert {
+  my ($class, $schema, $sub) = @_;
+
+  $schema->txn_do(
+    sub {
+      eval { $schema->storage->dbh->do('SET foreign_key_checks=0') };
+      $sub->();
+      eval { $schema->storage->dbh->do('SET foreign_key_checks=1') };
+    }
+  );
+}
+
+1;
index a0e978d..ef34238 100644 (file)
Binary files a/t/var/DBIxClass.db and b/t/var/DBIxClass.db differ