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 Log::Contextual::WarnLogger;
10 use Log::Contextual ':log', -default_logger => Log::Contextual::WarnLogger->new({
11 env_prefix => 'DBICDH'
15 isa => 'DBIx::Class::Schema',
20 has backup_directory => (
23 predicate => 'has_backup_directory',
32 sub _build_to_version { $_[0]->schema_version }
34 has schema_version => (
40 sub _build_schema_version { $_[0]->schema->schema_version }
43 log_info { '[DBICDH] installing version ' . $self->to_version };
44 croak 'Install not possible as versions table already exists in database'
45 if $self->version_storage_is_installed;
47 my $ddl = $self->deploy;
49 $self->add_database_version({
50 version => $self->to_version,
56 log_info { '[DBICDH] upgrading' };
58 while ( my $version_list = $self->next_version_set ) {
59 my ($ddl, $upgrade_sql) = @{
60 $self->upgrade_single_step({ version_set => $version_list })
63 $self->add_database_version({
64 version => $version_list->[-1],
66 upgrade_sql => $upgrade_sql,
72 log_info { '[DBICDH] upgrading' };
74 while ( my $version_list = $self->previous_version_set ) {
75 $self->downgrade_single_step({ version_set => $version_list });
77 # do we just delete a row here? I think so but not sure
78 $self->delete_database_version({ version => $version_list->[-1] });
83 log_info { '[DBICDH] backing up' };
84 $self->storage->backup($self->backup_directory)
87 __PACKAGE__->meta->make_immutable;
91 # vim: ts=2 sw=2 expandtab
99 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
100 and generate the DDL.
104 The version that the schema is currently at. Defaults to
105 C<< $self->schema->schema_version >>.
107 =attr backup_directory
109 The directory where backups are stored
113 The version (defaults to schema's version) to migrate the database to
119 Deploys the current schema into the database. Populates C<version_storage> with
120 C<version> and C<ddl>.
122 B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
125 B<Note>: you cannot install on top of an already installed database
131 Upgrades the database one step at a time till L</next_version_set>
132 returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
133 C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
134 returned from L</upgrade_single_step>.
140 Downgrades the database one step at a time till L</previous_version_set>
141 returns C<undef>. Each downgrade step will delete a C<version> from the
148 Simply calls backup on the C<< $schema->storage >>, passing in
149 C<< $self->backup_directory >> as an argument. Please test yourself before
150 assuming it will work.
152 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
156 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
158 =head2 version_storage_is_installed
160 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
162 =head2 add_database_version
164 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
166 =head2 delete_database_version
168 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
170 =head2 next_version_set
172 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
174 =head2 previous_version_set
176 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
178 =head2 upgrade_single_step
180 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
182 =head2 downgrade_single_step
184 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
186 =head1 ORTHODOX METHODS
188 These methods are not actually B<required> as things will probably still work
189 if you don't implement them, but if you want your subclass to get along with
190 other subclasses (or more likely, tools made to use another subclass), you
191 should probably implement these too, even if they are no-ops.
193 =head2 database_version
195 see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
197 =head2 prepare_deploy
199 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
201 =head2 prepare_resultsource_install
203 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
205 =head2 install_resultsource
207 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
209 =head2 prepare_upgrade
211 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
213 =head2 prepare_downgrade
215 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
219 All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
220 L</ORTHODOX METHODS> can be implemented in any fashion you choose. In the
221 spirit of code reuse I have used roles to implement them in my two subclasses,
222 L<DBIx::Class::DeploymentHandler> and
223 L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
224 them entirely in a subclass if you so choose to.
226 For in-depth documentation on how methods are supposed to work, see the roles
227 L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
228 L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
229 L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.