lib/SQL/Admin/Catalog/Compare.pm

filter: Statementstmt 15.2% 14 / 92 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 21.4% 3 / 14 Podpod 0.0% 0 / 5 totaltotal 9.3% 17 / 181 CC 37 SCAR 35.7
CC
37
Cov
9%
_diff_sequence
56.0
_diff_table
56.0
_diff_column
56.0
CRAP 35.6
↑ Summary lib/SQL/Admin/Driver/Pg/DBI.pm »
1  
2 package SQL::Admin::Catalog::Compare;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = v0.5.0;
8  
9 ######################################################################
10  
11 1 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ######################################################################
14  
15 1 our $HIDE_DROP_TABLE = 0;
16 1 our $HIDE_DROP_COLUMN = 0;
17 1 our $HIDE_DROP_SEQUENCE = 0;
18  
19 ######################################################################
20 ######################################################################
21 0 ▶sub new { # ;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
22 0 my ($class) = @_;
23  
24 0 ▶ bless [], ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
25 }
26  
27  
28 ######################################################################
29 ######################################################################
30 0 ▶sub decompose { # ;
Subroutine decompose: not called CC=4 SCAR=30.0
Pod undocumented
31 0 my ($self, $decomposer) = @_;
32 0 my @retval;
33  
34 0 for my $command (@$self) {
35 0 ▶ if ('HASH' eq ref $command) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $command) { } 0 0 0
36 0 push @retval, $command;
37 } else {
38 0 my ($name, $catalog, $data) = @$command;
39  
40 0 ▶ push @retval, $decomposer->$name ($catalog, $data)
Branch: 1 branch
BranchTrueFalseTotal
if $decomposer->can($name) 0 0 0
41 if $decomposer->can ($name);
42 }
43 }
44  
45 0 @retval;
46 }
47  
48  
49 ######################################################################
50 ######################################################################
51 0 ▶sub _diff_schema { # ;
Subroutine _diff_schema: not called CC=2 SCAR=17.9
52 0 my ($self, $src, $dst) = @_;
53 0 my ($src_map, $dst_map) = map $_->list ('schema'), $src, $dst;
54  
55 push @$self, [ 'create_schema', $dst, $_ ]
56 0 for map $dst_map->{$_}, grep ! exists $src_map->{$_}, keys %$dst_map;
57 }
58  
59  
60 ######################################################################
61 ######################################################################
62 0 ▶sub _diff_sequence { # ;
Subroutine _diff_sequence: not called CC=7 SCAR=40.3
63 0 my ($self, $srccat, $dstcat) = @_;
64  
65 0 ▶ my $sseq = $srccat->list ('sequences') || {};
Condition0%0/2
$srccat->list('sequences') || {}
l!l
00
MC/DC0%0/1
$srccat->list('sequences') || {}
$srccat->list('sequences')
Truth table0%0/2
A
A= $srccat->list('sequences')
Aresult
11
00
66 0 ▶ my $dseq = $dstcat->list ('sequences') || {};
Condition0%0/2
$dstcat->list('sequences') || {}
l!l
00
MC/DC0%0/1
$dstcat->list('sequences') || {}
$dstcat->list('sequences')
Truth table0%0/2
A
A= $dstcat->list('sequences')
Aresult
11
00
67 0 ▶ my $dsc = $dstcat->{_schema} || {};
Condition0%0/2
$dstcat->{'_schema'} || {}
l!l
00
MC/DC0%0/1
$dstcat->{'_schema'} || {}
$dstcat->{'_schema'}
Truth table0%0/2
A
A= $dstcat->{'_schema'}
Aresult
11
00
68  
69 # drop sequences
70 ##################################################################
71 0 ▶ unless ($HIDE_DROP_SEQUENCE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SQL::Admin::Catalog::Compare::HIDE_DROP_SEQUENCE) 0 0 0
72 push @$self, [ drop_sequence => $srccat, $_ ]
73 0 for grep exists $dsc->{$_->{schema}},
74 map $sseq->{$_},
75 grep ! exists $dseq->{$_},
76 keys %$sseq;
77 }
78  
79 # create sequences
80 ##################################################################
81 push @$self, { create_sequence => $dstcat, $dseq->{$_} }
82 0 for grep ! exists $sseq->{$_}, # -- not in src
83 keys %$dseq; # ++ all dst sequences
84  
85 ##################################################################
86  
87 0 1;
88 }
89  
90  
91 ######################################################################
92 ######################################################################
93 0 ▶sub _diff_table { # ;
Subroutine _diff_table: not called CC=7 SCAR=40.3
94 0 my ($self, $srccat, $dstcat) = @_;
95  
96 0 ▶ my $stab = $srccat->list ('table') || {};
Condition0%0/2
$srccat->list('table') || {}
l!l
00
MC/DC0%0/1
$srccat->list('table') || {}
$srccat->list('table')
Truth table0%0/2
A
A= $srccat->list('table')
Aresult
11
00
97 0 ▶ my $dtab = $dstcat->list ('table') || {};
Condition0%0/2
$dstcat->list('table') || {}
l!l
00
MC/DC0%0/1
$dstcat->list('table') || {}
$dstcat->list('table')
Truth table0%0/2
A
A= $dstcat->list('table')
Aresult
11
00
98 0 ▶ my $dsc = $dstcat->{_schema} || {};
Condition0%0/2
$dstcat->{'_schema'} || {}
l!l
00
MC/DC0%0/1
$dstcat->{'_schema'} || {}
$dstcat->{'_schema'}
Truth table0%0/2
A
A= $dstcat->{'_schema'}
Aresult
11
00
99  
100 # drop tables
101 ##################################################################
102 0 ▶ unless ($HIDE_DROP_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SQL::Admin::Catalog::Compare::HIDE_DROP_TABLE) 0 0 0
103 push @$self, [ 'drop_table', $srccat, $stab->{$_} ]
104 0 for grep exists $dsc->{$_->{schema}},
105 map $stab->{$_},
106 grep ! exists $dtab->{$_},
107 keys %$stab;
108 }
109  
110 # create tables
111 ##################################################################
112 push @$self, [ 'create_table', $dstcat, $dtab->{$_} ]
113 0 for grep ! exists $stab->{$_}, # -- not in src
114 keys %$dtab; # ++ all dst tables
115  
116 ##################################################################
117  
118 0 1;
119 }
120  
121  
122 ######################################################################
123 ######################################################################
124 0 ▶sub _diff_column { # ;
Subroutine _diff_column: not called CC=7 SCAR=40.3
125 0 my ($self, $srccat, $dstcat) = @_;
126 0 my $scol = $srccat->list ('column');
127 0 my $dcol = $dstcat->list ('column');
128 0 my $dtab = $dstcat->list ('table');
129 0 my $stab = $srccat->list ('table');
130  
131 # drop columns
132 ##################################################################
133 0 ▶ unless ($HIDE_DROP_COLUMN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SQL::Admin::Catalog::Compare::HIDE_DROP_COLUMN) 0 0 0
134 push @$self, [ 'drop_column', $srccat, $scol->{$_} ]
135 0 for grep $stab->{$_->{table}{fullname}},
136 grep $dtab->{$_->{table}{fullname}},
137 map $scol->{$_},
138 grep ! $dcol->{$_},
139 keys %$scol;
140 }
141  
142 # add columns
143 ##################################################################
144 0 ▶ while (my ($key, $value) = each %$dcol) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dcol) 0 0 0
145 0 ▶ next if exists $scol->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $scol->{$key} 0 0 0
146 0 ▶ next unless exists $stab->{ $value->table->fullname };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$stab{$value->table->fullname} 0 0 0
147 0 ▶ next unless exists $dtab->{ $value->table->fullname };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$dtab{$value->table->fullname} 0 0 0
148  
149 0 push @$self, [ 'add_column', $dstcat, $value ];
150 }
151 }
152  
153  
154 ######################################################################
155 ######################################################################
156 0 ▶sub _diff_column_not_null { # ;
Subroutine _diff_column_not_null: not called CC=6 SCAR=37.4
157 0 my ($self, $srccat, $dstcat) = @_;
158 0 my $scol = $srccat->list ('column');
159 0 my $dcol = $dstcat->list ('column');
160 0 my $dtab = $dstcat->list ('table');
161 0 my $stab = $srccat->list ('table');
162  
163 ##################################################################
164 0 ▶ while (my ($key, $value) = each %$dcol) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dcol) 0 0 0
165 # alter only existing columns
166 0 ▶ next unless exists $scol->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $scol->{$key} 0 0 0
167  
168 0 ▶ next if ($value->not_null || 0) == ($scol->{$key}->not_null || 0);
Branch: 1 branch
BranchTrueFalseTotal
if ($value->not_null || 0) == ($scol->{$key}->not_null || 0) 0 0 0
Condition0%0/4
$value->not_null || 0
l!l
00
$scol->{$key}->not_null || 0
l!l
00
MC/DC0%0/1
$value->not_null || 0
$value->not_null
MC/DC0%0/1
$scol->{$key}->not_null || 0
$scol->{$key}->not_null
Truth table0%0/2
A
A= $value->not_null
Aresult
11
00
Truth table0%0/2
A
A= $scol->{$key}->not_null
Aresult
11
00
169  
170 0 push @$self, +{ alter_table => {
171 table_name => { name => $value->table->name, schema => $value->table->schema },
172 alter_table_actions => [ { alter_column => {
173 column_name => $value->name,
174 not_null => $value->not_null,
175 } } ] } };
176 }
177 }
178  
179  
180 ######################################################################
181 ######################################################################
182 0 ▶sub _diff_column_default { # ;
Subroutine _diff_column_default: not called CC=4 SCAR=30.0
183 0 my ($self, $srccat, $dstcat) = @_;
184 0 my $scol = $srccat->list ('column');
185 0 my $dcol = $dstcat->list ('column');
186 0 my $dtab = $dstcat->list ('table');
187 0 my $stab = $srccat->list ('table');
188  
189 ##################################################################
190 0 ▶ while (my ($key, $value) = each %$dcol) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dcol) 0 0 0
191 # alter only existing columns
192 0 ▶ next unless exists $scol->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $scol->{$key} 0 0 0
193  
194 0 ▶ next if Data::Compare::Compare ($value->default, $scol->{$key}->default);
Branch: 1 branch
BranchTrueFalseTotal
if Data::Compare::Compare($value->default, $scol->{$key}->default) 0 0 0
195  
196 0 push @$self, +{ alter_table => {
197 table_name => { name => $value->table->name, schema => $value->table->schema },
198 alter_table_actions => [ { alter_column => {
199 column_name => $value->name,
200 default_clause=> $value->default,
201 } } ] } };
202 }
203 }
204  
205  
206 ######################################################################
207 ######################################################################
208 0 ▶sub compare { # ;
Subroutine compare: not called CC=2 SCAR=17.9
Pod undocumented
209 0 my ($self, $src, $dest) = @_;
210 0 ▶ $self = __PACKAGE__->new unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
211  
212 0 @$self = ();
213  
214 0 $self->_diff_schema ($src, $dest);
215 0 $self->_diff_table ($src, $dest);
216 # $self->_diff_sequence ($src, $dest);
217 # $self->_diff_index ($src, $dest);
218 # # $self->_diff_view ($src, $dest);
219 # # $self->_diff_trigger ($src, $dest);
220 # # $self->_diff_function ($src, $dest);
221 0 $self->_diff_column ($src, $dest);
222 # {
223 0 $self->_diff_column_not_null ($src, $dest);
224 0 $self->_diff_column_default ($src, $dest);
225 # $self->_diff_constraints ($src, $dest);
226 # }
227  
228 0 $self;
229 }
230  
231  
232 ######################################################################
233 ######################################################################
234 0 ▶sub is_difference { # ;
Subroutine is_difference: not called CC=1 SCAR=6.9
Pod undocumented
235 0 my $self= shift;
236  
237 0 return @$self > 0;
238 }
239  
240  
241 ######################################################################
242 ######################################################################
243 0 ▶sub save { # ;
Subroutine save: not called CC=5 SCAR=34.0
Pod undocumented
244 0 my ($self, $driver, @params) = @_;
245  
246 0 ▶ $driver = SQL::Admin->get_driver ($driver, @{ shift @params || [] } )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $driver 0 0 0
Condition0%0/2
shift @params || []
l!l
00
MC/DC0%0/1
shift @params || []
shift @params
Truth table0%0/2
A
A= shift @params
Aresult
11
00
247 unless ref $driver;
248  
249 0 my @list = $self->decompose ($driver->decomposer);
250  
251 ##################################################################
252  
253 0 my $fh = \*STDOUT;
254 0 ▶ if ($driver->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver->{"file"}) 0 0 0
255 open $fh, '>', $driver->{file}
256 0 ▶ or die "Unable write to $driver->{file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $driver->{"file"} or die qq[Unable write to $driver->{"file"}: $!\n] 0 0 0
257 }
258  
259 0 print $fh $driver->producer->produce (@list);
260 }
261  
262  
263 ######################################################################
264 ######################################################################
265  
266 package SQL::Admin::Catalog::Compare;
267  
268 1 1;
269  
270 __END__
↑ Summary lib/SQL/Admin/Driver/Pg/DBI.pm »