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