From: John Napiorkowski Date: Thu, 8 May 2008 23:34:55 +0000 (+0000) Subject: converted replicant to a role so that we can apply it after ensure_connected properly... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=de5dc9efd2c3575f78a7dc71c50d571c9a17c38d;p=dbsrgits%2FDBIx-Class-Historic.git converted replicant to a role so that we can apply it after ensure_connected properly reblesses the storage into the correct driver specific storage --- diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm index bba95d2..5bf61e6 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm @@ -36,7 +36,7 @@ has 'replicant_type' => ( is=>'ro', isa=>'ClassName', required=>1, - default=>'DBIx::Class::Storage::DBI::Replicated::Replicant', + default=>'DBIx::Class::Storage::DBI', handles=>{ 'create_replicant' => 'new', }, @@ -84,7 +84,7 @@ removes the replicant under $key from the pool has 'replicants' => ( is=>'rw', metaclass => 'Collection::Hash', - isa=>'HashRef[DBIx::Class::Storage::DBI::Replicated::Replicant]', + isa=>'HashRef[DBIx::Class::Storage::DBI]', default=>sub {{}}, provides => { 'set' => 'set_replicant', @@ -107,15 +107,21 @@ L attribute. =cut +use Data::Dump qw/dump/; + sub connect_replicants { my $self = shift @_; my $schema = shift @_; my @newly_created = (); foreach my $connect_info (@_) { + my $replicant = $self->create_replicant($schema); - $replicant->connect_info($connect_info); + $replicant->connect_info($connect_info); $replicant->ensure_connected; + + DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant); + my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/); $self->set_replicant( $key => $replicant); push @newly_created, $replicant; diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm index 59ac66e..e1e7f8d 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm @@ -1,16 +1,14 @@ package DBIx::Class::Storage::DBI::Replicated::Replicant; -use Moose; -extends 'DBIx::Class::Storage::DBI', 'Moose::Object'; +use Moose::Role; =head1 NAME -DBIx::Class::Storage::DBI::Replicated::Replicant; A replicated DBI Storage +DBIx::Class::Storage::DBI::Replicated::Replicant; A replicated DBI Storage Role =head1 SYNOPSIS -This class is used internally by L. You -shouldn't need to create instances of this class. +This class is used internally by L. =head1 DESCRIPTION @@ -65,29 +63,6 @@ around '_query_start' => sub { $self->$method("DSN: $dsn SQL: $sql", @bind); }; -=head2 is_replicating - -A boolean that reports if a particular L is set to -replicate from a master database. Default is false, which is the result -returned by databases that don't support replication. - -=cut - -sub is_replicating { - my $self = shift @_; -} - -=head2 lag_behind_master - -Returns a number that represents a certain amount of lag behind a master db -when a given storage is replicating. The number is database dependent, but -starts at zero, which is the default, and increases with the amount of lag. - -=cut - -sub lag_behind_master { - my $self = shift @_; -} =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/mysql.pm b/lib/DBIx/Class/Storage/DBI/mysql.pm index 002f50b..5c59d18 100644 --- a/lib/DBIx/Class/Storage/DBI/mysql.pm +++ b/lib/DBIx/Class/Storage/DBI/mysql.pm @@ -40,6 +40,7 @@ sub is_replicating { sub lag_behind_master { my $self = shift @_; + return $self->dbh->selectrow_hashref('show slave status'); } 1; diff --git a/t/93storage_replication.t b/t/93storage_replication.t index 26f6039..eb31c81 100644 --- a/t/93storage_replication.t +++ b/t/93storage_replication.t @@ -5,10 +5,10 @@ use Test::More; use DBICTest; BEGIN { - eval "use Moose"; + eval "use Moose; use Test::Moose"; plan $@ ? ( skip_all => 'needs Moose for testing' ) - : ( tests => 40 ); + : ( tests => 41 ); } use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool'; @@ -213,18 +213,18 @@ ok $replicated->schema->storage->pool->has_replicants is $replicated->schema->storage->num_replicants => 2 => 'has two replicants'; -isa_ok $replicated_storages[0] +does_ok $replicated_storages[0] => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; -isa_ok $replicated_storages[1] +does_ok $replicated_storages[1] => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; my @replicant_names = keys %{$replicated->schema->storage->replicants}; - -isa_ok $replicated->schema->storage->replicants->{$replicant_names[0]} + +does_ok $replicated->schema->storage->replicants->{$replicant_names[0]} => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; -isa_ok $replicated->schema->storage->replicants->{$replicant_names[1]} +does_ok $replicated->schema->storage->replicants->{$replicant_names[1]} => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; ## Add some info to the database @@ -355,7 +355,19 @@ $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'; + => 'Fallback to master'; + +$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1); +$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1); + +ok $replicated->schema->resultset('Artist')->find(2) + => 'Returned to replicates'; + +## Getting slave status tests + +use Data::Dump qw/dump/; +my $lag1 = $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master; +warn dump $lag1; ## Delete the old database files $replicated->cleanup;