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 { '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 { 'upgrading' };
60 while ( my $version_list = $self->next_version_set ) {
62 my ($ddl, $upgrade_sql) = @{
63 $self->upgrade_single_step({ version_set => $version_list })
66 $self->add_database_version({
67 version => $version_list->[-1],
69 upgrade_sql => $upgrade_sql,
73 log_warn { 'no need to run upgrade' } unless $ran_once;
77 log_info { 'upgrading' };
80 while ( my $version_list = $self->previous_version_set ) {
82 $self->downgrade_single_step({ version_set => $version_list });
84 # do we just delete a row here? I think so but not sure
85 $self->delete_database_version({ version => $version_list->[-1] });
87 log_warn { 'no version to run downgrade' } unless $ran_once;
91 log_info { 'backing up' };
92 $self->storage->backup($self->backup_directory)
95 __PACKAGE__->meta->make_immutable;
99 # vim: ts=2 sw=2 expandtab
107 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
108 and generate the DDL.
112 The version that the schema is currently at. Defaults to
113 C<< $self->schema->schema_version >>.
115 =attr backup_directory
117 The directory where backups are stored
121 The version (defaults to schema's version) to migrate the database to
127 Deploys the current schema into the database. Populates C<version_storage> with
128 C<version> and C<ddl>.
130 B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
133 B<Note>: you cannot install on top of an already installed database
139 Upgrades the database one step at a time till L</next_version_set>
140 returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
141 C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
142 returned from L</upgrade_single_step>.
148 Downgrades the database one step at a time till L</previous_version_set>
149 returns C<undef>. Each downgrade step will delete a C<version> from the
156 Simply calls backup on the C<< $schema->storage >>, passing in
157 C<< $self->backup_directory >> as an argument. Please test yourself before
158 assuming it will work.
160 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
164 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
166 =head2 version_storage_is_installed
168 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
170 =head2 add_database_version
172 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
174 =head2 delete_database_version
176 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
178 =head2 next_version_set
180 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
182 =head2 previous_version_set
184 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
186 =head2 upgrade_single_step
188 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
190 =head2 downgrade_single_step
192 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
194 =head1 ORTHODOX METHODS
196 These methods are not actually B<required> as things will probably still work
197 if you don't implement them, but if you want your subclass to get along with
198 other subclasses (or more likely, tools made to use another subclass), you
199 should probably implement these too, even if they are no-ops.
201 =head2 database_version
203 see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
205 =head2 prepare_deploy
207 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
209 =head2 prepare_resultsource_install
211 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
213 =head2 install_resultsource
215 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
217 =head2 prepare_upgrade
219 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
221 =head2 prepare_downgrade
223 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
227 All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
228 L</ORTHODOX METHODS> can be implemented in any fashion you choose. In the
229 spirit of code reuse I have used roles to implement them in my two subclasses,
230 L<DBIx::Class::DeploymentHandler> and
231 L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
232 them entirely in a subclass if you so choose to.
234 For in-depth documentation on how methods are supposed to work, see the roles
235 L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
236 L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
237 L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.