nicer logging
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Dad.pm
1 package DBIx::Class::DeploymentHandler::Dad;
2
3 # ABSTRACT: Parent class for DeploymentHandlers
4
5 use Moose;
6 use Method::Signatures::Simple;
7 require DBIx::Class::Schema;    # loaded for type constraint
8 use Carp::Clan '^DBIx::Class::DeploymentHandler';
9 use DBIx::Class::DeploymentHandler::Logger;
10 use Log::Contextual ':log', -default_logger =>
11   DBIx::Class::DeploymentHandler::Logger->new({
12     env_prefix => 'DBICDH'
13   });
14
15 has schema => (
16   isa      => 'DBIx::Class::Schema',
17   is       => 'ro',
18   required => 1,
19 );
20
21 has backup_directory => (
22   isa => 'Str',
23   is  => 'ro',
24   predicate  => 'has_backup_directory',
25 );
26
27 has to_version => (
28   is         => 'ro',
29   isa        => 'Str',
30   lazy_build => 1,
31 );
32
33 sub _build_to_version { $_[0]->schema_version }
34
35 has schema_version => (
36   is         => 'ro',
37   isa        => 'Str',
38   lazy_build => 1,
39 );
40
41 sub _build_schema_version { $_[0]->schema->schema_version }
42
43 method install {
44   log_info { '[DBICDH] installing version ' . $self->to_version };
45   croak 'Install not possible as versions table already exists in database'
46     if $self->version_storage_is_installed;
47
48   my $ddl = $self->deploy;
49
50   $self->add_database_version({
51     version     => $self->to_version,
52     ddl         => $ddl,
53   });
54 }
55
56 sub upgrade {
57   log_info { '[DBICDH] upgrading' };
58   my $self = shift;
59   while ( my $version_list = $self->next_version_set ) {
60     my ($ddl, $upgrade_sql) = @{
61       $self->upgrade_single_step({ version_set => $version_list })
62     ||[]};
63
64     $self->add_database_version({
65       version     => $version_list->[-1],
66       ddl         => $ddl,
67       upgrade_sql => $upgrade_sql,
68     });
69   }
70 }
71
72 sub downgrade {
73   log_info { '[DBICDH] upgrading' };
74   my $self = shift;
75   while ( my $version_list = $self->previous_version_set ) {
76     $self->downgrade_single_step({ version_set => $version_list });
77
78     # do we just delete a row here?  I think so but not sure
79     $self->delete_database_version({ version => $version_list->[-1] });
80   }
81 }
82
83 method backup {
84   log_info { '[DBICDH] backing up' };
85   $self->storage->backup($self->backup_directory)
86 }
87
88 __PACKAGE__->meta->make_immutable;
89
90 1;
91
92 # vim: ts=2 sw=2 expandtab
93
94 __END__
95
96 =pod
97
98 =attr schema
99
100 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
101 and generate the DDL.
102
103 =attr schema_version
104
105 The version that the schema is currently at.  Defaults to
106 C<< $self->schema->schema_version >>.
107
108 =attr backup_directory
109
110 The directory where backups are stored
111
112 =attr to_version
113
114 The version (defaults to schema's version) to migrate the database to
115
116 =method install
117
118  $dh->install
119
120 Deploys the current schema into the database.  Populates C<version_storage> with
121 C<version> and C<ddl>.
122
123 B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
124 this method.
125
126 B<Note>: you cannot install on top of an already installed database
127
128 =method upgrade
129
130  $dh->upgrade
131
132 Upgrades the database one step at a time till L</next_version_set>
133 returns C<undef>.  Each upgrade step will add a C<version>, C<ddl>, and
134 C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
135 returned from L</upgrade_single_step>.
136
137 =method downgrade
138
139  $dh->downgrade
140
141 Downgrades the database one step at a time till L</previous_version_set>
142 returns C<undef>.  Each downgrade step will delete a C<version> from the
143 version storage.
144
145 =method backup
146
147  $dh->backup
148
149 Simply calls backup on the C<< $schema->storage >>, passing in
150 C<< $self->backup_directory >> as an argument.  Please test yourself before
151 assuming it will work.
152
153 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
154
155 =head2 deploy
156
157 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
158
159 =head2 version_storage_is_installed
160
161 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
162
163 =head2 add_database_version
164
165 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
166
167 =head2 delete_database_version
168
169 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
170
171 =head2 next_version_set
172
173 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
174
175 =head2 previous_version_set
176
177 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
178
179 =head2 upgrade_single_step
180
181 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
182
183 =head2 downgrade_single_step
184
185 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
186
187 =head1 ORTHODOX METHODS
188
189 These methods are not actually B<required> as things will probably still work
190 if you don't implement them, but if you want your subclass to get along with
191 other subclasses (or more likely, tools made to use another subclass), you
192 should probably implement these too, even if they are no-ops.
193
194 =head2 database_version
195
196 see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
197
198 =head2 prepare_deploy
199
200 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
201
202 =head2 prepare_resultsource_install
203
204 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
205
206 =head2 install_resultsource
207
208 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
209
210 =head2 prepare_upgrade
211
212 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
213
214 =head2 prepare_downgrade
215
216 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
217
218 =head2 SUBCLASSING
219
220 All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
221 L</ORTHODOX METHODS> can be implemented in any fashion you choose.  In the
222 spirit of code reuse I have used roles to implement them in my two subclasses,
223 L<DBIx::Class::DeploymentHandler> and
224 L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
225 them entirely in a subclass if you so choose to.
226
227 For in-depth documentation on how methods are supposed to work, see the roles
228 L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
229 L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
230 L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.
231