Commit | Line | Data |
c4f4d4a2 |
1 | package DBIx::Class::DeploymentHandler::Dad; |
2 | |
3 | use Moose; |
4 | use Method::Signatures::Simple; |
5 | use DBIx::Class::DeploymentHandler::Types; |
6 | require DBIx::Class::Schema; # loaded for type constraint |
7 | require DBIx::Class::ResultSet; # loaded for type constraint |
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 | |
17 | has upgrade_directory => ( # configuration |
18 | isa => 'Str', |
19 | is => 'ro', |
20 | required => 1, |
21 | default => 'sql', |
22 | ); |
23 | |
24 | has backup_directory => ( # configuration |
25 | isa => 'Str', |
26 | is => 'ro', |
27 | predicate => 'has_backup_directory', |
28 | ); |
29 | |
30 | has to_version => ( # configuration |
31 | is => 'ro', |
32 | lazy_build => 1, |
33 | ); |
34 | |
35 | sub _build_to_version { $_[0]->schema->schema_version } |
36 | |
37 | has databases => ( # configuration |
38 | coerce => 1, |
39 | isa => 'DBIx::Class::DeploymentHandler::Databases', |
40 | is => 'ro', |
41 | default => sub { [qw( MySQL SQLite PostgreSQL )] }, |
42 | ); |
43 | |
44 | has sqltargs => ( # configuration |
45 | isa => 'HashRef', |
46 | is => 'ro', |
47 | default => sub { {} }, |
48 | ); |
49 | |
50 | method install { |
51 | croak 'Install not possible as versions table already exists in database' |
52 | if $self->version_storage_is_installed; |
53 | |
7d2a6974 |
54 | my $ddl = $self->deploy; |
c4f4d4a2 |
55 | |
34ac0a51 |
56 | $self->add_database_version({ |
c4f4d4a2 |
57 | version => $self->to_version, |
58 | ddl => $ddl, |
59 | }); |
60 | } |
61 | |
62 | sub upgrade { |
63 | my $self = shift; |
64 | while ( my $version_list = $self->next_version_set ) { |
7d2a6974 |
65 | my ($ddl, $upgrade_sql) = @{$self->upgrade_single_step($version_list)||[]}; |
c4f4d4a2 |
66 | |
67 | $self->add_database_version({ |
68 | version => $version_list->[-1], |
392a5ccc |
69 | ddl => $ddl, |
70 | upgrade_sql => $upgrade_sql, |
c4f4d4a2 |
71 | }); |
72 | } |
73 | } |
74 | |
7d2a6974 |
75 | sub downgrade { |
76 | my $self = shift; |
77 | while ( my $version_list = $self->previous_version_set ) { |
78 | $self->downgrade_single_step($version_list); |
c4f4d4a2 |
79 | |
7d2a6974 |
80 | # do we just delete a row here? I think so but not sure |
81 | $self->delete_database_version({ version => $version_list->[-1] }); |
82 | } |
392a5ccc |
83 | } |
84 | |
7d2a6974 |
85 | method backup { $self->storage->backup($self->backup_directory) } |
86 | |
c4f4d4a2 |
87 | __PACKAGE__->meta->make_immutable; |
88 | |
89 | 1; |
90 | |
91 | =pod |
92 | |
93 | =attr schema |
94 | |
34ac0a51 |
95 | The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database |
96 | and generate the DDL. |
97 | |
98 | # this should be in a different place, maybe the SQLT role |
99 | # this should be renamed |
c4f4d4a2 |
100 | =attr upgrade_directory |
101 | |
34ac0a51 |
102 | The directory (default C<'sql'>) that upgrades are stored in |
103 | |
c4f4d4a2 |
104 | =attr backup_directory |
105 | |
34ac0a51 |
106 | The directory that backups are stored in |
107 | |
c4f4d4a2 |
108 | =attr to_version |
109 | |
34ac0a51 |
110 | The version (defaults to schema's version) to migrate the database to |
111 | |
112 | # this should be in a different place, maybe the SQLT role |
c4f4d4a2 |
113 | =attr databases |
114 | |
34ac0a51 |
115 | The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to |
116 | generate files for |
117 | |
118 | |
c4f4d4a2 |
119 | =method install |
120 | |
34ac0a51 |
121 | $dh->install |
122 | |
123 | Deploys the current schema into the database. Populates C<version_storage> with |
124 | C<version> and C<ddl>. |
125 | |
126 | B<Note>: you typically need to call C<< $dh->prepare_install >> before you call |
127 | this method. |
128 | |
129 | B<Note>: you cannot install on top of an already installed database |
130 | |
c4f4d4a2 |
131 | =method upgrade |
132 | |
34ac0a51 |
133 | $dh->upgrade |
134 | |
135 | Upgrades the database one step at a time till L</next_version_set> |
136 | returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and |
137 | C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are |
138 | returned from L</upgrade_single_step>. |
139 | |
140 | =method downgrade |
141 | |
142 | $dh->downgrade |
143 | |
144 | Downgrades the database one step at a time till L</previous_version_set> |
145 | returns C<undef>. Each downgrade step will delete a C<version>from the |
146 | version storage. |
147 | |
c4f4d4a2 |
148 | =method backup |
149 | |
34ac0a51 |
150 | $dh->backup |
151 | |
152 | Simply calls backup on the C<< $schema->storage >>, passing in |
153 | C<< $self->backup_directory >> as an argument. Please test yourself before |
154 | assuming it will work. |
155 | |
156 | =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES |
157 | |
158 | =head2 version_storage_is_installed |
159 | |
160 | warn q(I can't version this database!) |
161 | unless $dh->version_storage_is_installed |
162 | |
163 | return true iff the version storage is installed. |
164 | |
165 | =head2 deploy |
166 | |
167 | $dh->deploy |
168 | |
169 | Deploy the schema to the database. |
170 | |
171 | =head2 add_database_version |
172 | |
173 | $dh->add_database_version({ |
174 | version => '1.02', |
175 | ddl => $ddl # can be undef, |
176 | upgrade_sql => $sql # can be undef, |
177 | }); |
178 | |
179 | Store a new version into the version storage |
180 | |
181 | =head2 delete_database_version |
182 | |
183 | $dh->delete_database_version({ version => '1.02' }) |
184 | |
185 | simply deletes given database version from the version storage |
186 | |
187 | =head2 next_version_set |
188 | |
189 | print 'versions to install: '; |
190 | while (my $vs = $dh->next_version_set) { |
191 | print join q(, ), @{$vs} |
192 | } |
193 | print qq(\n); |
194 | |
195 | return an arrayref describing each version that needs to be |
196 | installed to upgrade to C<< $dh->to_version >>. |
197 | |
198 | =head2 previous_version_set |
199 | |
200 | print 'versions to uninstall: '; |
201 | while (my $vs = $dh->previous_version_set) { |
202 | print join q(, ), @{$vs} |
203 | } |
204 | print qq(\n); |
205 | |
206 | return an arrayref describing each version that needs to be |
207 | "installed" to downgrade to C<< $dh->to_version >>. |
208 | |
209 | =head2 upgrade_single_step |
210 | |
211 | my ($ddl, $sql) = @{$dh->upgrade_single_step($version_set)||[]} |
212 | |
213 | call a single upgrade migration. Takes an arrayref describing the version to |
214 | upgrade to. Optionally return an arrayref containing C<$ddl> describing |
215 | version installed and C<$sql> used to get to that version. |
216 | |
217 | =head2 downgrade_single_step |
218 | |
219 | $dh->upgrade_single_step($version_set); |
220 | |
221 | call a single downgrade migration. Takes an arrayref describing the version to |
222 | downgrade to. |
223 | |
c4f4d4a2 |
224 | __END__ |
225 | |
226 | vim: ts=2 sw=2 expandtab |