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