1 package Catalyst::Model::DBIC::Schema::Trait::Replicated;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
7 use Catalyst::Model::DBIC::Schema::Types 'ConnectInfos';
9 use namespace::clean -except => 'meta';
13 Catalyst::Model::DBIC::Schema::Trait::Replicated - Replicated storage support for
14 L<Catalyst::Model::DBIC::Schema>
19 traits => ['Replicated']
21 ['dbi:mysql:master', 'user', 'pass'],
23 ['dbi:mysql:slave1', 'user', 'pass'],
24 ['dbi:mysql:slave2', 'user', 'pass'],
25 ['dbi:mysql:slave3', 'user', 'pass'],
28 master_read_weight => 0.3
34 Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
35 replicants provided in config. See that module for supported resultset
38 The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
42 L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
43 C<1> by default, meaning that you have the same chance of reading from master as
44 you do from replicants. Set to C<0> to turn off reads from master.
46 =head1 NOTE ON L<DBIx::Class> VERSIONS PRIOR TO 0.08103
48 This trait will work, however, any C<::Storage::Replicated> options in
49 L<Catalyst::Model::DBIC::Schema/connect_info> will be ignored, master
50 connect_info will not be merged to replicants, and
51 L<DBIx::Class::Storage::DBI::Replicated::Balancer::First> will be used instead,
52 with all your reads going only to one of your replicants. You'll also get some
53 warnings. The C<Caching> trait will also not work.
57 =head1 CONFIG PARAMETERS
61 Array of connect_info settings for every replicant.
66 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
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"
78 croak "This storage_type cannot be used with replication"
79 unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
81 $self->storage_type('::DBI::Replicated');
84 $self->connect_info->{balancer_type} ||= '::Random'
85 unless $self->connect_info->{balancer_type};
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;
93 after finalize => sub {
96 $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
101 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
102 L<DBIx::Class::Storage::DBI::Replicated>,
103 L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
107 Rafael Kitover, C<rkitover@cpan.org>
111 This program is free software, you can redistribute it and/or modify it
112 under the same terms as Perl itself.