Deploy.pm

filter: Statementstmt 82.7% 101 / 122 Branchbran 63.1% 24 / 38 Conditioncond 21.4% 3 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 80.7% 21 / 26 Podpod 33.3% 4 / 12 totaltotal 70.0% 154 / 220 CC 27 SCAR 21.1
CC
27
Cov
74%
connectable
42.0
parse
10.5
deploy
7.0
CRAP 8.2
↑ Summary Deploy/Carp.pm »
1 package DBIx::SQLite::Deploy;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 DBIx::SQLite::Deploy - Easy SQLite deployment
9  
10 =head1 VERSION
11  
12 Version 0.011
13  
14 =cut
15  
16 2 our $VERSION = '0.011';
17  
18 =head1 SYNOPSIS
19  
20 # ::Deploy will create the 'path/to' for you if it does not already exist
21 my $deploy = DBIx::SQLite::Deploy->deploy( path/to/database.sqlite => <<_END_ )
22 [% PRIMARY_KEY = "INTEGER PRIMARY KEY AUTOINCREMENT" %]
23 [% KEY = "INTEGER" %]
24 [% CLEAR %]
25 ---
26 CREATE TABLE artist (
27  
28 id [% PRIMARY_KEY %],
29 uuid TEXT NOT NULL,
30  
31 name TEXT,
32 description TEXT,
33  
34 UNIQUE (uuid)
35 );
36 ---
37 CREATE TABLE cd (
38  
39 id [% PRIMARY_KEY %],
40  
41 title TEXT,
42 description TEXT
43 );
44 _END_
45  
46 To use with DBI
47  
48 $dbh = $deploy->connect
49  
50 # ...or the long way:
51  
52 $dbh = DBI->connect( $deploy->information )
53  
54 To use with L<DBIx::Class>
55  
56 $schema = My::Schema->connect( $deploy->information )
57  
58 =head1 DESCRIPTION
59  
60 DBIx::SQLite::Deploy is a tool for creating a database and getting back a DBI connection in as little work as possible. Essentially, you pass the path
61 of your database and the schema (as a Template Toolkit template) to C<< DBIx::SQLite::Deploy->deploy >>. If the database is not there (file does not exist or is size 0), then
62 ::Deploy will create the database and install the schema
63  
64 =head1 Why Template Toolkit?
65  
66 Purely as a convenience. You probably have lots of repetition in your schema, and TT gives a way to combat that redundancy. You don't need to use it if you don't want/need to.
67  
68 =head1 USAGE
69  
70 =head2 $deploy = DBIx::SQLite::Deploy->deploy( <path>, [ <schema> ], ... )
71  
72 Create a new deployment using <path> as the file for the SQLite database, and <schema> as the (optional) schema
73  
74 The schema argument can be in the form of a Template Toolkit document.
75  
76 The database will NOT be created until you ask to C<< ->connect >>, ask for C<< ->information >>, or manually C<< ->deploy >>. To do creation on construction, pass
77 C<< create => 1 >> as an argument
78  
79 DBIx::SQLite::Deploy will not deploy over an existing database (the file exists and has non-zero size)
80  
81 =head2 $deploy->connect
82  
83 Return a L<DBI> database handle (C<$dbh>)
84  
85 =head2 $deploy->information
86  
87 =head2 $deploy->info
88  
89 Return a list of connection information, suitable for passing to C<< DBI->connect >>
90  
91 =head2 $deploy->deploy
92  
93 Deploy the database unless it already exists
94  
95 =cut
96  
97 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use DBIx::SQLite::Deploy::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 2 has schema_parser => qw/is ro lazy_build 1/;
101 1 ▶sub _build_schema_parser {
Subroutine _build_schema_parser: called CC=1 SCAR=0.0
102 1 require SQL::Script;
103 1 return SQL::Script->new( split_by => qr/\n\s*-{2,4}\n/ );
104 };
105  
106 2 has tt => qw/is ro lazy_build 1/;
107 1 ▶sub _build_tt {
Subroutine _build_tt: called CC=1 SCAR=0.0
108 1 require Template;
109 1 return Template->new({});
110 };
111  
112 2 has schema => qw/is ro/;
113 2 has connection => qw/is ro required 1/;
114  
115 3 ▶sub _deploy {
Subroutine _deploy: called CC=2 SCAR=6.9
116 3 my $class = shift;
117 3 my ($connection, $schema) = (shift, shift);
118 3 my %given = @_;
119 3 @given{qw/ connection schema /} = ( $connection, $schema );
120  
121 3 $connection = DBIx::SQLite::Deploy::Connection->parse( delete $given{connection} );
122  
123 3 my $create = delete $given{create};
124 3 my $deploy = $class->new( connection => $connection, %given );
125 3 ▶ $deploy->deploy if $create;
Branch: 1 branch
BranchTrueFalseTotal
if $create 0 3 3
126 3 return $deploy;
127 }
128  
129 7 ▶sub deploy {
Subroutine deploy: called CC=7 SCAR=19.5
Pod documented
130 7 ▶ return shift->_deploy( @_ ) unless ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 3 4 7
131 4 my $self = shift;
132  
133 4 my $connection = $self->connection;
134  
135 4 ▶ if ( my $schema = $self->schema ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $schema = $self->schema) 3 1 4
136  
137 3 ▶ unless ( $connection->database_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connection->database_exists) 1 2 3
138 {
139 1 my $input = $schema;
140 1 my $output;
141 1 ▶ $self->tt->process( \$input, {}, \$output ) or die $self->tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$self->tt->process(\$input, {}, \$output) or die $self->tt->error 1 0 1
142 1 $schema = $output;
143 }
144 1 $self->schema_parser->read( \$schema );
145 1 my @statements = $self->schema_parser->statements;
146 {
147 1 my $dbh = $connection->connect;
148 1 for my $statement ( @statements ) {
149 2 chomp $statement;
150 2 ▶ $dbh->do( $statement ) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($statement) or die $dbh->errstr 2 0 2
151 }
152 1 $dbh->disconnect;
153 }
154 }
155 }
156  
157 4 $connection->disconnect; # TODO huh?
158  
159 4 return $connection->information;
160 }
161  
162 1 ▶sub information {
Subroutine information: called CC=3 SCAR=11.3
Pod documented
163 1 my $self = shift;
164 1 my %given = @_;
165 1 ▶ $given{deploy} = 1 unless exists $given{deploy};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $given{"deploy"} 1 0 1
166 1 ▶ $self->deploy if $given{deploy};
Branch: 1 branch
BranchTrueFalseTotal
if $given{"deploy"} 1 0 1
167 1 return $self->connection->information;
168 }
169  
170 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
171 0 return shift->information( @_ );
172 }
173  
174 3 ▶sub connect {
Subroutine connect: called CC=3 SCAR=11.3
Pod documented
175 3 my $self = shift;
176 3 my %given = @_;
177 3 ▶ $given{deploy} = 1 unless exists $given{deploy};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $given{"deploy"} 3 0 3
178 3 ▶ $self->deploy if $given{deploy};
Branch: 1 branch
BranchTrueFalseTotal
if $given{"deploy"} 3 0 3
179 3 return $self->connection->connect;
180 }
181  
182 2 1;
183  
184 package DBIx::SQLite::Deploy::Connection;
185  
186 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
187 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
188  
189 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 2 ▶use DBIx::SQLite::Deploy::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
191  
192 2 has [qw/ source database username password attributes /] => qw/is ro/;
193 2 has handle => qw/ is ro lazy_build 1 /;
194 0 ▶sub _build_handle {
Subroutine _build_handle: not called CC=1 SCAR=6.9
195 0 my $self = shift;
196 0 return $self->connect;
197 }
198  
199 0 ▶sub dbh {
Subroutine dbh: not called CC=1 SCAR=6.9
Pod undocumented
200 0 return shift->handle;
201 }
202  
203 0 ▶sub open {
Subroutine open: not called CC=1 SCAR=6.9
Pod undocumented
204 0 return shift->handle;
205 }
206  
207 4 ▶sub close {
Subroutine close: called CC=2 SCAR=9.2
Pod undocumented
208 4 my $self = shift;
209 4 ▶ if ( $self->{handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handle"}) 0 4 4
210 0 $self->handle->disconnect;
211 0 $self->meta->get_attribute( 'handle' )->clear_value( $self );
212 }
213 }
214  
215 4 ▶sub disconnect {
Subroutine disconnect: called CC=1 SCAR=0.0
Pod undocumented
216 4 my $self = shift;
217 4 return $self->close;
218 }
219  
220 4 ▶sub connect {
Subroutine connect: called CC=1 SCAR=0.0
221 4 my $self = shift;
222 4 require DBI;
223 4 return DBI->connect( $self->information );
224 }
225  
226 4 ▶before connect => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
227 4 require Path::Class;
228 4 my $self = shift;
229 4 my $database = Path::Class::Dir->new( $self->database );
230 4 ▶ $database->parent->mkpath unless -d $database->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $database->parent 1 3 4
231 2 };
232  
233 0 ▶sub connectable {
Subroutine connectable: not called CC=6 SCAR=37.4
Pod undocumented
234 0 my $self = shift;
235  
236 0 my ($source, $username, $password, $attributes) = $self->information;
237 0 ▶ $attributes ||= {};
Condition0%0/2
$attributes ||= {}
l!l
00
MC/DC0%0/1
$attributes ||= {}
$attributes
Truth table0%0/2
A
A= $attributes
Aresult
11
00
238 0 $attributes->{$_} = 0 for qw/PrintWarn PrintError RaiseError/;
239 0 my $dbh = DBI->connect($source, $username, $password, $attributes);
240 0 ▶ my $success = $dbh && ! $dbh->err && $dbh->ping;
Condition0%0/6
$dbh && !$dbh->err && $dbh->ping
!ll&&!rl&&r
000
$dbh and not $dbh->err
!ll&&!rl&&r
000
MC/DC0%0/3
$dbh && !$dbh->err && $dbh->ping
$dbh not $dbh->err $dbh->ping
Truth table0%0/4
A and not(B) && C
A= $dbhB= not $dbh->errC= $dbh->ping
ABCresult
0XX0
10X0
1100
1111
241 0 ▶ $dbh->disconnect if $dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $dbh 0 0 0
242 0 return $success;
243 }
244  
245  
246 3 ▶sub database_exists {
Subroutine database_exists: called CC=3 SCAR=11.1
Pod undocumented
247 3 my $self = shift;
248 3 ▶ return -f $self->database && -s _ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-f $self->database && -s _ ? : 2 1 3
Condition66%2/3
-f $self->database and -s _
!ll&&!rl&&r
102
MC/DC50%1/2
-f $self->database and -s _
-f $self->database -s _
Truth table66%2/3
A and B
A= -f $self->databaseB= -s _
ABresult
0X0
100
111
249 }
250  
251 3 ▶sub parse {
Subroutine parse: called CC=6 SCAR=23.5
Pod undocumented
252 3 my $class = shift;
253 3 my $given = shift;
254  
255 3 my ( $database, $attributes );
256 3 ▶ if ( ref $given eq "ARRAY" ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $given eq 'ARRAY') { } 0 3 3
elsif (ref $given eq 'HASH') { } 0 3 3
elsif (builtin::blessed($given) and $given->isa(__PACKAGE__)) { } 0 3 3
elsif ($given) { } 3 0 3
257 0 ( $database, $attributes ) = @{ $given };
258 }
259 elsif ( ref $given eq "HASH" ) {
260 0 ( $database, $attributes ) = @{ $given }{qw/ database attributes /};
261 }
262 ▶ elsif ( blessed $given && $given->isa( __PACKAGE__ ) ) {
Condition33%1/3
builtin::blessed($given) and $given->isa(__PACKAGE__)
!ll&&!rl&&r
030
MC/DC0%0/2
builtin::blessed($given) and $given->isa(__PACKAGE__)
builtin::blessed($given) $given->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($given)B= $given->isa(__PACKAGE__)
ABresult
0X0
100
111
263 0 return $given;
264 }
265 elsif ( $given ) {
266 3 $database = $given;
267 }
268 else {
269 0 croak "Don't know what to do with @_";
270 }
271  
272 3 my $source = "dbi:SQLite:dbname=$database";
273  
274 3 return $class->new( source => $source, database => $database, attributes => $attributes );
275 }
276  
277 9 ▶sub information {
Subroutine information: called CC=2 SCAR=6.9
Pod undocumented
278 9 my $self = shift;
279 9 my @information = ( $self->source, $self->username, $self->password, $self->attributes );
280 9 ▶ return wantarray ? @information : \@information;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 4 9
281 }
282  
283 2 1;
284  
285 =head1 SYNOPSIS
286  
287 =head1 AUTHOR
288  
289 Robert Krimen, C<< <rkrimen at cpan.org> >>
290  
291 =head1 BUGS
292  
293 Please report any bugs or feature requests to C<bug-dbix-sqlite-deploy at rt.cpan.org>, or through
294 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-SQLite-Deploy>. I will be notified, and then you'll
295 automatically be notified of progress on your bug as I make changes.
296  
297  
298  
299  
300 =head1 SUPPORT
301  
302 You can find documentation for this module with the perldoc command.
303  
304 perldoc DBIx::SQLite::Deploy
305  
306  
307 You can also look for information at:
308  
309 =over 4
310  
311 =item * RT: CPAN's request tracker
312  
313 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-SQLite-Deploy>
314  
315 =item * AnnoCPAN: Annotated CPAN documentation
316  
317 L<http://annocpan.org/dist/DBIx-SQLite-Deploy>
318  
319 =item * CPAN Ratings
320  
321 L<http://cpanratings.perl.org/d/DBIx-SQLite-Deploy>
322  
323 =item * Search CPAN
324  
325 L<http://search.cpan.org/dist/DBIx-SQLite-Deploy/>
326  
327 =back
328  
329  
330 =head1 ACKNOWLEDGEMENTS
331  
332  
333 =head1 COPYRIGHT & LICENSE
334  
335 Copyright 2009 Robert Krimen, all rights reserved.
336  
337 This program is free software; you can redistribute it and/or modify it
338 under the same terms as Perl itself.
339  
340  
341 =cut
342  
343 2 1; # End of DBIx::SQLite::Deploy
↑ Summary Deploy/Carp.pm »