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