1 package DBIx::Class::DeploymentHandler::Dad;
3 # ABSTRACT: Parent class for DeploymentHandlers
6 use Carp::Clan '^DBIx::Class::DeploymentHandler';
7 use DBIx::Class::DeploymentHandler::LogImporter ':log';
8 use DBIx::Class::DeploymentHandler::Types 'StrSchemaVersion';
9 use MooX::Types::MooseLike::Base qw(Str);
16 has backup_directory => (
19 predicate => 'has_backup_directory',
25 builder => '_build_to_version',
28 sub _build_to_version { $_[0]->schema_version }
30 has schema_version => (
32 isa => StrSchemaVersion,
33 builder => '_build_schema_version',
36 sub _build_schema_version { $_[0]->schema->schema_version }
41 my $version = (shift @_ || {})->{version} || $self->to_version;
42 log_info { "installing version $version" };
43 croak 'Install not possible as versions table already exists in database'
44 if $self->version_storage_is_installed;
47 my $ddl = $self->deploy({ version=> $version });
49 $self->add_database_version({
57 log_info { 'upgrading' };
61 while ( my $version_list = $self->next_version_set ) {
63 my ($ddl, $upgrade_sql) = @{
64 $self->upgrade_single_step({ version_set => $version_list })
67 $self->add_database_version({
68 version => $version_list->[-1],
70 upgrade_sql => $upgrade_sql,
75 log_warn { 'no need to run upgrade' } unless $ran_once;
79 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] });
91 log_warn { 'no version to run downgrade' } unless $ran_once;
96 log_info { 'backing up' };
97 $self->schema->storage->backup($self->backup_directory)
102 # vim: ts=2 sw=2 expandtab
110 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
111 and generate the DDL.
115 The version that the schema is currently at. Defaults to
116 C<< $self->schema->schema_version >>.
118 =attr backup_directory
120 The directory where backups are stored
124 The version (defaults to schema's version) to migrate the database to
132 $dh->install({ version => 1 })
134 Deploys the requested version into the database Version defaults to
135 L</schema_version>. Populates C<version_storage> with C<version> and C<ddl>.
137 B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
140 B<Note>: you cannot install on top of an already installed database
146 Upgrades the database one step at a time till L</next_version_set>
147 returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
148 C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
149 returned from L</upgrade_single_step>.
155 Downgrades the database one step at a time till L</previous_version_set>
156 returns C<undef>. Each downgrade step will delete a C<version> from the
163 Simply calls backup on the C<< $schema->storage >>, passing in
164 C<< $self->backup_directory >> as an argument. Please test yourself before
165 assuming it will work.
167 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
171 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
173 =head2 version_storage_is_installed
175 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
177 =head2 add_database_version
179 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
181 =head2 delete_database_version
183 See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
185 =head2 next_version_set
187 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
189 =head2 previous_version_set
191 See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
193 =head2 upgrade_single_step
195 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
197 =head2 downgrade_single_step
199 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
203 See L<DBIx::Class::DeploymentHandler::HandlesDeploy/txn_do>.
205 =head1 ORTHODOX METHODS
207 These methods are not actually B<required> as things will probably still work
208 if you don't implement them, but if you want your subclass to get along with
209 other subclasses (or more likely, tools made to use another subclass), you
210 should probably implement these too, even if they are no-ops.
212 =head2 database_version
214 see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
216 =head2 prepare_deploy
218 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
220 =head2 prepare_resultsource_install
222 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
224 =head2 install_resultsource
226 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
228 =head2 prepare_upgrade
230 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
232 =head2 prepare_downgrade
234 see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
238 All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
239 L</ORTHODOX METHODS> can be implemented in any fashion you choose. In the
240 spirit of code reuse I have used roles to implement them in my two subclasses,
241 L<DBIx::Class::DeploymentHandler> and
242 L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
243 them entirely in a subclass if you so choose to.
245 For in-depth documentation on how methods are supposed to work, see the roles
246 L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
247 L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
248 L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.