1 package Catalyst::TraitFor::Model::DBIC::Schema::Replicated;
3 use namespace::autoclean;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
7 use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadedClass/;
8 use MooseX::Types::Moose qw/Str HashRef/;
12 Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
13 L<Catalyst::Model::DBIC::Schema>
18 traits => ['Replicated']
20 ['dbi:mysql:master', 'user', 'pass'],
22 ['dbi:mysql:slave1', 'user', 'pass'],
23 ['dbi:mysql:slave2', 'user', 'pass'],
24 ['dbi:mysql:slave3', 'user', 'pass'],
27 master_read_weight => 0.3
33 Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
34 replicants provided in config. See that module for supported resultset
37 The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
41 L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
42 C<1> by default, meaning that you have the same chance of reading from master as
43 you do from replicants. Set to C<0> to turn off reads from master.
45 =head1 CONFIG PARAMETERS
49 Array of connect_info settings for every replicant.
51 The following can be set via L<Catalyst::Model::DBIC::Schema/connect_info>, or
52 as their own parameters. If set via separate parameters, they will override the
53 settings in C<connect_info>.
57 See L<DBIx::Class::Storage::DBI::Replicated/pool_type>.
61 See L<DBIx::Class::Storage::DBI::Replicated/pool_args>.
65 See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>.
69 See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>.
74 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
77 has pool_type => (is => 'ro', isa => LoadedClass);
78 has pool_args => (is => 'ro', isa => HashRef);
79 has balancer_type => (is => 'ro', isa => Str);
80 has balancer_args => (is => 'ro', isa => HashRef);
85 # check storage_type compatibility (if configured)
86 if (my $storage_type = $self->storage_type) {
87 my $class = $storage_type =~ /^::/ ?
88 "DBIx::Class::Storage$storage_type"
91 croak "This storage_type cannot be used with replication"
92 unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
94 $self->storage_type('::DBI::Replicated');
97 my $connect_info = $self->connect_info;
99 $connect_info->{pool_type} = $self->pool_type
102 $connect_info->{pool_args} = $self->pool_args
105 $connect_info->{balancer_type} = $self->balancer_type ||
106 $connect_info->{balancer_type} || '::Random';
108 $connect_info->{balancer_args} = $self->balancer_args ||
109 $connect_info->{balancer_args} || {};
111 $connect_info->{balancer_args}{master_read_weight} = 1
112 unless exists $connect_info->{balancer_args}{master_read_weight};
120 $self->storage->connect_replicants(map [ $_ ], @{ $self->replicants });
125 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
126 L<DBIx::Class::Storage::DBI::Replicated>,
127 L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>
131 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
132 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
136 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
140 This program is free software, you can redistribute it and/or modify it
141 under the same terms as Perl itself.