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