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