lots of updates to make the test suite work with databases other than sqlite
John Napiorkowski [Thu, 8 May 2008 20:26:01 +0000 (20:26 +0000)]
lib/SQL/Translator/Parser/DBIx/Class.pm
t/93storage_replication.t
t/lib/DBICTest.pm
t/lib/DBICTest/Schema/ForceForeign.pm
t/lib/DBICTest/Schema/TreeLike.pm
t/lib/DBICTest/Schema/TwoKeys.pm

index 468256e..996d461 100644 (file)
@@ -122,8 +122,15 @@ sub parse {
             my $rel_table = $othertable->name;
 
             # Get the key information, mapping off the foreign/self markers
-            my @cond = keys(%{$rel_info->{cond}});
+            my @cond = keys(%{$rel_info->{cond}}); 
             my @refkeys = map {/^\w+\.(\w+)$/} @cond;
+            
+            # Force the order of the referenced fields to be the same as
+            # ->add_columns method.
+            my $idx;
+            my %other_columns_idx = map {$_ => $idx++} $othertable->columns;
+            @refkeys = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } @refkeys; 
+            
             my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
 
             if($rel_table)
index 2d1e58b..26f6039 100644 (file)
@@ -16,6 +16,18 @@ use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
 use_ok 'DBIx::Class::Storage::DBI::Replicated';
 
+=head1 HOW TO USE
+
+    This is a test of the replicated storage system.  This will work in one of
+    two ways, either it was try to fake replication with a couple of SQLite DBs
+    and creative use of copy, or if you define a couple of %ENV vars correctly
+    will try to test those.  If you do that, it will assume the setup is properly
+    replicating.  Your results may vary, but I have demonstrated this to work with
+    mysql native replication.
+    
+=cut
+
+
 ## ----------------------------------------------------------------------------
 ## Build a class to hold all our required testing data and methods.
 ## ----------------------------------------------------------------------------
@@ -47,12 +59,17 @@ TESTSCHEMACLASSES: {
     
     sub init_schema {
         my $class = shift @_;
+        
         my $schema = DBICTest->init_schema(
             storage_type=>[
                '::DBI::Replicated' => {
                        balancer_type=>'::Random',
-               }],
-            );
+               }
+            ],
+            deploy_args=>{
+                   add_drop_table => 1,
+            },
+        );
 
         return $schema;
     }
@@ -202,10 +219,12 @@ isa_ok $replicated_storages[0]
 isa_ok $replicated_storages[1]
     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
     
-isa_ok $replicated->schema->storage->replicants->{"t/var/DBIxClass_slave1.db"}
+my @replicant_names = keys %{$replicated->schema->storage->replicants};
+    
+isa_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
 
-isa_ok $replicated->schema->storage->replicants->{"t/var/DBIxClass_slave2.db"}
+isa_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';  
 
 ## Add some info to the database
@@ -245,11 +264,19 @@ $replicated
         [ 7, "Watergate"],
     ]);
 
-## Alright, the database 'cluster' is not in a consistent state.  When we do
-## a read now we expect bad news
-
-is $replicated->schema->resultset('Artist')->find(5), undef
-    => 'read after disconnect fails because it uses a replicant which we have neglected to "replicate" yet';
+SKIP: {
+    ## We can't do this test if we have a custom replicants, since we assume
+    ## if there are custom one that you are trying to test a real replicating
+    ## system.  See docs above for more.
+    
+    skip 'Cannot test inconsistent replication since you have a real replication system', 1
+     if DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
+    
+       ## Alright, the database 'cluster' is not in a consistent state.  When we do
+       ## a read now we expect bad news    
+    is $replicated->schema->resultset('Artist')->find(5), undef
+    => 'read after disconnect fails because it uses a replicant which we have neglected to "replicate" yet'; 
+}
 
 ## Make sure all the slaves have the table definitions
 $replicated->replicate;
@@ -270,8 +297,8 @@ is $artist2->name, "Doom's Children"
 is $replicated->schema->storage->pool->connected_replicants => 2
     => "both replicants are connected";
     
-$replicated->schema->storage->replicants->{"t/var/DBIxClass_slave1.db"}->disconnect;
-$replicated->schema->storage->replicants->{"t/var/DBIxClass_slave2.db"}->disconnect;
+$replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
+$replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
 
 is $replicated->schema->storage->pool->connected_replicants => 0
     => "both replicants are now disconnected";
@@ -324,8 +351,8 @@ ok $replicated->schema->resultset('Artist')->find(2)
 ## set all the replicants to inactive, and make sure the balancer falls back to
 ## the master.
 
-$replicated->schema->storage->replicants->{"t/var/DBIxClass_slave1.db"}->active(0);
-$replicated->schema->storage->replicants->{"t/var/DBIxClass_slave2.db"}->active(0);
+$replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
+$replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
     
 ok $replicated->schema->resultset('Artist')->find(2)
     => 'Fallback to master'; 
