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