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