1 package DBIx::Class::DeploymentHandler::Dad;
3 # ABSTRACT: Parent class for DeploymentHandlers
6 require DBIx::Class::Schema; # loaded for type constraint
7 use Carp::Clan '^DBIx::Class::DeploymentHandler';
8 use DBIx::Class::DeploymentHandler::Logger;
9 use DBIx::Class::DeploymentHandler::Types;
10 use Log::Contextual ':log', -package_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 => (
37 isa => 'StrSchemaVersion',
41 sub _build_schema_version { $_[0]->schema->schema_version }
46 my $version = (shift @_ || {})->{version} || $self->to_version;
47 log_info { "installing version $version" };
48 croak 'Install not possible as versions table already exists in database'
49 if $self->version_storage_is_installed;
51 my $ddl = $self->deploy({ version=> $version });
53 $self->add_database_version({
54 version => $self->to_version,
60 log_info { 'upgrading' };
63 while ( my $version_list = $self->next_version_set ) {
65 my ($ddl, $upgrade_sql) = @{
66 $self->upgrade_single_step({ version_set => $version_list })
69 $self->add_database_version({
70 version => $version_list->[-1],
72 upgrade_sql => $upgrade_sql,
76 log_warn { 'no need to run upgrade' } unless $ran_once;
80 log_info { 'downgrading' };
83 while ( my $version_list = $self->previous_version_set ) {
85 $self->downgrade_single_step({ version_set => $version_list });
87 # do we just delete a row here? I think so but not sure
88 $self->delete_database_version({ version => $version_list->[0] });
90 log_warn { 'no version to run downgrade' } unless $ran_once;
95 log_info { 'backing up' };
96 $self->schema->storage->backup($self->backup_directory)
99 __PACKAGE__->meta->make_immutable;
103 # vim: ts=2 sw=2 expandtab
111 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
112 and generate the DDL.
116 The version that the schema is currently at. Defaults to
117 C<< $self->schema->schema_version >>.
119 =attr backup_directory
121 The directory where backups are stored
125 The version (defaults to schema's version) to migrate the database to
133 $dh->install({ version => 1 })
135 Deploys the requested version into the database Version defaults to
136 L</schema_version>. Populates C<version_storage> with C<version> and C<ddl>.
138 B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
141 B<Note>: you cannot install on top of an already installed database
147 Upgrades the database one step at a time till L</next_version_set>
148 returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
149 C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
150 returned from L</upgrade_single_step>.
156 Downgrades the database one step at a time till L</previous_version_set>
157 returns C<undef>. Each downgrade step will delete a C<version> from the
164 Simply calls backup on the C<< $schema->storage >>, passing in
165 C<< $self->backup_directory >> as an argument. Please test yourself before
166 assuming it will work.
168 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
172 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
174 =head2 version_storage_is_installed
176 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
178 =head2 add_database_version
180 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
182 =head2 delete_database_version
184 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
186 =head2 next_version_set
188 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
190 =head2 previous_version_set
192 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
194 =head2 upgrade_single_step
196 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
198 =head2 downgrade_single_step
200 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
202 =head1 ORTHODOX METHODS
204 These methods are not actually B<required> as things will probably still work
205 if you don't implement them, but if you want your subclass to get along with
206 other subclasses (or more likely, tools made to use another subclass), you
207 should probably implement these too, even if they are no-ops.
209 =head2 database_version
211 see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
213 =head2 prepare_deploy
215 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
217 =head2 prepare_resultsource_install
219 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
221 =head2 install_resultsource
223 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
225 =head2 prepare_upgrade
227 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
229 =head2 prepare_downgrade
231 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
235 All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
236 L</ORTHODOX METHODS> can be implemented in any fashion you choose. In the
237 spirit of code reuse I have used roles to implement them in my two subclasses,
238 L<DBIx::Class::DeploymentHandler> and
239 L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
240 them entirely in a subclass if you so choose to.
242 For in-depth documentation on how methods are supposed to work, see the roles
243 L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
244 L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
245 L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.