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