1 package DBIx::Class::DeploymentHandler::Dad;
3 # ABSTRACT: Parent class for DeploymentHandlers
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'
16 isa => 'DBIx::Class::Schema',
21 has backup_directory => (
24 predicate => 'has_backup_directory',
33 sub _build_to_version { $_[0]->schema_version }
35 has schema_version => (
41 sub _build_schema_version { $_[0]->schema->schema_version }
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;
48 my $ddl = $self->deploy;
50 $self->add_database_version({
51 version => $self->to_version,
57 log_info { '[DBICDH] upgrading' };
59 while ( my $version_list = $self->next_version_set ) {
60 my ($ddl, $upgrade_sql) = @{
61 $self->upgrade_single_step({ version_set => $version_list })
64 $self->add_database_version({
65 version => $version_list->[-1],
67 upgrade_sql => $upgrade_sql,
73 log_info { '[DBICDH] upgrading' };
75 while ( my $version_list = $self->previous_version_set ) {
76 $self->downgrade_single_step({ version_set => $version_list });
78 # do we just delete a row here? I think so but not sure
79 $self->delete_database_version({ version => $version_list->[-1] });
84 log_info { '[DBICDH] backing up' };
85 $self->storage->backup($self->backup_directory)
88 __PACKAGE__->meta->make_immutable;
92 # vim: ts=2 sw=2 expandtab
100 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
101 and generate the DDL.
105 The version that the schema is currently at. Defaults to
106 C<< $self->schema->schema_version >>.
108 =attr backup_directory
110 The directory where backups are stored
114 The version (defaults to schema's version) to migrate the database to
120 Deploys the current schema into the database. Populates C<version_storage> with
121 C<version> and C<ddl>.
123 B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
126 B<Note>: you cannot install on top of an already installed database
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>.
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
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.
153 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
157 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
159 =head2 version_storage_is_installed
161 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
163 =head2 add_database_version
165 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
167 =head2 delete_database_version
169 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
171 =head2 next_version_set
173 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
175 =head2 previous_version_set
177 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
179 =head2 upgrade_single_step
181 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
183 =head2 downgrade_single_step
185 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
187 =head1 ORTHODOX METHODS
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.
194 =head2 database_version
196 see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
198 =head2 prepare_deploy
200 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
202 =head2 prepare_resultsource_install
204 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
206 =head2 install_resultsource
208 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
210 =head2 prepare_upgrade
212 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
214 =head2 prepare_downgrade
216 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
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.
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>.