lib/DBIx/SchemaChecksum.pm

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