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