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