C::M::DBIC::Schema - make replicated role work with older DBIC, sort of
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Model / DBIC / Schema / Role / Replicated.pm
CommitLineData
c4fee9b8 1package Catalyst::Model::DBIC::Schema::Role::Replicated;
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
13Catalyst::Model::DBIC::Schema::Role::Replicated - Replicated storage support for
14L<Catalyst::Model::DBIC::Schema>
15
16=head1 SYNOPSiS
17
18 __PACKAGE__->config({
19 roles => ['Replicated']
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
ac63f7fa 46=head1 NOTE ON L<DBIx::Class> VERSIONS PRIOR TO 0.08103
47
48This role will work, however, any C<::Storage::Replicated> options in
49L<Catalyst::Model::DBIC::Schema/connect_info> will be ignored, master
50connect_info will not be merged to replicants, and
51L<DBIx::Class::Storage::DBI::Replicated::Balancer::First> will be used instead,
52with all your reads going only to one of your replicants. You'll also get some
53warnings. The C<Caching> role will also not work.
54
55Please upgrade.
56
c4fee9b8 57=head1 CONFIG PARAMETERS
58
59=head2 replicants
60
61Array of connect_info settings for every replicant.
62
63=cut
64
65has replicants => (
66 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
67);
68
69after setup => sub {
70 my $self = shift;
71
72# check storage_type compatibility (if configured)
73 if (my $storage_type = $self->storage_type) {
74 my $class = $storage_type =~ /^::/ ?
75 "DBIx::Class::Storage$storage_type"
76 : $storage_type;
77
78 croak "This storage_type cannot be used with replication"
79 unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
80 } else {
81 $self->storage_type('::DBI::Replicated');
82 }
39f5f008 83
bd309c0c 84 $self->connect_info->{balancer_type} ||= '::Random'
85 unless $self->connect_info->{balancer_type};
86
87 unless ($self->connect_info->{balancer_args} &&
88 exists $self->connect_info->{balancer_args}{master_read_weight}) {
89 $self->connect_info->{balancer_args}{master_read_weight} = 1;
90 }
c4fee9b8 91};
92
93after finalize => sub {
94 my $self = shift;
95
ac63f7fa 96 $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
c4fee9b8 97};
98
99=head1 SEE ALSO
100
101L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
102L<DBIx::Class::Storage::DBI::Replicated>,
103L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
104
105=head1 AUTHOR
106
107Rafael Kitover, C<rkitover@cpan.org>
108
109=head1 COPYRIGHT
110
111This program is free software, you can redistribute it and/or modify it
112under the same terms as Perl itself.
113
114=cut
115
1161;