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