73317d816fbd6e2be30b125691cf7f91160a187e
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / TraitFor / Model / DBIC / Schema / Replicated.pm
1 package Catalyst::TraitFor::Model::DBIC::Schema::Replicated;
2
3 use namespace::autoclean;
4 use Moose::Role;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
6
7 use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadedClass/;
8 use MooseX::Types::Moose qw/Str HashRef/;
9
10 =head1 NAME
11
12 Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
13 L<Catalyst::Model::DBIC::Schema>
14
15 =head1 SYNOPSiS
16
17     __PACKAGE__->config({
18         traits => ['Replicated']
19         connect_info =>
20             ['dbi:mysql:master', 'user', 'pass'],
21         replicants => [
22             ['dbi:mysql:slave1', 'user', 'pass'],
23             ['dbi:mysql:slave2', 'user', 'pass'],
24             ['dbi:mysql:slave3', 'user', 'pass'],
25         ],
26         balancer_args => {
27           master_read_weight => 0.3
28         }
29     });
30
31 =head1 DESCRIPTION
32
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
35 attributes.
36
37 The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
38 C<::Random>.
39
40 Sets the
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.
44
45 =head1 CONFIG PARAMETERS
46
47 =head2 replicants
48
49 Array of connect_info settings for every replicant.
50
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>.
54
55 =head2 pool_type
56
57 See L<DBIx::Class::Storage::DBI::Replicated/pool_type>.
58
59 =head2 pool_args
60
61 See L<DBIx::Class::Storage::DBI::Replicated/pool_args>.
62
63 =head2 balancer_type
64
65 See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>.
66
67 =head2 balancer_args
68
69 See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>.
70
71 =cut
72
73 has replicants => (
74     is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
75 );
76
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);
81
82 after setup => sub {
83     my $self = shift;
84
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"
89             : $storage_type;
90
91         Class::MOP::load_class($class);
92
93         croak "This storage_type cannot be used with replication"
94             unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
95     } else {
96         $self->storage_type('::DBI::Replicated');
97     }
98
99     my $connect_info = $self->connect_info;
100
101     $connect_info->{pool_type} = $self->pool_type
102         if $self->pool_type;
103
104     $connect_info->{pool_args} = $self->pool_args
105         if $self->pool_args;
106
107     $connect_info->{balancer_type} = $self->balancer_type ||
108         $connect_info->{balancer_type} || '::Random';
109
110     $connect_info->{balancer_args} = $self->balancer_args ||
111         $connect_info->{balancer_args} || {};
112
113     $connect_info->{balancer_args}{master_read_weight} = 1
114         unless exists $connect_info->{balancer_args}{master_read_weight};
115 };
116
117 sub BUILD {}
118
119 after BUILD => sub {
120     my $self = shift;
121
122     $self->storage->connect_replicants(map [ $_ ], @{ $self->replicants });
123 };
124
125 =head1 SEE ALSO
126
127 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
128 L<DBIx::Class::Storage::DBI::Replicated>,
129 L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>
130
131 =head1 AUTHOR
132
133 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
134 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
135
136 =head1 COPYRIGHT
137
138 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
139
140 =head1 LICENSE
141
142 This program is free software, you can redistribute it and/or modify it
143 under the same terms as Perl itself.
144
145 =cut
146
147 1;