add missing dep
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Dad.pm
CommitLineData
c4f4d4a2 1package DBIx::Class::DeploymentHandler::Dad;
2
9deabd1f 3# ABSTRACT: Parent class for DeploymentHandlers
4
c4f4d4a2 5use Moose;
6use Method::Signatures::Simple;
c4f4d4a2 7require DBIx::Class::Schema; # loaded for type constraint
c4f4d4a2 8use Carp::Clan '^DBIx::Class::DeploymentHandler';
0df68524 9use Log::Contextual::WarnLogger;
10use Log::Contextual ':log', -default_logger => Log::Contextual::WarnLogger->new({
11 env_prefix => 'DBICDH'
12});
c4f4d4a2 13
14has schema => (
15 isa => 'DBIx::Class::Schema',
16 is => 'ro',
17 required => 1,
c4f4d4a2 18);
19
cfc9edf9 20has backup_directory => (
c4f4d4a2 21 isa => 'Str',
22 is => 'ro',
23 predicate => 'has_backup_directory',
24);
25
cfc9edf9 26has to_version => (
c4f4d4a2 27 is => 'ro',
e86c0c07 28 isa => 'Str',
c4f4d4a2 29 lazy_build => 1,
30);
31
ed45e175 32sub _build_to_version { $_[0]->schema_version }
c4f4d4a2 33
cefb17d4 34has schema_version => (
35 is => 'ro',
e86c0c07 36 isa => 'Str',
cefb17d4 37 lazy_build => 1,
38);
39
40sub _build_schema_version { $_[0]->schema->schema_version }
41
c4f4d4a2 42method install {
0df68524 43 log_info { '[DBICDH] installing version ' . $self->to_version };
c4f4d4a2 44 croak 'Install not possible as versions table already exists in database'
45 if $self->version_storage_is_installed;
46
7d2a6974 47 my $ddl = $self->deploy;
c4f4d4a2 48
34ac0a51 49 $self->add_database_version({
c4f4d4a2 50 version => $self->to_version,
51 ddl => $ddl,
52 });
53}
54
55sub upgrade {
0df68524 56 log_info { '[DBICDH] upgrading' };
c4f4d4a2 57 my $self = shift;
58 while ( my $version_list = $self->next_version_set ) {
be140a5f 59 my ($ddl, $upgrade_sql) = @{
60 $self->upgrade_single_step({ version_set => $version_list })
61 ||[]};
c4f4d4a2 62
63 $self->add_database_version({
64 version => $version_list->[-1],
392a5ccc 65 ddl => $ddl,
66 upgrade_sql => $upgrade_sql,
c4f4d4a2 67 });
68 }
69}
70
7d2a6974 71sub downgrade {
0df68524 72 log_info { '[DBICDH] upgrading' };
7d2a6974 73 my $self = shift;
74 while ( my $version_list = $self->previous_version_set ) {
be140a5f 75 $self->downgrade_single_step({ version_set => $version_list });
c4f4d4a2 76
7d2a6974 77 # do we just delete a row here? I think so but not sure
78 $self->delete_database_version({ version => $version_list->[-1] });
79 }
392a5ccc 80}
81
0df68524 82method backup {
83 log_info { '[DBICDH] backing up' };
84 $self->storage->backup($self->backup_directory)
85}
7d2a6974 86
c4f4d4a2 87__PACKAGE__->meta->make_immutable;
88
891;
90
e52174e3 91# vim: ts=2 sw=2 expandtab
92
93__END__
94
c4f4d4a2 95=pod
96
97=attr schema
98
34ac0a51 99The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
100and generate the DDL.
101
f7e215c9 102=attr schema_version
103
104The version that the schema is currently at. Defaults to
105C<< $self->schema->schema_version >>.
106
c4f4d4a2 107=attr backup_directory
108
9b80ce72 109The directory where backups are stored
34ac0a51 110
c4f4d4a2 111=attr to_version
112
34ac0a51 113The version (defaults to schema's version) to migrate the database to
114
c4f4d4a2 115=method install
116
34ac0a51 117 $dh->install
118
119Deploys the current schema into the database. Populates C<version_storage> with
120C<version> and C<ddl>.
121
91557c90 122B<Note>: you typically need to call C<< $dh->prepare_deploy >> before you call
34ac0a51 123this method.
124
125B<Note>: you cannot install on top of an already installed database
126
c4f4d4a2 127=method upgrade
128
34ac0a51 129 $dh->upgrade
130
131Upgrades the database one step at a time till L</next_version_set>
132returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
133C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
134returned from L</upgrade_single_step>.
135
136=method downgrade
137
138 $dh->downgrade
139
140Downgrades the database one step at a time till L</previous_version_set>
d1ae780e 141returns C<undef>. Each downgrade step will delete a C<version> from the
34ac0a51 142version storage.
143
c4f4d4a2 144=method backup
145
34ac0a51 146 $dh->backup
147
148Simply calls backup on the C<< $schema->storage >>, passing in
149C<< $self->backup_directory >> as an argument. Please test yourself before
150assuming it will work.
151
152=head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
153
5228a963 154=head2 deploy
155
156See L<DBIx::Class::DeploymentHandler::HandlesDeploy/deploy>.
157
34ac0a51 158=head2 version_storage_is_installed
159
5228a963 160See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/version_storage_is_installed>.
34ac0a51 161
5228a963 162=head2 add_database_version
34ac0a51 163
5228a963 164See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/add_database_version>.
34ac0a51 165
5228a963 166=head2 delete_database_version
34ac0a51 167
5228a963 168See L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/delete_database_version>.
34ac0a51 169
5228a963 170=head2 next_version_set
34ac0a51 171
5228a963 172See L<DBIx::Class::DeploymentHandler::HandlesVersioning/next_version_set>.
34ac0a51 173
5228a963 174=head2 previous_version_set
34ac0a51 175
5228a963 176See L<DBIx::Class::DeploymentHandler::HandlesVersioning/previous_version_set>.
34ac0a51 177
5228a963 178=head2 upgrade_single_step
34ac0a51 179
5228a963 180See L<DBIx::Class::DeploymentHandler::HandlesDeploy/upgrade_single_step>.
34ac0a51 181
5228a963 182=head2 downgrade_single_step
34ac0a51 183
5228a963 184See L<DBIx::Class::DeploymentHandler::HandlesDeploy/downgrade_single_step>.
34ac0a51 185
5228a963 186=head1 ORTHODOX METHODS
34ac0a51 187
5228a963 188These methods are not actually B<required> as things will probably still work
189if you don't implement them, but if you want your subclass to get along with
190other subclasses (or more likely, tools made to use another subclass), you
191should probably implement these too, even if they are no-ops.
34ac0a51 192
5228a963 193=head2 database_version
34ac0a51 194
5228a963 195see L<DBIx::Class::DeploymentHandler::HandlesVersionStorage/database_version>
34ac0a51 196
91557c90 197=head2 prepare_deploy
34ac0a51 198
91557c90 199see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_deploy>
34ac0a51 200
5228a963 201=head2 prepare_resultsource_install
202
d1ae780e 203see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_resultsource_install>
5228a963 204
205=head2 install_resultsource
206
207see L<DBIx::Class::DeploymentHandler::HandlesDeploy/install_resultsource>
208
209=head2 prepare_upgrade
210
211see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_upgrade>
212
213=head2 prepare_downgrade
214
215see L<DBIx::Class::DeploymentHandler::HandlesDeploy/prepare_downgrade>
34ac0a51 216
ed1721b9 217=head2 SUBCLASSING
218
219All of the methods mentioned in L</METHODS THAT ARE REQUIRED IN SUBCLASSES> and
220L</ORTHODOX METHODS> can be implemented in any fashion you choose. In the
221spirit of code reuse I have used roles to implement them in my two subclasses,
222L<DBIx::Class::DeploymentHandler> and
223L<DBIx::Class::DeploymentHandler::Deprecated>, but you are free to implement
224them entirely in a subclass if you so choose to.
225
226For in-depth documentation on how methods are supposed to work, see the roles
227L<DBIx::Class::DeploymentHandler::HandlesDeploy>,
228L<DBIx::Class::DeploymentHandler::HandlesVersioning>, and
229L<DBIx::Class::DeploymentHandler::HandlesVersionStorage>.
34ac0a51 230