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