Stop using deprecated Class::MOP::load_module
[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;
bd309c0c 5use Carp::Clan '^Catalyst::Model::DBIC::Schema';
c4fee9b8 6
45b10191 7use Catalyst::Model::DBIC::Schema::Types qw/ConnectInfos LoadableClass/;
7314403a 8use MooseX::Types::Moose qw/Str HashRef/;
c4fee9b8 9
45b10191 10use Module::Runtime qw/use_module/;
11
c4fee9b8 12=head1 NAME
13
fb691af9 14Catalyst::TraitFor::Model::DBIC::Schema::Replicated - Replicated storage support for
c4fee9b8 15L<Catalyst::Model::DBIC::Schema>
16
17=head1 SYNOPSiS
18
19 __PACKAGE__->config({
c34bcab6 20 traits => ['Replicated']
7314403a 21 connect_info =>
c4fee9b8 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'],
bd309c0c 27 ],
28 balancer_args => {
29 master_read_weight => 0.3
30 }
c4fee9b8 31 });
32
33=head1 DESCRIPTION
34
c4fee9b8 35Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects
36replicants provided in config. See that module for supported resultset
37attributes.
38
bd309c0c 39The default L<DBIx::Class::Storage::DBI::Replicated/balancer_type> is
40C<::Random>.
41
42Sets the
43L<DBIx::Class::Storage::DBI::Replicated::Balancer::Random/master_read_weight> to
44C<1> by default, meaning that you have the same chance of reading from master as
45you do from replicants. Set to C<0> to turn off reads from master.
46
c4fee9b8 47=head1 CONFIG PARAMETERS
48
49=head2 replicants
50
51Array of connect_info settings for every replicant.
52
7314403a 53The following can be set via L<Catalyst::Model::DBIC::Schema/connect_info>, or
54as their own parameters. If set via separate parameters, they will override the
55settings in C<connect_info>.
56
57=head2 pool_type
58
59See L<DBIx::Class::Storage::DBI::Replicated/pool_type>.
60
61=head2 pool_args
62
63See L<DBIx::Class::Storage::DBI::Replicated/pool_args>.
64
65=head2 balancer_type
66
67See L<DBIx::Class::Storage::DBI::Replicated/balancer_type>.
68
69=head2 balancer_args
70
71See L<DBIx::Class::Storage::DBI::Replicated/balancer_args>.
72
c4fee9b8 73=cut
74
75has replicants => (
76 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
77);
78
45b10191 79has pool_type => (is => 'ro', isa => LoadableClass);
7314403a 80has pool_args => (is => 'ro', isa => HashRef);
81has balancer_type => (is => 'ro', isa => Str);
82has balancer_args => (is => 'ro', isa => HashRef);
83
c4fee9b8 84after 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
45b10191 93 use_module($class);
f24a5fbb 94
c4fee9b8 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 }
39f5f008 100
7314403a 101 my $connect_info = $self->connect_info;
bd309c0c 102
7314403a 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};
c4fee9b8 117};
118
73f72d28 119sub BUILD {}
7314403a 120
73f72d28 121after BUILD => sub {
c4fee9b8 122 my $self = shift;
123
7dfd616a 124 $self->storage->connect_replicants(map [ $_ ], @{ $self->replicants });
c4fee9b8 125};
126
127=head1 SEE ALSO
128
129L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
130L<DBIx::Class::Storage::DBI::Replicated>,
e203cd42 131L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>
c4fee9b8 132
133=head1 AUTHOR
134
4e251d1a 135See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
136L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
c4fee9b8 137
138=head1 COPYRIGHT
139
4e251d1a 140See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
141
142=head1 LICENSE
143
c4fee9b8 144This program is free software, you can redistribute it and/or modify it
145under the same terms as Perl itself.
146
147=cut
148
1491;