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