lib/DBIx/SchemaChecksum.pm

filter: Statementstmt 86.4% 134 / 155 Branchbran 64.5% 40 / 62 Conditioncond 50.0% 5 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 20 / 20 Podpod 66.6% 2 / 3 totaltotal 79.3% 203 / 256 CC 41 SCAR 22.7
CC
41
Cov
79%
_build_update_path
21.7
_build_schemadump
9.4
_build_schemadump_column
8.4
CRAP 9.7
« lib/DBIx/SchemaChecksum/Driver/Pg.pm ↑ Summary lib/DBIx/SchemaChecksum/App/ShowUpdatePath.pm »
1 package DBIx::SchemaChecksum;
2  
3 # ABSTRACT: Manage your datebase schema via checksums
4 17 our $VERSION = '1.104'; # VERSION
5  
6 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 17 has 'dbh' => (
18 is => 'ro',
19 required => 1
20 );
21  
22 17 has 'catalog' => (
23 is => 'ro',
24 isa => 'Str',
25 default => '%',
26 documentation => q[might be required by some DBI drivers]
27 );
28  
29 has 'schemata' => (
30 is => 'ro',
31 isa => 'ArrayRef[Str]',
32 15 ▶ default => sub { ['%'] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 17 documentation => q[List of schematas to include in checksum]
34 );
35  
36 17 has 'sqlsnippetdir' => (
37 isa => 'Str',
38 is => 'ro',
39 documentation => q[Directory containing sql update files],
40 );
41  
42 has 'driveropts' => (
43 isa => 'HashRef',
44 is => 'ro',
45 15 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
46 17 documentation => q[Driver specific options],
47 );
48  
49 17 has 'verbose' => (
50 is => 'rw',
51 isa => 'Bool',
52 default => 0
53 );
54  
55 17 has '_update_path' => (
56 is => 'rw',
57 isa => 'Maybe[HashRef]',
58 lazy_build => 1,
59 builder => '_build_update_path',
60 );
61  
62 17 has '_schemadump' => (
63 isa=>'Str',
64 is=>'rw',
65 lazy_build=>1,
66 clearer=>'reset_checksum',
67 builder => '_build_schemadump',
68 );
69  
70 15 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
71 15 my ($self) = @_;
72  
73 # Apply driver role to instance
74 15 my $driver = $self->dbh->{Driver}{Name};
75 15 my $class = __PACKAGE__.'::Driver::'.$driver;
76 15 ▶ if (Class::Load::try_load_class($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::Load::try_load_class($class)) 15 0 15
77 15 $class->meta->apply($self);
78 }
79 15 return $self;
80 }
81  
82  
83  
84 10 ▶sub checksum {
Subroutine checksum: called CC=1 SCAR=0.0
Pod documented
85 10 my $self = shift;
86 10 return Digest::SHA1::sha1_hex($self->_schemadump);
87 }
88  
89  
90 7 ▶sub _build_schemadump {
Subroutine _build_schemadump: called CC=8 SCAR=22.4
91 7 my $self = shift;
92  
93 7 my %relevants = ();
94  
95 7 foreach my $schema ( @{ $self->schemata } ) {
96 7 my $schema_relevants = $self->_build_schemadump_schema($schema);
97 7 ▶ while (my ($type,$type_value) = each %{$schema_relevants}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$schema_relevants;}) 7 7 14
98 7 my $ref = ref($type_value);
99 7 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 7 7
elsif ($ref eq 'HASH') { } 7 0 7
100 0 ▶ $relevants{$type} ||= [];
Condition0%0/2
$relevants{$type} ||= []
l!l
00
MC/DC0%0/1
$relevants{$type} ||= []
$relevants{$type}
Truth table0%0/2
A
A= $relevants{$type}
Aresult
11
00
101 0 foreach my $value (@{$type_value}) {
102 0 push(@{$relevants{$type}}, $value);
103 }
104 }
105 elsif ($ref eq 'HASH') {
106 7 ▶ while (my ($key,$value) = each %{$type_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$type_value;}) 7 7 14
107 7 $relevants{$type}{$key} = $value;
108 }
109 }
110 }
111 }
112  
113 7 my $dumper = Data::Dumper->new( [ \%relevants ] );
114 7 $dumper->Sortkeys(1);
115 7 $dumper->Indent(1);
116 7 my $dump = $dumper->Dump;
117  
118 7 return $dump;
119 }
120  
121  
122 7 ▶sub _build_schemadump_schema {
Subroutine _build_schemadump_schema: called CC=1 SCAR=0.0
123 7 my ($self,$schema) = @_;
124  
125 7 my %relevants = ();
126 7 $relevants{tables} = $self->_build_schemadump_tables($schema);
127  
128 7 return \%relevants;
129 }
130  
131  
132 7 ▶sub _build_schemadump_tables {
Subroutine _build_schemadump_tables: called CC=4 SCAR=13.9
133 7 my ($self,$schema) = @_;
134  
135 7 my $dbh = $self->dbh;
136  
137 7 my %relevants;
138 7 foreach my $table ( $dbh->tables( $self->catalog, $schema, '%' ) ) {
139 next
140 28 ▶ unless $table =~ m/^"?(?<schema>[^"]+)"?\."?(?<table>[^"]+)"?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $table =~ /^"?(?<schema>[^"]+)"?\."?(?<table>[^"]+)"?$/ 0 28 28
141 28 my $this_schema = $+{schema};
142 28 my $table = $+{table};
143  
144 28 my $table_data = $self->_build_schemadump_table($this_schema,$table);
145 next
146 28 ▶ unless $table_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $table_data 21 7 28
147 7 $relevants{$this_schema.'.'.$table} = $table_data;
148 }
149  
150 7 return \%relevants;
151 }
152  
153  
154 7 ▶sub _build_schemadump_table {
Subroutine _build_schemadump_table: called CC=7 SCAR=20.6
155 7 my ($self,$schema,$table) = @_;
156  
157 7 my %relevants = ();
158  
159 7 my $dbh = $self->dbh;
160  
161 # Primary key
162 7 my @primary_keys = $dbh->primary_key( $self->catalog, $schema, $table );
163 $relevants{primary_keys} = \@primary_keys
164 7 ▶ if scalar @primary_keys;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @primary_keys 7 0 7
165  
166 # Columns
167 7 my $sth_col = $dbh->column_info( $self->catalog, $schema, $table, '%' );
168 7 my $column_info = $sth_col->fetchall_hashref('COLUMN_NAME');
169 7 ▶ while ( my ( $column, $data ) = each %$column_info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$column_info) 21 7 28
170 21 my $column_data = $self->_build_schemadump_column($schema,$table,$column,$data);
171 21 ▶ $relevants{columns}->{$column} = $column_data
Branch: 1 branch
BranchTrueFalseTotal
if $column_data 21 0 21
172 if $column_data;
173 }
174  
175 # Foreign keys (only use a few selected meta-fields)
176 7 my $sth_fk = $dbh->foreign_key_info( undef, undef, undef, $self->catalog, $schema, $table );
177 7 ▶ if ($sth_fk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth_fk) 7 0 7
178 7 my $fk={};
179 7 ▶ while (my $data = $sth_fk->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $sth_fk->fetchrow_hashref) 0 7 7
180 0 my %useful = map { $_ => $data->{$_}} qw(UK_COLUMN_NAME UK_TABLE_NAME UK_TABLE_SCHEM);
181 0 $fk->{$data->{FK_COLUMN_NAME}} = \%useful;
182 }
183 7 ▶ $relevants{foreign_keys} = $fk if keys %$fk;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$fk 0 7 7
184 }
185  
186 7 return \%relevants;
187 }
188  
189  
190 21 ▶sub _build_schemadump_column {
Subroutine _build_schemadump_column: called CC=4 SCAR=21.3
191 21 my ($self,$schema,$table,$column,$data) = @_;
192  
193 21 my $relevants = { map { $_ => $data->{$_} } qw(COLUMN_NAME COLUMN_SIZE NULLABLE TYPE_NAME COLUMN_DEF) };
194  
195 # some cleanup
196 21 ▶ if (my $default = $relevants->{COLUMN_DEF}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $default = $relevants->{"COLUMN_DEF"}) 0 21 21
197 0 ▶ if ( $default =~ /nextval/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default =~ /nextval/) 0 0 0
198 0 $default =~ m{'([\w\.\-_]+)'};
199 0 ▶ if ($1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 0 0 0
200 0 my $new = $1;
201 0 $new =~ s/^\w+\.//;
202 0 $default = 'nextval:' . $new;
203 }
204 }
205 0 $default=~s/["'\(\)\[\]\{\}]//g;
206 0 $relevants->{COLUMN_DEF}=$default;
207 }
208  
209 21 $relevants->{TYPE_NAME} =~ s/^(?:.+\.)?(.+)$/$1/g;
210  
211 21 return $relevants;
212 }
213  
214  
215 9 ▶sub _build_update_path {
Subroutine _build_update_path: called CC=16 SCAR=30.8
216 9 my $self = shift;
217 9 my $dir = $self->sqlsnippetdir;
218 9 ▶ croak("Please specify sqlsnippetdir") unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 9 9
219 9 ▶ croak("Cannot find sqlsnippetdir: $dir") unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 7 9
220  
221 7 ▶ say "Checking directory $dir for checksum_files" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 7 7
222  
223 7 my %update_info;
224 7 my @files = File::Find::Rule->file->name('*.sql')->in($dir);
225  
226 7 foreach my $file ( sort @files ) {
227 18 my ( $pre, $post ) = $self->get_checksums_from_snippet($file);
228  
229 18 ▶ if ( !$pre && !$post ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pre or $post) 0 18 18
Condition33%1/3
$pre or $post
l!l&&r!l&&!r
1800
MC/DC0%0/2
$pre or $post
$pre $post
Truth table33%1/3
A or B
A= $preB= $post
ABresult
1X1
011
000
230 0 ▶ say "skipping $file (has no checksums)" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
231 0 next;
232 }
233  
234 18 ▶ if ( $pre eq $post ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre eq $post) 3 15 18
235 3 ▶ if ( $update_info{$pre} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update_info{$pre}) { } 3 0 3
236 3 my @new = ('SAME_CHECKSUM');
237 3 foreach my $item ( @{ $update_info{$pre} } ) {
238 6 ▶ push( @new, $item ) unless $item eq 'SAME_CHECKSUM';
Branch: 1 branch
BranchTrueFalseTotal
unless $item eq "SAME_CHECKSUM" 6 0 6
239 }
240 3 $update_info{$pre} = \@new;
241 }
242 else {
243 0 $update_info{$pre} = ['SAME_CHECKSUM'];
244 }
245 }
246  
247 18 ▶ if ( $update_info{$pre}
Branch: 1 branch
BranchTrueFalseTotal
if ($update_info{$pre} and $update_info{$pre}[0] eq 'SAME_CHECKSUM') { } 3 15 18
Condition66%2/3
$update_info{$pre} and $update_info{$pre}[0] eq "SAME_CHECKSUM"
!ll&&!rl&&r
1503
MC/DC50%1/2
$update_info{$pre} and $update_info{$pre}[0] eq "SAME_CHECKSUM"
$update_info{$pre} $update_info{$pre}[0] eq "SAME_CHECKSUM"
Truth table66%2/3
A and B
A= $update_info{$pre}B= $update_info{$pre}[0] eq "SAME_CHECKSUM"
ABresult
0X0
100
111
248 && $update_info{$pre}->[0] eq 'SAME_CHECKSUM' )
249 {
250 3 ▶ if ( $post eq $pre ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post eq $pre) { } 3 0 3
251 3 splice( @{ $update_info{$pre} },
252 1, 0, Path::Class::File->new($file), $post );
253 }
254 else {
255 0 push( @{ $update_info{$pre} },
256 Path::Class::File->new($file), $post );
257 }
258 }
259 else {
260 15 $update_info{$pre} = [ Path::Class::File->new($file), $post ];
261 }
262 }
263  
264 7 ▶ return $self->_update_path( \%update_info ) if %update_info;
Branch: 1 branch
BranchTrueFalseTotal
if %update_info 6 1 7
265 1 return;
266 }
267  
268  
269 22 ▶sub get_checksums_from_snippet {
Subroutine get_checksums_from_snippet: called CC=6 SCAR=17.9
Pod documented
270 22 my ($self, $filename) = @_;
271 22 ▶ die "need a filename" unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 1 21 22
272  
273 21 my %checksums;
274  
275 21 ▶ open( my $fh, "<", $filename ) || croak "Cannot read $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or croak("Cannot read ${filename}: $!") 20 1 21
276 20 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 114 20 134
277 114 ▶ if (m/^--\s+(pre|post)SHA1sum:?\s+([0-9A-Fa-f]{40,})\s+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^--\s+(pre|post)SHA1sum:?\s+([0-9A-Fa-f]{40,})\s+$/) 39 75 114
278 39 $checksums{$1} = $2;
279 }
280 }
281 20 close $fh;
282 20 ▶ return map { $checksums{$_} || '' } qw(pre post);
Condition100%2/2
$checksums{$_} || ''
l!l
391
MC/DC100%1/1
$checksums{$_} || ''
$checksums{$_}
Truth table100%2/2
A
A= $checksums{$_}
Aresult
11
00
283 }
284  
285 17 __PACKAGE__->meta->make_immutable();
286  
287 __END__
« lib/DBIx/SchemaChecksum/Driver/Pg.pm ↑ Summary lib/DBIx/SchemaChecksum/App/ShowUpdatePath.pm »