two schemas populate issue fixed
Luke Saunders [Tue, 5 Feb 2008 14:42:02 +0000 (14:42 +0000)]
lib/DBIx/Class/Fixtures.pm
t/13populate-two-dbs.t [new file with mode: 0644]

index 8766688..ca0aa2e 100644 (file)
@@ -15,13 +15,11 @@ use File::Copy::Recursive qw/dircopy/;
 use File::Copy qw/move/;
 use Hash::Merge qw( merge );
 use Data::Dumper;
+use Class::C3::Componentised;
 
 use base qw(Class::Accessor::Grouped);
 
-our %db_to_parser = (
-  'mysql'      => 'DateTime::Format::MySQL',
-  'pg'         => 'DateTime::Format::Pg',
-);
+our $namespace_counter = 0;
 
 __PACKAGE__->mk_group_accessors( 'simple' => qw/config_dir _inherited_attributes debug schema_class/);
 
@@ -610,10 +608,14 @@ sub _generate_schema {
 
   my $pre_schema;
   my $connection_details = $params->{connection_details};
-  unless( $pre_schema = $schema_class->connect(@{$connection_details}) ) {
+  $namespace_counter++;
+  my $namespace = "DBIx::Class::Fixtures::GeneratedSchema_" . $namespace_counter;
+  Class::C3::Componentised->inject_base( $namespace => $schema_class );
+  $pre_schema = $namespace->connect(@{$connection_details});
+  unless( $pre_schema ) {
     return DBIx::Class::Exception->throw('connection details not valid');
   }
-  my @tables = map { $pre_schema->source($_)->from }$pre_schema->sources;
+  my @tables = map { $pre_schema->source($_)->from } $pre_schema->sources;
   my $dbh = $pre_schema->storage->dbh;
 
   # clear existing db
@@ -639,7 +641,7 @@ sub _generate_schema {
 
   # load schema object from our new DB
   $self->msg("- loading fresh DBIC object from DB");
-  my $schema = $schema_class->connect(@{$connection_details});
+  my $schema = $namespace->connect(@{$connection_details});
   return $schema;
 }
 
diff --git a/t/13populate-two-dbs.t b/t/13populate-two-dbs.t
new file mode 100644 (file)
index 0000000..ca93d8b
--- /dev/null
@@ -0,0 +1,26 @@
+#!perl
+
+use DBIx::Class::Fixtures;
+use Test::More tests => 7;
+use lib qw(t/lib);
+use DBICTest;
+use Path::Class;
+use Data::Dumper;
+use DBICTest::Schema2;
+
+# set up and populate normal schema
+ok(my $schema = DBICTest->init_schema(), 'got schema');
+my $config_dir = 't/var/configs';
+
+my @different_connection_details = ('dbi:SQLite:t/var/DBIxClassDifferent.db', '', '');
+ok(my $schema2 = DBICTest::Schema2->compose_namespace('DBICTest2')->connect(@different_connection_details));
+unlink('t/var/DBIxClassDifferent.db') if (-e 't/var/DBIxClassDifferent.db');
+DBICTest->deploy_schema($schema2, 't/lib/sqlite_different.sql');
+# do dump
+ok(my $fixtures = DBIx::Class::Fixtures->new({ config_dir => $config_dir, debug => 0 }), 'object created with correct config dir');
+ok($fixtures->dump({ config => "simple.json", schema => $schema, directory => 't/var/fixtures' }), "simple dump executed okay");
+
+ok($fixtures->populate({ ddl => 't/lib/sqlite_different.sql', connection_details => [@different_connection_details], directory => 't/var/fixtures' }), 'mysql populate okay');
+ok($fixtures->populate({ ddl => 't/lib/sqlite.sql', connection_details => ['dbi:SQLite:t/var/DBIxClass.db', '', ''], directory => 't/var/fixtures' }), 'sqlite populate okay');
+
+is($schema->resultset('Artist')->count, 1, 'artist imported to sqlite okay');