remove MX::Object::Pluggable
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema / Trait / Replicated.pm
CommitLineData
c34bcab6 1package Catalyst::Model::DBIC::Schema::Trait::Replicated;
c4fee9b8 2
3use Moose::Role;
4use Moose::Autobox;
bd309c0c 5use Carp::Clan '^Catalyst::Model::DBIC::Schema';
c4fee9b8 6
7use Catalyst::Model::DBIC::Schema::Types 'ConnectInfos';
8
9use namespace::clean -except => 'meta';
10
11=head1 NAME
12
c34bcab6 13Catalyst::Model::DBIC::Schema::Trait::Replicated - Replicated storage support for
c4fee9b8 14L<Catalyst::Model::DBIC::Schema>
15
16=head1 SYNOPSiS
17
18 __PACKAGE__->config({
c34bcab6 19 traits => ['Replicated']
c4fee9b8 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'],
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
52=cut
53
54has replicants => (
55 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
56);
57
58after setup => sub {
59 my $self = shift;
60
61# check storage_type compatibility (if configured)
62 if (my $storage_type = $self->storage_type) {
63 my $class = $storage_type =~ /^::/ ?
64 "DBIx::Class::Storage$storage_type"
65 : $storage_type;
66
67 croak "This storage_type cannot be used with replication"
68 unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
69 } else {
70 $self->storage_type('::DBI::Replicated');
71 }
39f5f008 72
bd309c0c 73 $self->connect_info->{balancer_type} ||= '::Random'
74 unless $self->connect_info->{balancer_type};
75
76 unless ($self->connect_info->{balancer_args} &&
77 exists $self->connect_info->{balancer_args}{master_read_weight}) {
78 $self->connect_info->{balancer_args}{master_read_weight} = 1;
79 }
c4fee9b8 80};
81
82after finalize => sub {
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
96Rafael Kitover, C<rkitover@cpan.org>
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;