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