minor doc tweaks
[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 Moose::Autobox;
6 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
7
8 use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadedClass/;
9 use MooseX::Types::Moose qw/Str HashRef/;
10
11 =head1 NAME
12
13 Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
14 L<Catalyst::Model::DBIC::Schema>
15
16 =head1 SYNOPSiS
17
18     __PACKAGE__->config({
19         traits => ['Replicated']
20         connect_info =>
21             ['dbi:mysql:master', 'user', 'pass'],
22         replicants => [
23             ['dbi:mysql:slave1', 'user', 'pass'],
24             ['dbi:mysql:slave2', 'user', 'pass'],
25             ['dbi:mysql:slave3', 'user', 'pass'],
26         ],
27         balancer_args => {
28           master_read_weight => 0.3
29         }
30     });
31
32 =head1 DESCRIPTION
33
34 Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
35 replicants provided in config. See that module for supported resultset
36 attributes.
37
38 The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
39 C<::Random>.
40
41 Sets the
42 L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
43 C<1> by default, meaning that you have the same chance of reading from master as
44 you do from replicants. Set to C<0> to turn off reads from master.
45
46 =head1 CONFIG PARAMETERS
47
48 =head2 replicants
49
50 Array of connect_info settings for every replicant.
51
52 The following can be set via L<Catalyst::Model::DBIC::Schema/connect_info>, or
53 as their own parameters. If set via separate parameters, they will override the
54 settings in C<connect_info>.
55
56 =head2 pool_type
57
58 See L<DBIx::Class::Storage::DBI::Replicated/pool_type>.
59
60 =head2 pool_args
61
62 See L<DBIx::Class::Storage::DBI::Replicated/pool_args>.
63
64 =head2 balancer_type
65
66 See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>.
67
68 =head2 balancer_args
69
70 See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>.
71
72 =cut
73
74 has replicants => (
75     is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
76 );
77
78 has pool_type => (is => 'ro', isa => LoadedClass);
79 has pool_args => (is => 'ro', isa => HashRef);
80 has balancer_type => (is => 'ro', isa => Str);
81 has balancer_args => (is => 'ro', isa => HashRef);
82
83 after setup => sub {
84     my $self = shift;
85
86 # check storage_type compatibility (if configured)
87     if (my $storage_type = $self->storage_type) {
88         my $class = $storage_type =~ /^::/ ?
89             "DBIx::Class::Storage$storage_type"
90             : $storage_type;
91
92         croak "This storage_type cannot be used with replication"
93             unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
94     } else {
95         $self->storage_type('::DBI::Replicated');
96     }
97
98     my $connect_info = $self->connect_info;
99
100     $connect_info->{pool_type} = $self->pool_type
101         if $self->pool_type;
102
103     $connect_info->{pool_args} = $self->pool_args
104         if $self->pool_args;
105
106     $connect_info->{balancer_type} = $self->balancer_type ||
107         $connect_info->{balancer_type} || '::Random';
108
109     $connect_info->{balancer_args} = $self->balancer_args ||
110         $connect_info->{balancer_args} || {};
111
112     $connect_info->{balancer_args}{master_read_weight} = 1
113         unless exists $connect_info->{balancer_args}{master_read_weight};
114 };
115
116 sub BUILD {}
117
118 after BUILD => sub {
119     my $self = shift;
120
121     $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
122 };
123
124 =head1 SEE ALSO
125
126 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
127 L<DBIx::Class::Storage::DBI::Replicated>,
128 L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>
129
130 =head1 AUTHOR
131
132 Rafael Kitover, C<rkitover at cpan.org>
133
134 =head1 COPYRIGHT
135
136 This program is free software, you can redistribute it and/or modify it
137 under the same terms as Perl itself.
138
139 =cut
140
141 1;