X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FReplicated.pm;h=c583c6e58edcf3655cee7d1e2a97683791a0677a;hb=6a151f5877412d2824295071428c52eb4bf646ec;hp=8cb0fcd82e3ff8f804c338ed7b58c259c58a88b1;hpb=bd5da3696c505d7f014320ef53e477985b5f7b3c;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Replicated.pm b/lib/DBIx/Class/Storage/DBI/Replicated.pm index 8cb0fcd..c583c6e 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated.pm @@ -30,7 +30,7 @@ use Moose; use DBIx::Class::Storage::DBI; use DBIx::Class::Storage::DBI::Replicated::Pool; use DBIx::Class::Storage::DBI::Replicated::Balancer; -use DBIx::Class::Storage::DBI::Replicated::Types 'BalancerClassNamePart'; +use DBIx::Class::Storage::DBI::Replicated::Types qw/BalancerClassNamePart DBICSchema DBICStorageDBI/; use MooseX::Types::Moose qw/ClassName HashRef Object/; use Scalar::Util 'reftype'; use Carp::Clan qw/^DBIx::Class/; @@ -48,11 +48,15 @@ The Following example shows how to change an existing $schema to a replicated storage type, add some replicated (readonly) databases, and perform reporting tasks. - ## Change storage_type in your schema class +You should set the 'storage_type attribute to a replicated type. You should +also defined you arguments, such as which balancer you want and any arguments +that the Pool object should get. + $schema->storage_type( ['::DBI::Replicated', {balancer=>'::Random'}] ); - ## Add some slaves. Basically this is an array of arrayrefs, where each - ## arrayref is database connect information +Next, you need to add in the Replicants. Basically this is an array of +arrayrefs, where each arrayref is database connect information. Think of these +arguments as what you'd pass to the 'normal' $schema->connect method. $schema->storage->connect_replicants( [$dsn1, $user, $pass, \%opts], @@ -60,20 +64,28 @@ tasks. [$dsn3, $user, $pass, \%opts], ); - ## Now, just use the $schema as normal +Now, just use the $schema as you normally would. Automatically all reads will +be delegated to the replicants, while writes to the master. + $schema->resultset('Source')->search({name=>'etc'}); - ## You can force a given query to use a particular storage using the search - ### attribute 'force_pool'. For example: +You can force a given query to use a particular storage using the search +attribute 'force_pool'. For example: my $RS = $schema->resultset('Source')->search(undef, {force_pool=>'master'}); - - ## Now $RS will force everything (both reads and writes) to use whatever was - ## setup as the master storage. 'master' is hardcoded to always point to the - ## Master, but you can also use any Replicant name. Please see: - ## L and the replicants attribute for - ## More. Also see transactions and L for alternative ways - ## to force read traffic to the master. + +Now $RS will force everything (both reads and writes) to use whatever was setup +as the master storage. 'master' is hardcoded to always point to the Master, +but you can also use any Replicant name. Please see: +L and the replicants attribute for more. + +Also see transactions and L for alternative ways to +force read traffic to the master. In general, you should wrap your statements +in a transaction when you are reading and writing to the same tables at the +same time, since your replicants will often lag a bit behind the master. + +See L for more help and +walkthroughs. =head1 DESCRIPTION @@ -129,7 +141,7 @@ The underlying L object this storage is attaching has 'schema' => ( is=>'rw', - isa=>'DBIx::Class::Schema', + isa=>DBICSchema, weak_ref=>1, required=>1, ); @@ -153,7 +165,7 @@ has 'pool_type' => ( =head2 pool_args Contains a hashref of initialized information to pass to the Balancer object. -See L for available arguments. +See L for available arguments. =cut @@ -186,7 +198,7 @@ has 'balancer_type' => ( =head2 balancer_args Contains a hashref of initialized information to pass to the Balancer object. -See L for available arguments. +See L for available arguments. =cut @@ -242,7 +254,7 @@ pool of databases that is allowed to handle write traffic. has 'master' => ( is=> 'ro', - isa=>'DBIx::Class::Storage::DBI', + isa=>DBICStorageDBI, lazy_build=>1, ); @@ -288,7 +300,8 @@ has 'write_handler' => ( create_ddl_dir deployment_statements datetime_parser - datetime_parser_type + datetime_parser_type + build_datetime_parser last_insert_id insert insert_bulk @@ -303,10 +316,19 @@ has 'write_handler' => ( sth deploy with_deferred_fk_checks - + dbh_do reload_row + with_deferred_fk_checks _prep_for_execute - + + backup + is_datatype_numeric + _count_select + _subq_count_select + _subq_update_delete + svp_rollback + svp_begin + svp_release /], );