blib/lib/DBIx/Class/DeploymentAdapter.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 115 CC 9 SCAR 21.6
CC
9
Cov
0%
upgrade_incremental
20.0
dh
12.0
install
6.0
CRAP 8.6
1 package DBIx::Class::DeploymentAdapter;
2 use 5.008001;
3 use strict;
4 use warnings;
5  
6 our $VERSION = "0.08";
7  
8 =encoding utf-8
9  
10 =head1 NAME
11  
12 DBIx::Class::DeploymentAdapter - Deployment handler adapter to your DBIC app, which offers some candy
13  
14 =head1 SYNOPSIS
15  
16 use DBIx::Class::DeploymentAdapter;
17  
18 my $args = {
19 schema => $schema,
20 script_directory => './share/migrations',
21 databases => ['MySQL'],
22 sql_translator_args => { mysql_enable_utf8 => 1 },
23 };
24  
25 $args->{to_version} = $to_version if $to_version;
26 $args->{force_overwrite} = $force_overwrite if $force_overwrite;
27  
28 my $da = DBIx::Class::DeploymentAdapter->new($args);
29  
30 =head1 DESCRIPTION
31  
32 Deployment handler adapter to your DBIC app, which offers some candy
33  
34 =cut
35  
36 use DBIx::Class::DeploymentHandler;
37  
38 use Moose;
39  
40 has dh_store => (
41 is => "rw",
42 isa => "Maybe[Object]"
43 );
44  
45 sub dh {
46  
47 my ( $self, $args ) = @_;
48  
49 if ( !$self->dh_store ) {
50  
51 return unless $args && $args->{schema};
52  
53 $args->{script_directory} ||= "./share/migrations";
54 $args->{databases} ||= ["MySQL"];
55 $args->{sql_translator_args} ||= { mysql_enable_utf8 => 1 };
56  
57 my $dh = DBIx::Class::DeploymentHandler->new($args);
58 $self->dh_store( $dh );
59  
60 }
61  
62 return $self->dh_store;
63 }
64  
65 sub BUILD {
66  
67 my $self = shift;
68 my $args = shift;
69  
70 $self->dh($args);
71 }
72  
73 =head2 install
74  
75 Installs the schema files to the given Database
76  
77 $da->install;
78  
79 =cut
80  
81 sub install {
82  
83 my $self = shift;
84 my @params = @_;
85  
86 return unless $self->dh;
87  
88 $self->dh->install(@params);
89 }
90  
91 =head2 prepare
92  
93 Summarize all prepares from L<DBIx::Class::DeploymentHandler> in one Command
94  
95 $da->prepare;
96  
97 =cut
98  
99 sub prepare {
100  
101 my ($self) = @_;
102  
103 return unless $self->dh;
104  
105 my $start_version = $self->dh->database_version;
106 my $target_version = $self->dh->schema->schema_version;
107  
108 $self->dh->prepare_install;
109  
110 $self->dh->prepare_upgrade(
111 {
112 from_version => $start_version,
113 to_version => $target_version,
114 }
115 );
116  
117 $self->dh->prepare_downgrade(
118 {
119 from_version => $target_version,
120 to_version => $start_version,
121 }
122 );
123 }
124  
125 =head2 status
126  
127 Returns the Status of database and schema versions as string
128  
129 $da->status;
130  
131 =cut
132  
133 sub status {
134  
135 my ( $self ) = @_;
136  
137 return unless ref $self->dh;
138  
139 my $deployed_version = $self->dh->database_version;
140 my $schema_version = $self->dh->schema->schema_version;
141  
142 return sprintf( "Schema is %s\nDeployed database is %s\n", $schema_version, $deployed_version );
143  
144 }
145  
146 =head2 upgrade_incremental
147  
148 Upgrade the database version step by step, if anything wents wrong, it dies with the specific database error.
149  
150 You can give a target version to the method to make it stop there
151  
152 $da->upgrade_incremental;
153 $da->upgrade_incremental(112);
154  
155 =cut
156  
157 sub upgrade_incremental {
158  
159 my ( $self, $to_version ) = @_;
160  
161 return unless $self->dh;
162  
163 my $start_version = $self->dh->database_version + 1;
164 my $target_version = $self->dh->schema->schema_version;
165  
166 for my $upgrade_version ( $start_version .. $target_version ) {
167  
168 my $version = $self->dh->database_version;
169  
170 if( $to_version && $upgrade_version > $to_version ) {
171 next;
172 }
173  
174 warn "upgrading to version $upgrade_version\n";
175  
176 eval {
177 my ( $ddl, $sql ) = @{ $self->dh->upgrade_single_step( { version_set => [ $version, $upgrade_version ] } ) || [] }; # from last version to desired version
178 $self->dh->add_database_version(
179 {
180 version => $upgrade_version,
181 ddl => $ddl,
182 upgrade_sql => $sql,
183 }
184 );
185 };
186  
187 if ($@) {
188 my $error_version = $self->dh->database_version;
189 warn "Database remains on version $error_version";
190 die "UPGRADE ERROR - Version $error_version upgrading to $version: " . $@;
191 }
192 }
193 }
194  
195 1;
196  
197  
198  
199 =head1 LICENSE
200  
201 Copyright (C) Patrick Kilter.
202  
203 This library is free software; you can redistribute it and/or modify
204 it under the same terms as Perl itself.
205  
206 =head1 AUTHOR
207  
208 Patrick Kilter E<lt>pk@gassmann.itE<gt>
209  
210 =cut