blib/lib/DBIO/MySQL/Diff/View.pm

filter: Statementstmt 100% 97 / 97 Branchbran 91.6% 33 / 36 Conditioncond 79.0% 34 / 43 MC/DCmcdc 65.2% 15 / 23 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 90.8% 199 / 219 CC 39 SCAR 26.2
CC
39
Cov
93%
diff
25.2
__ANON__
5.1
as_sql
5.1
CRAP 13.8
« blib/lib/DBIO/MySQL/Diff/Column.pm ↑ Summary blib/lib/DBIO/MySQL/NotifyTable.pm »
1 package DBIO::MySQL::Diff::View;
2 # ABSTRACT: Diff operations for MySQL/MariaDB views
3  
4 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 58 our $VERSION = '0.900007';
8  
9 58 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 58 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 58 ▶use DBIO::MySQL::Identifier qw(quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 58 ▶use DBIO::MySQL::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 58 __PACKAGE__->mk_diff_accessors(qw/view_name view_info replaced/);
16  
17  
18  
19  
20 2502 ▶sub is_view { return (($_[0] // {})->{kind} // '') eq 'view' }
Subroutine is_view: called CC=3 SCAR=11.0
Pod documented
Condition100%4/4
+($_[0] // {})->{"kind"} // ""
l!l
7001802
$_[0] // {}
l!l
2370132
MC/DC100%1/1
+($_[0] // {})->{"kind"} // ""
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
21  
22  
23 1442 ▶sub without_views {
Subroutine without_views: called CC=4 SCAR=13.9
Pod documented
24 1442 my ($tables, @sections) = @_;
25 1442 ▶ $tables //= {};
Condition50%1/2
$tables //= {}
l!l
14420
MC/DC0%0/1
$tables //= {}
$tables
Truth table50%1/2
A
A= $tables
Aresult
11
00
26 1442 my %view = map { $_ => 1 } grep { is_view($tables->{$_}) } keys %$tables;
27 1442 ▶ return ($tables, @sections) unless %view;
Branch: 1 branch
BranchTrueFalseTotal
unless %view 1405 37 1442
28 return (
29 30 { map { $_ => $tables->{$_} } grep { !$view{$_} } keys %$tables },
30 37 ▶ map { my $s = $_; ref $s eq 'HASH' ? { map { $_ => $s->{$_} } grep { !$view{$_} } keys %$s } : $s } @sections,
Branch: 1 branch
BranchTrueFalseTotal
ref $s eq 'HASH' ? : 97 0 97
31 );
32 }
33  
34  
35 338 ▶sub model_without_views {
Subroutine model_without_views: called CC=1 SCAR=0.0
Pod documented
36 338 my ($model) = @_;
37 my ($tables, @sections) = without_views(
38 338 $model->{tables}, map { $model->{$_} } qw(columns indexes foreign_keys));
39 338 return { %$model, tables => $tables,
40 columns => $sections[0], indexes => $sections[1], foreign_keys => $sections[2] };
41 }
42  
43 52 ▶sub _comparable {
Subroutine _comparable: called CC=2 SCAR=7.0
44 52 my ($def) = @_;
45 52 ▶ return undef unless defined $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def 0 52 52
46 52 $def =~ s/\s+/ /g;
47 52 $def =~ s/\A //;
48 52 $def =~ s/ \z//;
49 52 return $def;
50 }
51  
52  
53 172 ▶sub diff {
Subroutine diff: called CC=25 SCAR=32.3
Pod documented
54 172 my ($class, $source, $target) = @_;
55 172 ▶ $source //= {};
Condition50%1/2
$source //= {}
l!l
1720
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
56 172 ▶ $target //= {};
Condition50%1/2
$target //= {}
l!l
1720
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
57  
58 172 my %seen;
59 172 my (%drop, %create, %replaced);
60 172 for my $name (sort grep { !$seen{$_}++ } keys %$source, keys %$target) {
61 279 ▶ my $old = is_view($source->{$name}) ? $source->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
is_view($source->{$name}) ? : 44 235 279
62 279 ▶ my $new = is_view($target->{$name}) ? $target->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
is_view($target->{$name}) ? : 42 237 279
63 279 ▶ next unless $old || $new;
Branch: 1 branch
BranchTrueFalseTotal
unless $old or $new 219 60 279
Condition100%3/3
$old or $new
l!l&&r!l&&!r
4416219
MC/DC100%2/2
$old or $new
$old $new
Truth table100%3/3
A or B
A= $oldB= $new
ABresult
1X1
011
000
64  
65 60 ▶ if ($old && $new) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old and $new) 26 34 60
Condition100%3/3
$old and $new
!ll&&!rl&&r
161826
MC/DC100%2/2
$old and $new
$old $new
Truth table100%3/3
A and B
A= $oldB= $new
ABresult
0X0
100
111
66 26 my ($was, $will) = map { _comparable($_->{view_definition}) } $old, $new;
67 26 ▶ next if !defined $was || !defined $will || $was eq $will;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $was && defined $will or $was eq $will 14 12 26
Condition50%3/6
not defined $was && defined $will or $was eq $will
l!l&&r!l&&!r
01412
defined $was && defined $will
!ll&&!rl&&r
0026
MC/DC33%1/3
not defined $was && defined $will or $was eq $will
defined $was defined $will $was eq $will
Truth table50%2/4
not(A && B) or C
A= defined $wasB= defined $willC= $was eq $will
ABCresult
0XX1
10X1
1111
1100
68 }
69  
70 46 ▶ $drop{$name} = $old if $old;
Branch: 1 branch
BranchTrueFalseTotal
if $old 30 16 46
71 46 ▶ $create{$name} = $new if $new;
Branch: 1 branch
BranchTrueFalseTotal
if $new 28 18 46
72 46 ▶ $replaced{$name} = 1 if $old && $new;
Branch: 1 branch
BranchTrueFalseTotal
if $old and $new 12 34 46
Condition100%3/3
$old and $new
!ll&&!rl&&r
161812
MC/DC100%2/2
$old and $new
$old $new
Truth table100%3/3
A and B
A= $oldB= $new
ABresult
0X0
100
111
73 }
74  
75 # A view that reads a view being replaced is replaced with it (karr k113),
76 # transitively, although its own definition is the same: the server lets the
77 # view it reads be dropped and answers with 1356 until a view of that name
78 # is back, and if the new one lost a column the reader reads, the reader is
79 # broken without a word. Created again, the server checks it against the new
80 # definition and a reader that no longer fits fails in the apply. An
81 # unchanged view has the same reads in both models.
82 14 my %stay = map { $_ => $target->{$_} } grep {
83 172 ▶ is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
Condition100%6/6
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
!ll&&!rl&&r
2251214
is_view($source->{$_}) and is_view($target->{$_})
!ll&&!rl&&r
224126
MC/DC100%3/3
is_view($source->{$_}) && is_view($target->{$_}) && !$replaced{$_}
is_view($source->{$_}) is_view($target->{$_}) !$replaced{$_}
Truth table100%4/4
A and B && not(C)
A= is_view($source->{$_})B= is_view($target->{$_})C= !$replaced{$_}
ABCresult
0XX0
10X0
1100
1111
84 } keys %$target;
85 172 my %fold;
86 172 push @{ $fold{ lc $_ } }, $_ for keys %stay, keys %replaced;
87 18 ▶ my $resolve = sub { # as order_views reads a name: exact, else the one name of that spelling
Subroutine __ANON__: called CC=5 SCAR=16.3
88 18 my ($r) = @_;
89 18 ▶ return $r if exists $stay{$r} || exists $replaced{$r};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stay{$r} or exists $replaced{$r} 11 7 18
Condition100%3/3
exists $stay{$r} or exists $replaced{$r}
l!l&&r!l&&!r
1107
MC/DC100%2/2
exists $stay{$r} or exists $replaced{$r}
exists $stay{$r} exists $replaced{$r}
Truth table100%3/3
A or B
A= exists $stay{$r}B= exists $replaced{$r}
ABresult
1X1
011
000
90 7 ▶ return @{ $fold{ lc $r } // [] } == 1 ? $fold{ lc $r }[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$fold{lc $r} // [];} == 1 ? : 0 7 7
Condition50%1/2
$fold{lc $r} // []
l!l
07
MC/DC0%0/1
$fold{lc $r} // []
$fold{lc $r}
Truth table50%1/2
A
A= $fold{lc $r}
Aresult
11
00
91 172 };
92 172 my @grow = keys %replaced;
93 172 ▶ while (defined(my $changed = shift @grow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $changed = shift @grow)) 20 172 192
94 20 for my $name (sort keys %stay) {
95 18 ▶ next unless grep { ($resolve->($_) // '') eq $changed }
Condition100%2/2
&$resolve($_) // ""
l!l
117
MC/DC100%1/1
&$resolve($_) // ""
&$resolve($_)
Truth table100%2/2
A
A= &$resolve($_)
Aresult
11
00
96 17 ▶ DBIO::MySQL::DDL::view_reads($stay{$name}{view_definition});
Branch: 1 branch
BranchTrueFalseTotal
unless grep {(&$resolve($_) // "") eq $changed;} DBIO::MySQL::DDL::view_reads($stay{$name}{"view_definition"}) 9 8 17
97 8 $drop{$name} = $source->{$name};
98 8 $create{$name} = $target->{$name};
99 8 $replaced{$name} = 1;
100 8 delete $stay{$name};
101 8 push @grow, $name;
102 }
103 }
104  
105 # the order is worked out for the whole set before an op is made: a cycle
106 # throws and leaves nothing half done
107 172 my @drop_order = reverse DBIO::MySQL::DDL::order_views({ map { $_ => $drop{$_}{view_definition} } keys %drop });
108 171 my @create_order = DBIO::MySQL::DDL::order_views({ map { $_ => $create{$_}{view_definition} } keys %create });
109  
110 return (
111 (map { $class->new(
112 action => 'drop',
113 view_name => $_,
114 view_info => $drop{$_},
115 34 ▶ replaced => ($replaced{$_} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$replaced{$_} ? : 20 14 34
116 ) } @drop_order),
117 170 (map { $class->new(
118 action => 'create',
119 view_name => $_,
120 32 view_info => $create{$_},
121 ) } @create_order),
122 );
123 }
124  
125  
126 67 ▶sub as_sql {
Subroutine as_sql: called CC=5 SCAR=16.2
Pod documented
127 67 my ($self) = @_;
128  
129 67 ▶ return sprintf 'DROP VIEW %s;', quote_ident($self->view_name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 35 32 67
130 if $self->action eq 'drop';
131  
132 32 ▶ my $def = ($self->view_info // {})->{view_definition};
Condition50%1/2
$self->view_info // {}
l!l
320
MC/DC0%0/1
$self->view_info // {}
$self->view_info
Truth table50%1/2
A
A= $self->view_info
Aresult
11
00
133 32 ▶ Carp::croak(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 1 31 32
Condition66%2/3
defined $def and length $def
!ll&&!rl&&r
1031
MC/DC50%1/2
defined $def and length $def
defined $def length $def
Truth table66%2/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
134 'Cannot create view %s: the target model holds no view definition for it, '
135 . 'and a view cannot be written from its columns', $self->view_name)
136 unless defined $def && length $def;
137  
138 31 return sprintf 'CREATE VIEW %s AS %s;', quote_ident($self->view_name), $def;
139 }
140  
141  
142 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
143 2 my ($self) = @_;
144 2 return sprintf '%s view: %s', $self->summary_prefix, $self->view_name;
145 }
146  
147 58 1;
148  
149 __END__
« blib/lib/DBIO/MySQL/Diff/Column.pm ↑ Summary blib/lib/DBIO/MySQL/NotifyTable.pm »