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