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