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