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