cleanups, deps on mx::traits which is not released yet
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema / Trait / Replicated.pm
1 package Catalyst::Model::DBIC::Schema::Trait::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 'ConnectInfos';
9
10 =head1 NAME
11
12 Catalyst::Model::DBIC::Schema::Trait::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 =cut
52
53 has replicants => (
54     is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
55 );
56
57 after setup => sub {
58     my $self = shift;
59
60 # check storage_type compatibility (if configured)
61     if (my $storage_type = $self->storage_type) {
62         my $class = $storage_type =~ /^::/ ?
63             "DBIx::Class::Storage$storage_type"
64             : $storage_type;
65
66         croak "This storage_type cannot be used with replication"
67             unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
68     } else {
69         $self->storage_type('::DBI::Replicated');
70     }
71
72     $self->connect_info->{balancer_type} ||= '::Random'
73         unless $self->connect_info->{balancer_type};
74
75     unless ($self->connect_info->{balancer_args} &&
76             exists $self->connect_info->{balancer_args}{master_read_weight}) {
77         $self->connect_info->{balancer_args}{master_read_weight} = 1;
78     }
79 };
80
81 sub BUILD {}
82 after BUILD => sub {
83     my $self = shift;
84
85     $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
86 };
87
88 =head1 SEE ALSO
89
90 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
91 L<DBIx::Class::Storage::DBI::Replicated>,
92 L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
93
94 =head1 AUTHOR
95
96 Rafael Kitover, C<rkitover at cpan.org>
97
98 =head1 COPYRIGHT
99
100 This program is free software, you can redistribute it and/or modify it
101 under the same terms as Perl itself.
102
103 =cut
104
105 1;