Stop using deprecated Class::MOP::load_module
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / TraitFor / Model / DBIC / Schema / Replicated.pm
1 package Catalyst::TraitFor::Model::DBIC::Schema::Replicated;
2
3 use namespace::autoclean;
4 use Moose::Role;
5 use Carp::Clan '^Catalyst::Model::DBIC::Schema';
6
7 use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadableClass/;
8 use MooseX::Types::Moose qw/Str HashRef/;
9
10 use Module::Runtime qw/use_module/;
11
12 =head1 NAME
13
14 Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
15 L<Catalyst::Model::DBIC::Schema>
16
17 =head1 SYNOPSiS
18
19     __PACKAGE__->config({
20         traits => ['Replicated']
21         connect_info =>
22             ['dbi:mysql:master', 'user', 'pass'],
23         replicants => [
24             ['dbi:mysql:slave1', 'user', 'pass'],
25             ['dbi:mysql:slave2', 'user', 'pass'],
26             ['dbi:mysql:slave3', 'user', 'pass'],
27         ],
28         balancer_args => {
29           master_read_weight => 0.3
30         }
31     });
32
33 =head1 DESCRIPTION
34
35 Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
36 replicants provided in config. See that module for supported resultset
37 attributes.
38
39 The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
40 C<::Random>.
41
42 Sets the
43 L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
44 C<1> by default, meaning that you have the same chance of reading from master as
45 you do from replicants. Set to C<0> to turn off reads from master.
46
47 =head1 CONFIG PARAMETERS
48
49 =head2 replicants
50
51 Array of connect_info settings for every replicant.
52
53 The following can be set via L<Catalyst::Model::DBIC::Schema/connect_info>, or
54 as their own parameters. If set via separate parameters, they will override the
55 settings in C<connect_info>.
56
57 =head2 pool_type
58
59 See L<DBIx::Class::Storage::DBI::Replicated/pool_type>.
60
61 =head2 pool_args
62
63 See L<DBIx::Class::Storage::DBI::Replicated/pool_args>.
64
65 =head2 balancer_type
66
67 See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>.
68
69 =head2 balancer_args
70
71 See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>.
72
73 =cut
74
75 has replicants => (
76     is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
77 );
78
79 has pool_type => (is => 'ro', isa => LoadableClass);
80 has pool_args => (is => 'ro', isa => HashRef);
81 has balancer_type => (is => 'ro', isa => Str);
82 has balancer_args => (is => 'ro', isa => HashRef);
83
84 after setup => sub {
85     my $self = shift;
86
87 # check storage_type compatibility (if configured)
88     if (my $storage_type = $self->storage_type) {
89         my $class = $storage_type =~ /^::/ ?
90             "DBIx::Class::Storage$storage_type"
91             : $storage_type;
92
93         use_module($class);
94
95         croak "This storage_type cannot be used with replication"
96             unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
97     } else {
98         $self->storage_type('::DBI::Replicated');
99     }
100
101     my $connect_info = $self->connect_info;
102
103     $connect_info->{pool_type} = $self->pool_type
104         if $self->pool_type;
105
106     $connect_info->{pool_args} = $self->pool_args
107         if $self->pool_args;
108
109     $connect_info->{balancer_type} = $self->balancer_type ||
110         $connect_info->{balancer_type} || '::Random';
111
112     $connect_info->{balancer_args} = $self->balancer_args ||
113         $connect_info->{balancer_args} || {};
114
115     $connect_info->{balancer_args}{master_read_weight} = 1
116         unless exists $connect_info->{balancer_args}{master_read_weight};
117 };
118
119 sub BUILD {}
120
121 after BUILD => sub {
122     my $self = shift;
123
124     $self->storage->connect_replicants(map [ $_ ], @{ $self->replicants });
125 };
126
127 =head1 SEE ALSO
128
129 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
130 L<DBIx::Class::Storage::DBI::Replicated>,
131 L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>
132
133 =head1 AUTHOR
134
135 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
136 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
137
138 =head1 COPYRIGHT
139
140 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
141
142 =head1 LICENSE
143
144 This program is free software, you can redistribute it and/or modify it
145 under the same terms as Perl itself.
146
147 =cut
148
149 1;