switch to CX::Component::Traits
[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
8use Catalyst::Model::DBIC::Schema::Types 'ConnectInfos';
9
c4fee9b8 10=head1 NAME
11
fb691af9 12Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
c4fee9b8 13L<Catalyst::Model::DBIC::Schema>
14
15=head1 SYNOPSiS
16
17 __PACKAGE__->config({
c34bcab6 18 traits => ['Replicated']
c4fee9b8 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'],
bd309c0c 25 ],
26 balancer_args => {
27 master_read_weight => 0.3
28 }
c4fee9b8 29 });
30
31=head1 DESCRIPTION
32
c4fee9b8 33Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
34replicants provided in config. See that module for supported resultset
35attributes.
36
bd309c0c 37The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
38C<::Random>.
39
40Sets the
41L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
42C<1> by default, meaning that you have the same chance of reading from master as
43you do from replicants. Set to C<0> to turn off reads from master.
44
c4fee9b8 45=head1 CONFIG PARAMETERS
46
47=head2 replicants
48
49Array of connect_info settings for every replicant.
50
51=cut
52
53has replicants => (
54 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
55);
56
57after 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 }
39f5f008 71
bd309c0c 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 }
c4fee9b8 79};
80
73f72d28 81sub BUILD {}
82after BUILD => sub {
c4fee9b8 83 my $self = shift;
84
ac63f7fa 85 $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
c4fee9b8 86};
87
88=head1 SEE ALSO
89
90L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
91L<DBIx::Class::Storage::DBI::Replicated>,
92L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
93
94=head1 AUTHOR
95
c7d7b849 96Rafael Kitover, C<rkitover at cpan.org>
c4fee9b8 97
98=head1 COPYRIGHT
99
100This program is free software, you can redistribute it and/or modify it
101under the same terms as Perl itself.
102
103=cut
104
1051;