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