blib/lib/DBIx/Schema/UpToDate.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 86.3% 19 / 22 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 19 / 19 Podpod 100% 12 / 12 totaltotal 96.9% 127 / 131 CC 14 SCAR 8.5
CC
14
Cov
96%
current_version
4.0
up_to_date
4.0
new
3.2
CRAP 2.3
↑ Summary
1 # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2 #
3 # This file is part of DBIx-Schema-UpToDate
4 #
5 # This software is copyright (c) 2011 by Randy Stauner.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package DBIx::Schema::UpToDate;
14 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 $DBIx::Schema::UpToDate::VERSION = '1.001';
16 }
17 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 $DBIx::Schema::UpToDate::AUTHORITY = 'cpan:RWSTAUNER';
19 }
20 # ABSTRACT: Helps keep a database schema up to date
21  
22 3 ▶use Carp qw(croak carp); # core
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.8
Pod documented
26 1 my $class = shift;
27 my $self = {
28 auto_update => 1,
29 sql_limit => 1,
30 transactions => 1,
31 0 ▶ @_ == 1 ? %{$_[0]} : @_
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
32 };
33 1 bless $self, $class;
34  
35 # make sure the database schema is current
36 $self->up_to_date()
37 1 ▶ if $self->{auto_update};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto_update"} 0 1 1
38  
39 1 return $self;
40 }
41  
42  
43 3 foreach my $action ( qw(begin_work commit) ){
44 3 ▶ no strict 'refs'; ## no critic (NoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
45 24 ▶ *$action = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
46 24 my ($self) = @_;
47 24 ▶ if( $self->{transactions} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transactions"}) 22 2 24
48 22 my $dbh = $self->dbh;
49 22 ▶ $dbh->$action()
Branch: 1 branch
BranchTrueFalseTotal
$dbh->$action or croak($dbh->errstr) 20 2 22
50 or croak $dbh->errstr;
51 }
52 }
53 6 }
54  
55  
56  
57 81 ▶sub dbh {
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
58 81 my ($self) = @_;
59 81 return $self->{dbh};
60 }
61  
62  
63 9 ▶sub current_version {
Subroutine current_version: called CC=4 SCAR=13.9
Pod documented
64 9 my ($self) = @_;
65 9 my $dbh = $self->dbh;
66 9 my $table = $self->version_table_name;
67 9 my $version;
68  
69 9 my $tables = $dbh->table_info('%', '%', $table, 'TABLE')
70 ->fetchall_arrayref;
71  
72 # if table exists query it for current database version
73 9 ▶ if( @$tables ){
Branch: 1 branch
BranchTrueFalseTotal
if (@$tables) 5 4 9
74 5 my $qtable = $self->quoted_table_name;
75 5 my $field = $dbh->quote_identifier('version');
76  
77 my $v = $dbh->selectcol_arrayref(
78 "SELECT $field from $qtable ORDER BY $field DESC"
79 5 ▶ . ($self->{sql_limit} ? ' LIMIT 1' : '')
Branch: 1 branch
BranchTrueFalseTotal
$self->{'sql_limit'} ? : 4 1 5
80 )->[0];
81 5 ▶ $version = $v
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 5 0 5
82 if defined $v;
83 }
84  
85 9 return $version;
86 }
87  
88  
89 3 ▶sub initialize_version_table {
Subroutine initialize_version_table: called CC=2 SCAR=6.9
Pod documented
90 3 my ($self) = @_;
91 3 my $dbh = $self->dbh;
92  
93 3 my ($version, $updated) = $self->quote_identifiers(qw(version updated));
94  
95 3 $self->begin_work();
96  
97 3 ▶ $dbh->do('CREATE TABLE ' . $self->quoted_table_name .
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do('CREATE TABLE ' . $self->quoted_table_name . " ($version integer, $updated timestamp)") or croak($dbh->errstr) 2 1 3
98 " ($version integer, $updated timestamp)"
99 )
100 or croak $dbh->errstr;
101  
102 2 $self->set_version(0);
103  
104 2 $self->commit();
105 }
106  
107  
108 5 ▶sub latest_version {
Subroutine latest_version: called CC=1 SCAR=0.0
Pod documented
109 5 my ($self) = @_;
110 5 return scalar @{ $self->updates };
111 }
112  
113  
114 20 ▶sub quoted_table_name {
Subroutine quoted_table_name: called CC=1 SCAR=0.0
Pod documented
115 20 my ($self) = @_;
116 20 return $self->dbh->quote_identifier($self->version_table_name);
117 }
118  
119  
120 15 ▶sub quote_identifiers {
Subroutine quote_identifiers: called CC=1 SCAR=0.0
Pod documented
121 15 my ($self, @names) = @_;
122 15 my $dbh = $self->dbh;
123 15 return map { $dbh->quote_identifier($_) } @names;
124 }
125  
126  
127 12 ▶sub set_version {
Subroutine set_version: called CC=2 SCAR=6.9
Pod documented
128 12 my ($self, $version) = @_;
129 12 my $dbh = $self->dbh;
130  
131 12 ▶ $dbh->do('INSERT INTO ' . $self->quoted_table_name .
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do('INSERT INTO ' . $self->quoted_table_name . ' (' . join(", ", $self->quote_identifiers("version", "updated")) . ') VALUES(?, ?)', {}, $version, time) or croak($dbh->errstr) 11 1 12
132 ' (' .
133 join(', ', $self->quote_identifiers(qw(version updated)))
134 . ') VALUES(?, ?)',
135 {}, $version, time()
136 )
137 or croak $dbh->errstr;
138 }
139  
140  
141 15 ▶sub updates {
Subroutine updates: called CC=2 SCAR=6.9
Pod documented
142 15 my ($self) = @_;
143 return $self->{updates} ||= [
144 15 ▶ ];
Condition100%2/2
$self->{"updates"} ||= []
l!l
141
MC/DC100%1/1
$self->{"updates"} ||= []
$self->{"updates"}
Truth table100%2/2
A
A= $self->{"updates"}
Aresult
11
00
145 }
146  
147  
148 10 ▶sub update_to_version {
Subroutine update_to_version: called CC=1 SCAR=0.0
Pod documented
149 10 my ($self, $version) = @_;
150  
151 10 $self->begin_work();
152  
153 # execute updates to bring database to $version
154 9 $self->updates->[$version - 1]->($self);
155  
156 # save the version now in case we get interrupted before the next commit
157 9 $self->set_version($version);
158  
159 9 $self->commit();
160 }
161  
162  
163 4 ▶sub up_to_date {
Subroutine up_to_date: called CC=4 SCAR=13.9
Pod documented
164 4 my ($self) = @_;
165  
166 4 my $current = $self->current_version;
167 4 ▶ if( !defined($current) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $current) 2 2 4
168 2 $self->initialize_version_table;
169 2 $current = $self->current_version;
170 2 ▶ die("Unable to initialize version table\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 1 1 2
171 if !defined($current);
172 }
173  
174 3 my $latest = $self->latest_version;
175  
176 # execute each update required to go from current to latest version
177 # (starting with next version, obviously (don't redo current))
178 $self->update_to_version($_)
179 3 foreach ($current + 1) .. $latest;
180 }
181  
182  
183 29 ▶sub version_table_name {
Subroutine version_table_name: called CC=1 SCAR=0.0
Pod documented
184 29 'schema_version'
185 }
186  
187 3 1;
188  
189  
190 __END__
↑ Summary