index d33f336..516c299 100755 (executable)
@@ -89,11 +89,13 @@ sub init_schema {
     }    
     if ( !$args{no_connect} ) {
       $schema = $schema->connect($self->_database);
-      $schema->storage->on_connect_do(['PRAGMA synchronous = OFF']);
+      $schema->storage->on_connect_do(['PRAGMA synchronous = OFF'])
+       unless $self->has_custom_dsn;
     }
     if ( !$args{no_deploy} ) {
-        __PACKAGE__->deploy_schema( $schema );
-        __PACKAGE__->populate_schema( $schema ) if( !$args{no_populate} );
+        __PACKAGE__->deploy_schema( $schema, $args{deploy_args} );
+        __PACKAGE__->populate_schema( $schema )
+         if( !$args{no_populate} );
     }
     return $schema;
 }
@@ -112,10 +114,14 @@ of tables for testing.
 
 sub deploy_schema {
     my $self = shift;
-    my $schema = shift; 
+    my $schema = shift;
+    my $args = shift || {};
 
     if ($ENV{"DBICTEST_SQLT_DEPLOY"}) { 
-        return $schema->deploy();
+       
+#$schema->create_ddl_dir([qw/MySQL/], $schema->VERSION, '.', undef, $args);
+$schema->deploy($args);    
+
     } else {
         open IN, "t/lib/sqlite.sql";
         my $sql;
@@ -123,6 +129,7 @@ sub deploy_schema {
         close IN;
         ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
     }
+    return;
 }
 
 =head2 populate_schema
@@ -224,15 +231,16 @@ sub populate_schema {
         [ 1, 3 ],
     ]);
 
-    $schema->populate('TreeLike', [
-        [ qw/id parent name/ ],
-        [ 1, 0, 'foo'  ],
-        [ 2, 1, 'bar'  ],
-        [ 5, 1, 'blop' ],
-        [ 3, 2, 'baz'  ],
-        [ 4, 3, 'quux' ],
-        [ 6, 2, 'fong'  ],
-    ]);
+ #   $schema->populate('TreeLike', [
+ #       [ qw/id parent name/ ],
+ #       [ 0, 0, 'root' ],
+ #       [ 1, 0, 'foo'  ],
+ #       [ 2, 1, 'bar'  ],
+ #       [ 5, 1, 'blop' ],
+ #       [ 3, 2, 'baz'  ],
+ #       [ 4, 3, 'quux' ],
+ #       [ 6, 2, 'fong'  ],
+ #   ]);
 
     $schema->populate('Track', [
         [ qw/trackid cd  position title/ ],
@@ -273,16 +281,7 @@ sub populate_schema {
         [ 1, "Tools" ],
         [ 2, "Body Parts" ],
     ]);
-
-    $schema->populate('CollectionObject', [
-        [ qw/collection object/ ],
-        [ 1, 1 ],
-        [ 1, 2 ],
-        [ 1, 3 ],
-        [ 2, 4 ],
-        [ 2, 5 ],
-    ]);
-
+    
     $schema->populate('TypedObject', [
         [ qw/objectid type value/ ],
         [ 1, "pointy", "Awl" ],
@@ -291,6 +290,14 @@ sub populate_schema {
         [ 4, "pointy", "Tooth" ],
         [ 5, "round", "Head" ],
     ]);
+    $schema->populate('CollectionObject', [
+        [ qw/collection object/ ],
+        [ 1, 1 ],
+        [ 1, 2 ],
+        [ 1, 3 ],
+        [ 2, 4 ],
+        [ 2, 5 ],
+    ]);
 
     $schema->populate('Owners', [
         [ qw/ownerid name/ ],
index e3b2857..149f759 100644 (file)
@@ -14,7 +14,7 @@ __PACKAGE__->set_primary_key(qw/artist/);
 # since it uses the PK
 __PACKAGE__->might_have(
                        'artist_1', 'DBICTest::Schema::Artist', {
-                           'foreign.artist_id' => 'self.artist',
+                           'foreign.artistid' => 'self.artist',
                        }, {
                            is_foreign_key_constraint => 1,
                        },
index 297cfc6..b5b2763 100644 (file)
@@ -16,4 +16,12 @@ __PACKAGE__->belongs_to('parent', 'TreeLike',
                           { 'foreign.id' => 'self.parent' });
 __PACKAGE__->has_many('children', 'TreeLike', { 'foreign.parent' => 'self.id' });
 
+## since this is a self referential table we need to do a post deploy hook and get
+## some data in while constraints are off
+
+ sub sqlt_deploy_hook {
+   my ($self, $sqlt_table) = @_;
+
+   $sqlt_table->add_index(name => 'idx_name', fields => ['name']);
+ }
 1;
index b6dedf0..beced31 100755 (executable)
@@ -10,7 +10,11 @@ __PACKAGE__->add_columns(
 );
 __PACKAGE__->set_primary_key(qw/artist cd/);
 
-__PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist' );
+__PACKAGE__->belongs_to(
+    artist => 'DBICTest::Schema::Artist',
+    {'foreign.artistid'=>'self.artist'},
+);
+
 __PACKAGE__->belongs_to( cd => 'DBICTest::Schema::CD', undef, { is_deferrable => 0 } );
 
 __PACKAGE__->has_many(