C::M::DBIC::Schema - warn on empty schema
[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;
39f5f008 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'],
26 ]
27 });
28
29=head1 DESCRIPTION
30
31B<DOES NOT WORK YET> -- requires some DBIC changes
32
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
37=head1 CONFIG PARAMETERS
38
39=head2 replicants
40
41Array of connect_info settings for every replicant.
42
43=cut
44
45has replicants => (
46 is => 'ro', isa => ConnectInfos, coerce => 1, required => 1
47);
48
49after setup => sub {
50 my $self = shift;
51
52# check storage_type compatibility (if configured)
53 if (my $storage_type = $self->storage_type) {
54 my $class = $storage_type =~ /^::/ ?
55 "DBIx::Class::Storage$storage_type"
56 : $storage_type;
57
58 croak "This storage_type cannot be used with replication"
59 unless $class->isa('DBIx::Class::Storage::DBI::Replicated');
60 } else {
61 $self->storage_type('::DBI::Replicated');
62 }
39f5f008 63
64 $self->connect_info->{balancer_type} ||= '::Random';
c4fee9b8 65};
66
67after finalize => sub {
68 my $self = shift;
69
39f5f008 70 $self->storage->connect_replicants(map [ $_ ], $self->replicants->flatten);
c4fee9b8 71};
72
73=head1 SEE ALSO
74
75L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>,
76L<DBIx::Class::Storage::DBI::Replicated>,
77L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached>
78
79=head1 AUTHOR
80
81Rafael Kitover, C<rkitover@cpan.org>
82
83=head1 COPYRIGHT
84
85This program is free software, you can redistribute it and/or modify it
86under the same terms as Perl itself.
87
88=cut
89
901;