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