C::M::DBIC::Schema - make replicated role work with older DBIC, sort of
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema / Role / Replicated.pm
index 2ef3c51..ed727e3 100644 (file)
@@ -23,17 +23,37 @@ L<Catalyst::Model::DBIC::Schema>
             ['dbi:mysql:slave1', 'user', 'pass'],
             ['dbi:mysql:slave2', 'user', 'pass'],
             ['dbi:mysql:slave3', 'user', 'pass'],
-        ]
+        ],
+        balancer_args => {
+          master_read_weight => 0.3
+        }
     });
 
 =head1 DESCRIPTION
 
-B<DOES NOT WORK YET> -- requires some DBIC changes
-
 Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
 replicants provided in config. See that module for supported resultset
 attributes.
 
+The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
+C<::Random>.
+
+Sets the
+L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
+C<1> by default, meaning that you have the same chance of reading from master as
+you do from replicants. Set to C<0> to turn off reads from master.
+
+=head1 NOTE ON L<DBIx::Class> VERSIONS PRIOR TO 0.08103
+
+This role will work, however, any C<::Storage::Replicated> options in
+L<Catalyst::Model::DBIC::Schema/connect_info> will be ignored, master
+connect_info will not be merged to replicants, and
+L<DBIx::Class::Storage::DBI::Replicated::Balancer::First> will be used instead,
+with all your reads going only to one of your replicants. You'll also get some
+warnings. The C<Caching> role will also not work.
+
+Please upgrade.
+
 =head1 CONFIG PARAMETERS
 
 =head2 replicants
@@ -60,12 +80,20 @@ after setup => sub {
     } else {
         $self->storage_type('::DBI::Replicated');
     }
+
+    $self->connect_info->{balancer_type} ||= '::Random'
+        unless $self->connect_info->{balancer_type};
+
+    unless ($self->connect_info->{balancer_args} &&
+            exists $self->connect_info->{balancer_args}{master_read_weight}) {
+        $self->connect_info->{balancer_args}{master_read_weight} = 1;
+    }
 };
 
 after finalize => sub {
     my $self = shift;
 
-    $self->storage->connect_replicants($self->replicants->flatten);
+    $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
 };
 
 =head1 SEE ALSO