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