Fix DBIC::Schema->compose_namespace POD link
[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
d9bff0e4 3## WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
4## If you make changes to this code and don't actually go and test it
5## on a real replicated environment I will rip you an new hole. The
6## test suite DOES NOT properly test this. --JNAP
7
73f72d28 8use namespace::autoclean;
c4fee9b8 9use Moose::Role;
bd309c0c 10use Carp::Clan '^Catalyst::Model::DBIC::Schema';
c4fee9b8 11
d9bff0e4 12use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadedClass/;
7314403a 13use MooseX::Types::Moose qw/Str HashRef/;
c4fee9b8 14
d9bff0e4 15use Module::Runtime;
45b10191 16
c4fee9b8 17=head1 NAME
18
fb691af9 19Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
c4fee9b8 20L<Catalyst::Model::DBIC::Schema>
21
22=head1 SYNOPSiS
23
24 __PACKAGE__->config({
c34bcab6 25 traits => ['Replicated']
7314403a 26 connect_info =>
c4fee9b8 27 ['dbi:mysql:master', 'user', 'pass'],
28 replicants => [
29 ['dbi:mysql:slave1', 'user', 'pass'],
30 ['dbi:mysql:slave2', 'user', 'pass'],
31 ['dbi:mysql:slave3', 'user', 'pass'],
bd309c0c 32 ],
33 balancer_args => {
34 master_read_weight => 0.3
35 }
c4fee9b8 36 });
37
38=head1 DESCRIPTION
39
c4fee9b8 40Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
41replicants provided in config. See that module for supported resultset
42attributes.
43
bd309c0c 44The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
45C<::Random>.
46
47Sets the
48L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
49C<1> by default, meaning that you have the same chance of reading from master as
50you do from replicants. Set to C<0> to turn off reads from master.
51
c4fee9b8 52=head1 CONFIG PARAMETERS
53
54=head2 replicants
55
56Array of connect_info settings for every replicant.
57
7314403a 58The following can be set via L<Catalyst::Model::DBIC::Schema/connect_info>, or
59as their own parameters. If set via separate parameters, they will override the
60settings in C<connect_info>.
61
62=head2 pool_type
63
64See L<DBIx::Class::Storage::DBI::Replicated/pool_type>.
65
66=head2 pool_args
67
68See L<DBIx::Class::Storage::DBI::Replicated/pool_args>.
69
70=head2 balancer_type
71
72See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>.
73
74=head2 balancer_args
75
76See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>.
77
c4fee9b8 78=cut
79
80has replicants => (
81 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
82);
83
d9bff0e4 84# If you change LoadedClass with LoadableClass I will rip you a new hole,
85# it doesn't work exactly the same - JNAP
86
87has pool_type => (is => 'ro', isa => LoadedClass);
7314403a 88has pool_args => (is => 'ro', isa => HashRef);
89has balancer_type => (is => 'ro', isa => Str);
90has balancer_args => (is => 'ro', isa => HashRef);
91
c4fee9b8 92after setup => sub {
93 my $self = shift;
94
95# check storage_type compatibility (if configured)
96 if (my $storage_type = $self->storage_type) {
97 my $class = $storage_type =~ /^::/ ?
98 "DBIx::Class::Storage$storage_type"
99 : $storage_type;
100
d9bff0e4 101 # For some odd reason if you try to use 'use_module' as an export
102 # the code breaks. I guess something odd about MR and all these
103 # runtime loaded crazy trait code. Please don't "tidy the code up" -JNAP
104 Module::Runtime::use_module($class);
f24a5fbb 105
c4fee9b8 106 croak "This storage_type cannot be used with replication"
107 unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
108 } else {
109 $self->storage_type('::DBI::Replicated');
110 }
39f5f008 111
7314403a 112 my $connect_info = $self->connect_info;
bd309c0c 113
7314403a 114 $connect_info->{pool_type} = $self->pool_type
115 if $self->pool_type;
116
117 $connect_info->{pool_args} = $self->pool_args
118 if $self->pool_args;
119
120 $connect_info->{balancer_type} = $self->balancer_type ||
121 $connect_info->{balancer_type} || '::Random';
122
123 $connect_info->{balancer_args} = $self->balancer_args ||
124 $connect_info->{balancer_args} || {};
125
126 $connect_info->{balancer_args}{master_read_weight} = 1
127 unless exists $connect_info->{balancer_args}{master_read_weight};
c4fee9b8 128};
129
73f72d28 130sub BUILD {}
7314403a 131
73f72d28 132after BUILD => sub {
c4fee9b8 133 my $self = shift;
134
7dfd616a 135 $self->storage->connect_replicants(map [ $_ ], @{ $self->replicants });
c4fee9b8 136};
137
138=head1 SEE ALSO
139
140L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
141L<DBIx::Class::Storage::DBI::Replicated>,
e203cd42 142L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>
c4fee9b8 143
144=head1 AUTHOR
145
4e251d1a 146See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
147L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
c4fee9b8 148
149=head1 COPYRIGHT
150
4e251d1a 151See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
152
153=head1 LICENSE
154
c4fee9b8 155This program is free software, you can redistribute it and/or modify it
156under the same terms as Perl itself.
157
158=cut
159
1601;