1 package DBIx::Class::DeploymentHandler::Dad;
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';
11 isa => 'DBIx::Class::Schema',
14 handles => ['schema_version'],
17 has upgrade_directory => ( # configuration
24 has backup_directory => ( # configuration
27 predicate => 'has_backup_directory',
30 has to_version => ( # configuration
35 sub _build_to_version { $_[0]->schema->schema_version }
37 has databases => ( # configuration
39 isa => 'DBIx::Class::DeploymentHandler::Databases',
41 default => sub { [qw( MySQL SQLite PostgreSQL )] },
44 has sqltargs => ( # configuration
47 default => sub { {} },
51 croak 'Install not possible as versions table already exists in database'
52 if $self->version_storage_is_installed;
54 my $ddl = $self->deploy;
56 $self->add_database_version({
57 version => $self->to_version,
64 while ( my $version_list = $self->next_version_set ) {
65 my ($ddl, $upgrade_sql) = @{$self->upgrade_single_step($version_list)||[]};
67 $self->add_database_version({
68 version => $version_list->[-1],
70 upgrade_sql => $upgrade_sql,
77 while ( my $version_list = $self->previous_version_set ) {
78 $self->downgrade_single_step($version_list);
80 # do we just delete a row here? I think so but not sure
81 $self->delete_database_version({ version => $version_list->[-1] });
85 method backup { $self->storage->backup($self->backup_directory) }
87 __PACKAGE__->meta->make_immutable;
95 The L<DBIx::Class::Schema> (B<required>) that is used to talk to the database
98 # this should be in a different place, maybe the SQLT role
99 # this should be renamed
100 =attr upgrade_directory
102 The directory (default C<'sql'>) that upgrades are stored in
104 =attr backup_directory
106 The directory that backups are stored in
110 The version (defaults to schema's version) to migrate the database to
112 # this should be in a different place, maybe the SQLT role
115 The types of databases (default C<< [qw( MySQL SQLite PostgreSQL )] >>) to
123 Deploys the current schema into the database. Populates C<version_storage> with
124 C<version> and C<ddl>.
126 B<Note>: you typically need to call C<< $dh->prepare_install >> before you call
129 B<Note>: you cannot install on top of an already installed database
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>.
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
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.
156 =head1 METHODS THAT ARE REQUIRED IN SUBCLASSES
158 =head2 version_storage_is_installed
160 warn q(I can't version this database!)
161 unless $dh->version_storage_is_installed
163 return true iff the version storage is installed.
169 Deploy the schema to the database.
171 =head2 add_database_version
173 $dh->add_database_version({
175 ddl => $ddl # can be undef,
176 upgrade_sql => $sql # can be undef,
179 Store a new version into the version storage
181 =head2 delete_database_version
183 $dh->delete_database_version({ version => '1.02' })
185 simply deletes given database version from the version storage
187 =head2 next_version_set
189 print 'versions to install: ';
190 while (my $vs = $dh->next_version_set) {
191 print join q(, ), @{$vs}
195 return an arrayref describing each version that needs to be
196 installed to upgrade to C<< $dh->to_version >>.
198 =head2 previous_version_set
200 print 'versions to uninstall: ';
201 while (my $vs = $dh->previous_version_set) {
202 print join q(, ), @{$vs}
206 return an arrayref describing each version that needs to be
207 "installed" to downgrade to C<< $dh->to_version >>.
209 =head2 upgrade_single_step
211 my ($ddl, $sql) = @{$dh->upgrade_single_step($version_set)||[]}
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.
217 =head2 downgrade_single_step
219 $dh->upgrade_single_step($version_set);
221 call a single downgrade migration. Takes an arrayref describing the version to
226 vim: ts=2 sw=2 expandtab