doc for ::Dad and fix a tiny bug
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler / Dad.pm
CommitLineData
c4f4d4a2 1package DBIx::Class::DeploymentHandler::Dad;
2
3use Moose;
4use Method::Signatures::Simple;
5use DBIx::Class::DeploymentHandler::Types;
6require DBIx::Class::Schema; # loaded for type constraint
7require DBIx::Class::ResultSet; # loaded for type constraint
8use Carp::Clan '^DBIx::Class::DeploymentHandler';
9
10has schema => (
11 isa => 'DBIx::Class::Schema',
12 is => 'ro',
13 required => 1,
14 handles => ['schema_version'],
15);
16
17has upgrade_directory => ( # configuration
18 isa => 'Str',
19 is => 'ro',
20 required => 1,
21 default => 'sql',
22);
23
24has backup_directory => ( # configuration
25 isa => 'Str',
26 is => 'ro',
27 predicate => 'has_backup_directory',
28);
29
30has to_version => ( # configuration
31 is => 'ro',
32 lazy_build => 1,
33);
34
35sub _build_to_version { $_[0]->schema->schema_version }
36
37has databases => ( # configuration
38 coerce => 1,
39 isa => 'DBIx::Class::DeploymentHandler::Databases',
40 is => 'ro',
41 default => sub { [qw( MySQL SQLite PostgreSQL )] },
42);
43
44has sqltargs => ( # configuration
45 isa => 'HashRef',
46 is => 'ro',
47 default => sub { {} },
48);
49
50method 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
62sub 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 75sub 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 85method backup { $self->storage->backup($self->backup_directory) }
86
c4f4d4a2 87__PACKAGE__->meta->make_immutable;
88
891;
90
91=pod
92
93=attr schema
94
34ac0a51 95The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
96and 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 102The directory (default C<'sql'>) that upgrades are stored in
103
c4f4d4a2 104=attr backup_directory
105
34ac0a51 106The directory that backups are stored in
107
c4f4d4a2 108=attr to_version
109
34ac0a51 110The 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 115The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to
116generate files for
117
118
c4f4d4a2 119=method install
120
34ac0a51 121 $dh->install
122
123Deploys the current schema into the database. Populates C<version_storage> with
124C<version> and C<ddl>.
125
126B<Note>: you typically need to call C<< $dh->prepare_install >> before you call
127this method.
128
129B<Note>: you cannot install on top of an already installed database
130
c4f4d4a2 131=method upgrade
132
34ac0a51 133 $dh->upgrade
134
135Upgrades the database one step at a time till L</next_version_set>
136returns C<undef>. Each upgrade step will add a C<version>, C<ddl>, and
137C<upgrade_sql> to the version storage (if C<ddl> and/or C<upgrade_sql> are
138returned from L</upgrade_single_step>.
139
140=method downgrade
141
142 $dh->downgrade
143
144Downgrades the database one step at a time till L</previous_version_set>
145returns C<undef>. Each downgrade step will delete a C<version>from the
146version storage.
147
c4f4d4a2 148=method backup
149
34ac0a51 150 $dh->backup
151
152Simply calls backup on the C<< $schema->storage >>, passing in
153C<< $self->backup_directory >> as an argument. Please test yourself before
154assuming 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
163return true iff the version storage is installed.
164
165=head2 deploy
166
167 $dh->deploy
168
169Deploy 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
179Store a new version into the version storage
180
181=head2 delete_database_version
182
183 $dh->delete_database_version({ version => '1.02' })
184
185simply 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
195return an arrayref describing each version that needs to be
196installed 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
206return 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
213call a single upgrade migration. Takes an arrayref describing the version to
214upgrade to. Optionally return an arrayref containing C<$ddl> describing
215version 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
221call a single downgrade migration. Takes an arrayref describing the version to
222downgrade to.
223
c4f4d4a2 224__END__
225
226vim: ts=2 sw=2 expandtab