Commit | Line | Data |
c4fee9b8 |
1 | package Catalyst::Model::DBIC::Schema::Role::Replicated; |
2 | |
3 | use Moose::Role; |
4 | use Moose::Autobox; |
5 | use Carp::Clan '^Catalyst::Model::DBIC::Schema'; |
6 | |
7 | use Catalyst::Model::DBIC::Schema::Types 'ConnectInfos'; |
8 | |
9 | use namespace::clean -except => 'meta'; |
10 | |
11 | =head1 NAME |
12 | |
13 | Catalyst::Model::DBIC::Schema::Role::Replicated - Replicated storage support for |
14 | L<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 | |
31 | B<DOES NOT WORK YET> -- requires some DBIC changes |
32 | |
33 | Sets your storage_type to L<DBIx::Class::Storage::DBI::Replicated> and connects |
34 | replicants provided in config. See that module for supported resultset |
35 | attributes. |
36 | |
37 | =head1 CONFIG PARAMETERS |
38 | |
39 | =head2 replicants |
40 | |
41 | Array of connect_info settings for every replicant. |
42 | |
43 | =cut |
44 | |
45 | has replicants => ( |
46 | is => 'ro', isa => ConnectInfos, coerce => 1, required => 1 |
47 | ); |
48 | |
49 | after 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 | } |
63 | }; |
64 | |
65 | after finalize => sub { |
66 | my $self = shift; |
67 | |
68 | $self->storage->connect_replicants($self->replicants->flatten); |
69 | }; |
70 | |
71 | =head1 SEE ALSO |
72 | |
73 | L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class>, |
74 | L<DBIx::Class::Storage::DBI::Replicated>, |
75 | L<Cache::FastMmap>, L<DBIx::Class::Cursor::Cached> |
76 | |
77 | =head1 AUTHOR |
78 | |
79 | Rafael Kitover, C<rkitover@cpan.org> |
80 | |
81 | =head1 COPYRIGHT |
82 | |
83 | This program is free software, you can redistribute it and/or modify it |
84 | under the same terms as Perl itself. |
85 | |
86 | =cut |
87 | |
88 | 1; |