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)
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.
## ----------------------------------------------------------------------------
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;
}
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
[ 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;
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";
## 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';
}
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;
}
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;
close IN;
($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
}
+ return;
}
=head2 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/ ],
[ 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" ],
[ 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/ ],
# 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,
},
{ '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;
);
__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(