use BUILD's $args instead of %$self
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / TraitFor / Model / DBIC / Schema / Replicated.pm
CommitLineData
fb691af9 1package Catalyst::TraitFor::Model::DBIC::Schema::Replicated;
c4fee9b8 2
73f72d28 3use namespace::autoclean;
c4fee9b8 4use Moose::Role;
5use Moose::Autobox;
bd309c0c 6use Carp::Clan '^Catalyst::Model::DBIC::Schema';
c4fee9b8 7
7314403a 8use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadedClass/;
9use MooseX::Types::Moose qw/Str HashRef/;
c4fee9b8 10
c4fee9b8 11=head1 NAME
12
fb691af9 13Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
c4fee9b8 14L<Catalyst::Model::DBIC::Schema>
15
16=head1 SYNOPSiS
17
18 __PACKAGE__->config({
c34bcab6 19 traits => ['Replicated']
7314403a 20 connect_info =>
c4fee9b8 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'],
bd309c0c 26 ],
27 balancer_args => {
28 master_read_weight => 0.3
29 }
c4fee9b8 30 });
31
32=head1 DESCRIPTION
33
c4fee9b8 34Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
35replicants provided in config. See that module for supported resultset
36attributes.
37
bd309c0c 38The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
39C<::Random>.
40
41Sets the
42L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
43C<1> by default, meaning that you have the same chance of reading from master as
44you do from replicants. Set to C<0> to turn off reads from master.
45
c4fee9b8 46=head1 CONFIG PARAMETERS
47
48=head2 replicants
49
50Array of connect_info settings for every replicant.
51
7314403a 52The following can be set via L<Catalyst::Model::DBIC::Schema/connect_info>, or
53as their own parameters. If set via separate parameters, they will override the
54settings in C<connect_info>.
55
56=head2 pool_type
57
58See L<DBIx::Class::Storage::DBI::Replicated/pool_type>.
59
60=head2 pool_args
61
62See L<DBIx::Class::Storage::DBI::Replicated/pool_args>.
63
64=head2 balancer_type
65
66See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>.
67
68=head2 balancer_args
69
70See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>.
71
c4fee9b8 72=cut
73
74has replicants => (
75 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
76);
77
7314403a 78has pool_type => (is => 'ro', isa => LoadedClass);
79has pool_args => (is => 'ro', isa => HashRef);
80has balancer_type => (is => 'ro', isa => Str);
81has balancer_args => (is => 'ro', isa => HashRef);
82
c4fee9b8 83after 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 }
39f5f008 97
7314403a 98 my $connect_info = $self->connect_info;
bd309c0c 99
7314403a 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};
c4fee9b8 114};
115
73f72d28 116sub BUILD {}
7314403a 117
73f72d28 118after BUILD => sub {
c4fee9b8 119 my $self = shift;
120
ac63f7fa 121 $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
c4fee9b8 122};
123
124=head1 SEE ALSO
125
126L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
127L<DBIx::Class::Storage::DBI::Replicated>,
e203cd42 128L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>
c4fee9b8 129
130=head1 AUTHOR
131
c7d7b849 132Rafael Kitover, C<rkitover at cpan.org>
c4fee9b8 133
134=head1 COPYRIGHT
135
136This program is free software, you can redistribute it and/or modify it
137under the same terms as Perl itself.
138
139=cut
140
1411;