File Coverage

blib/lib/DBIx/Class/DeploymentHandler/Dad.pm
Criterion Covered Total %
statement 41 50 82.0
branch 6 10 60.0
condition 2 3 66.6
subroutine 12 19 63.1
pod 4 4 100.0
total 65 86 75.5


line stmt bran cond sub pod time code
1             package DBIx::Class::DeploymentHandler::Dad;
2             $DBIx::Class::DeploymentHandler::Dad::VERSION = '0.002233';
3             # ABSTRACT: Parent class for DeploymentHandlers
4              
5 12     12   7850 use Moose;
  12         26  
  12         68  
6             require DBIx::Class::Schema; # loaded for type constraint
7 12     12   71812 use Carp::Clan '^DBIx::Class::DeploymentHandler';
  12         16226  
  12         75  
8 12     12   5367 use DBIx::Class::DeploymentHandler::LogImporter ':log';
  12         41  
  12         163  
9 12     12   5081 use DBIx::Class::DeploymentHandler::Types;
  12         57  
  12         6737  
10              
11             has schema => (
12             is => 'ro',
13             required => 1,
14             );
15              
16             has backup_directory => (
17             isa => 'Str',
18             is => 'ro',
19             predicate => 'has_backup_directory',
20             );
21              
22             has to_version => (
23             is => 'ro',
24             isa => 'Str',
25             lazy_build => 1,
26             );
27              
28             sub _build_to_version {
29 31     31   1007 my $version = $_[0]->schema_version;
30 31 50       985 ref($version) ? $version->numify : $version;
31             }
32              
33             has schema_version => (
34             is => 'ro',
35             lazy_build => 1,
36             );
37              
38 36     36   1004 sub _build_schema_version { $_[0]->schema->schema_version }
39              
40             sub install {
41 19     19 1 231978 my $self = shift;
42              
43 19   66     662 my $version = (shift @_ || {})->{version} || $self->to_version;
44 19     0   185 log_info { "installing version $version" };
  0         0  
45 19 100       1035 croak 'Install not possible as versions table already exists in database'
46             if $self->version_storage_is_installed;
47              
48             $self->txn_do(sub {
49 12     12   100 my $ddl = $self->deploy({ version=> $version });
50              
51 12         1597 $self->add_database_version({
52             version => $version,
53             ddl => $ddl,
54             });
55 12         232 });
56             }
57              
58             sub upgrade {
59 0     0 1 0 log_info { 'upgrading' };
  15     15   113687  
60 15         778 my $self = shift;
61 15         37 my $ran_once = 0;
62             $self->txn_do(sub {
63 15     15   219 while ( my $version_list = $self->next_version_set ) {
64 15         35 $ran_once = 1;
65             my ($ddl, $upgrade_sql) = @{
66 15 50       30 $self->upgrade_single_step({ version_set => $version_list })
  15         112  
67             ||[]};
68              
69 15         186 $self->add_database_version({
70             version => $version_list->[-1],
71             ddl => $ddl,
72             upgrade_sql => $upgrade_sql,
73             });
74             }
75 15         160 });
76              
77 15 50   0   1156 log_warn { 'no need to run upgrade' } unless $ran_once;
  0         0  
78             }
79              
80             sub downgrade {
81 0     0 1 0 log_info { 'downgrading' };
  7     7   23774  
82 7         350 my $self = shift;
83 7         17 my $ran_once = 0;
84             $self->txn_do(sub {
85 7     7   51 while ( my $version_list = $self->previous_version_set ) {
86 7         17 $ran_once = 1;
87 7         52 $self->downgrade_single_step({ version_set => $version_list });
88              
89             # do we just delete a row here? I think so but not sure
90 7         80 $self->delete_database_version({ version => $version_list->[0] });
91             }
92 7         64 });
93 7 50   0   563 log_warn { 'no version to run downgrade' } unless $ran_once;
  0            
94             }
95              
96             sub backup {
97 0     0 1   my $self = shift;
98 0     0     log_info { 'backing up' };
  0            
99 0           $self->schema->storage->backup($self->backup_directory)
100             }
101              
102             __PACKAGE__->meta->make_immutable;
103              
104             1;
105              
106             # vim: ts=2 sw=2 expandtab
107              
108             __END__
109              
110             =pod
111              
112             =head1 NAME
113              
114             DBIx::Class::DeploymentHandler::Dad - Parent class for DeploymentHandlers
115              
116             =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
117              
118             =head2 deploy
119              
120             See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
121              
122             =head2 version_storage_is_installed
123              
124             See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
125              
126             =head2 add_database_version
127              
128             See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
129              
130             =head2 delete_database_version
131              
132             See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
133              
134             =head2 next_version_set
135              
136             See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
137              
138             =head2 previous_version_set
139              
140             See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
141              
142             =head2 upgrade_single_step
143              
144             See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
145              
146             =head2 downgrade_single_step
147              
148             See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
149              
150             =head2 txn_do
151              
152             See L<DBIx::Class::DeploymentHandler::HandlesDeploy/txn_do>.
153              
154             =head1 ORTHODOX METHODS
155              
156             These methods are not actually B<required> as things will probably still work
157             if you don't implement them, but if you want your subclass to get along with
158             other subclasses (or more likely, tools made to use another subclass), you
159             should probably implement these too, even if they are no-ops.
160              
161             =head2 database_version
162              
163             see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
164              
165             =head2 prepare_deploy
166              
167             see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
168              
169             =head2 prepare_resultsource_install
170              
171             see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
172              
173             =head2 install_resultsource
174              
175             see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
176              
177             =head2 prepare_upgrade
178              
179             see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
180              
181             =head2 prepare_downgrade
182              
183             see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
184              
185             =head2 SUBCLASSING
186              
187             All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
188             L</ORTHODOX METHODS> can be implemented in any fashion you choose. In the
189             spirit of code reuse I have used roles to implement them in my two subclasses,
190             L<DBIx::Class::DeploymentHandler> and
191             L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
192             them entirely in a subclass if you so choose to.
193              
194             For in-depth documentation on how methods are supposed to work, see the roles
195             L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
196             L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
197             L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.
198              
199             =head1 ATTRIBUTES
200              
201             =head2 schema
202              
203             The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
204             and generate the DDL.
205              
206             =head2 schema_version
207              
208             The version that the schema is currently at. Defaults to
209             C<< $self->schema->schema_version >>.
210              
211             =head2 backup_directory
212              
213             The directory where backups are stored
214              
215             =head2 to_version
216              
217             The version (defaults to schema's version) to migrate the database to
218              
219             =head1 METHODS
220              
221             =head2 install
222              
223             $dh->install
224              
225             or
226              
227             $dh->install({ version => 1 })
228              
229             Deploys the requested version into the database Version defaults to
230             L</schema_version>. Populates C<version_storage> with C<version> and C<ddl>.
231              
232             B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
233             this method.
234              
235             B<Note>: you cannot install on top of an already installed database
236              
237             =head2 upgrade
238              
239             $dh->upgrade
240              
241             Upgrades the database one step at a time till L</next_version_set>
242             returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
243             C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
244             returned from L</upgrade_single_step>.
245              
246             =head2 downgrade
247              
248             $dh->downgrade
249              
250             Downgrades the database one step at a time till L</previous_version_set>
251             returns C<undef>. Each downgrade step will delete a C<version> from the
252             version storage.
253              
254             =head2 backup
255              
256             $dh->backup
257              
258             Simply calls backup on the C<< $schema->storage >>, passing in
259             C<< $self->backup_directory >> as an argument. Please test yourself before
260             assuming it will work.
261              
262             =head1 AUTHOR
263              
264             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
265              
266             =head1 COPYRIGHT AND LICENSE
267              
268             This software is copyright (c) 2019 by Arthur Axel "fREW" Schmidt.
269              
270             This is free software; you can redistribute it and/or modify it under
271             the same terms as the Perl 5 programming language system itself.
272              
273             =cut