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