Add missing attribute to DBIx::Class::DeploymentHandler (force_overwrite)
[dbsrgits/DBIx-Class-DeploymentHandler.git] / lib / DBIx / Class / DeploymentHandler.pm
1 package DBIx::Class::DeploymentHandler;
2
3 # ABSTRACT: Extensible DBIx::Class deployment
4
5 use Moose;
6
7 extends 'DBIx::Class::DeploymentHandler::Dad';
8 # a single with would be better, but we can't do that
9 # see: http://rt.cpan.org/Public/Bug/Display.html?id=46347
10 with 'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
11     interface_role       => 'DBIx::Class::DeploymentHandler::HandlesDeploy',
12     class_name           => 'DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator',
13     delegate_name        => 'deploy_method',
14     attributes_to_assume => [qw(schema schema_version)],
15     attributes_to_copy   => [qw(
16       ignore_ddl databases script_directory sql_translator_args force_overwrite
17     )],
18   },
19   'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
20     interface_role       => 'DBIx::Class::DeploymentHandler::HandlesVersioning',
21     class_name           => 'DBIx::Class::DeploymentHandler::VersionHandler::Monotonic',
22     delegate_name        => 'version_handler',
23     attributes_to_assume => [qw( database_version schema_version to_version )],
24   },
25   'DBIx::Class::DeploymentHandler::WithApplicatorDumple' => {
26     interface_role       => 'DBIx::Class::DeploymentHandler::HandlesVersionStorage',
27     class_name           => 'DBIx::Class::DeploymentHandler::VersionStorage::Standard',
28     delegate_name        => 'version_storage',
29     attributes_to_assume => ['schema'],
30   };
31 with 'DBIx::Class::DeploymentHandler::WithReasonableDefaults';
32
33 sub prepare_version_storage_install {
34   my $self = shift;
35
36   $self->prepare_resultsource_install({
37     result_source => $self->version_storage->version_rs->result_source
38   });
39 }
40
41 sub install_version_storage {
42   my $self = shift;
43
44   my $version = (shift||{})->{version} || $self->schema_version;
45
46   $self->install_resultsource({
47     result_source => $self->version_storage->version_rs->result_source,
48     version       => $version,
49   });
50 }
51
52 sub prepare_install {
53   $_[0]->prepare_deploy;
54   $_[0]->prepare_version_storage_install;
55 }
56
57 # the following is just a hack so that ->version_storage
58 # won't be lazy
59 sub BUILD { $_[0]->version_storage }
60 __PACKAGE__->meta->make_immutable;
61
62 1;
63
64 #vim: ts=2 sw=2 expandtab
65
66 __END__
67
68 =head1 SYNOPSIS
69
70  use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
71  my $s = My::Schema->connect(...);
72
73  my $dh = DH->new({
74    schema              => $s,
75    databases           => 'SQLite',
76    sql_translator_args => { add_drop_table => 0 },
77  });
78
79  $dh->prepare_install;
80
81  $dh->install;
82
83 or for upgrades:
84
85  use aliased 'DBIx::Class::DeploymentHandler' => 'DH';
86  my $s = My::Schema->connect(...);
87
88  my $dh = DH->new({
89    schema              => $s,
90    databases           => 'SQLite',
91    sql_translator_args => { add_drop_table => 0 },
92  });
93
94  $dh->prepare_upgrade(1, 2);
95
96  $dh->upgrade;
97
98 =head1 DESCRIPTION
99
100 C<DBIx::Class::DeploymentHandler> is, as it's name suggests, a tool for
101 deploying and upgrading databases with L<DBIx::Class>.  It is designed to be
102 much more flexible than L<DBIx::Class::Schema::Versioned>, hence the use of
103 L<Moose> and lots of roles.
104
105 C<DBIx::Class::DeploymentHandler> itself is just a recommended set of roles
106 that we think will not only work well for everyone, but will also yeild the
107 best overall mileage.  Each role it uses has it's own nuances and
108 documentation, so I won't describe all of them here, but here are a few of the
109 major benefits over how L<DBIx::Class::Schema::Versioned> worked (and
110 L<DBIx::Class::DeploymentHandler::Deprecated> tries to maintain compatibility
111 with):
112
113 =over
114
115 =item *
116
117 Downgrades in addition to upgrades.
118
119 =item *
120
121 Multiple sql files files per upgrade/downgrade/install.
122
123 =item *
124
125 Perl scripts allowed for upgrade/downgrade/install.
126
127 =item *
128
129 Just one set of files needed for upgrade, unlike before where one might need
130 to generate C<factorial(scalar @versions)>, which is just silly.
131
132 =item *
133
134 And much, much more!
135
136 =back
137
138 That's really just a taste of some of the differences.  Check out each role for
139 all the details.
140
141 =head1 WHERE IS ALL THE DOC?!
142
143 C<DBIx::Class::DeploymentHandler> extends
144 L<DBIx::Class::DeploymentHandler::Dad>, so that's probably the first place to
145 look when you are trying to figure out how everything works.
146
147 Next would be to look at all the pieces that fill in the blanks that
148 L<DBIx::Class::DeploymentHandler::Dad> expects to be filled.  They would be
149 L<DBIx::Class::DeploymentHandler::DeployMethod::SQL::Translator>,
150 L<DBIx::Class::DeploymentHandler::VersionHandler::Monotonic>,
151 L<DBIx::Class::DeploymentHandler::VersionStorage::Standard>, and
152 L<DBIx::Class::DeploymentHandler::WithReasonableDefaults>.
153
154 =method prepare_version_storage_install
155
156  $dh->prepare_version_storage_install
157
158 Creates the needed C<.sql> file to install the version storage and not the rest
159 of the tables
160
161 =method prepare_install
162
163  $dh->prepare_install
164
165 First prepare all the tables to be installed and the prepare just the version
166 storage
167
168 =method install_version_storage
169
170  $dh->install_version_storage
171
172 Install the version storage and not the rest of the tables
173
174 =head1 THIS SUCKS
175
176 You started your project and weren't using C<DBIx::Class::DeploymentHandler>?
177 Lucky for you I had you in mind when I wrote this doc.
178
179 First off, you'll want to just install the C<version_storage>:
180
181  my $s = My::Schema->connect(...);
182  my $dh = DBIx::Class::DeploymentHandler->({ schema => $s });
183
184  $dh->prepare_version_storage_install;
185  $dh->install_version_storage;
186
187 Then set your database version:
188
189  $dh->add_database_version({ version => $s->version });
190
191 Now you should be able to use C<DBIx::Class::DeploymentHandler> like normal!
192
193 =head1 LOGGING
194
195 This is a complex tool, and because of that sometimes you'll want to see
196 what exactly is happening.  The best way to do that is to use the built in
197 logging functionality.  It the standard six log levels; C<fatal>, C<error>,
198 C<warn>, C<info>, C<debug>, and C<trace>.  Most of those are pretty self
199 explanatory.  Generally a safe level to see what all is going on is debug,
200 which will give you everything except for the exact SQL being run.
201
202 To enable the various logging levels all you need to do is set an environment
203 variables: C<DBICDH_FATAL>, C<DBICDH_ERROR>, C<DBICDH_WARN>, C<DBICDH_INFO>,
204 C<DBICDH_DEBUG>, and C<DBICDH_TRACE>.  Each level can be set on it's own,
205 but the default is the first three on and the last three off, and the levels
206 cascade, so if you turn on trace the rest will turn on automatically.
207
208 =head1 DONATIONS
209
210 If you'd like to thank me for the work I've done on this module, don't give me
211 a donation. I spend a lot of free time creating free software, but I do it
212 because I love it.
213
214 Instead, consider donating to someone who might actually need it.  Obviously
215 you should do research when donating to a charity, so don't just take my word
216 on this.  I like Children's Survival Fund:
217 L<http://www.childrenssurvivalfund.org>, but there are a host of other
218 charities that can do much more good than I will with your money.