PostgreSQL/Diff/View.pm

filter: Statementstmt 100% 145 / 145 Branchbran 100% 38 / 38 Conditioncond 75.7% 53 / 70 MC/DCmcdc 56.7% 21 / 37 Subroutinesub 100% 25 / 25 Podpod 100% 8 / 8 totaltotal 89.7% 290 / 323 CC 59 SCAR 21.4
CC
59
Cov
93%
diff
19.1
with_dependents
9.1
as_sql
9.1
CRAP 8.5
« PostgreSQL/Diff/ForeignKey.pm ↑ Summary PostgreSQL/Diff/CaseOnly.pm »
1 package DBIO::PostgreSQL::Diff::View;
2 # ABSTRACT: Diff operations for PostgreSQL views and materialized views
3  
4 120 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 120 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 120 our $VERSION = '0.900004';
8  
9 120 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 120 ▶use DBIO::PostgreSQL::Diff::NoStatement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 120 ▶use DBIO::PostgreSQL::Diff::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 120 ▶use DBIO::PostgreSQL::Util qw(quote_qualified trim_end);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 120 __PACKAGE__->mk_diff_accessors(qw(schema_name table_name table_info replaced));
16  
17  
18  
19  
20  
21  
22 120 my %KIND = (
23 v => 'VIEW',
24 m => 'MATERIALIZED VIEW',
25 );
26  
27  
28 3604 ▶sub is_view {
Subroutine is_view: called CC=4 SCAR=13.9
Pod documented
29 3604 my ($class, $info) = @_;
30 3604 ▶ return $info && $KIND{ $info->{kind} // '' } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$info && $KIND{$info->{'kind'} // ''} ? : 518 3086 3604
Condition80%4/5
$info and $KIND{$info->{"kind"} // ""}
!ll&&!rl&&r
03086518
$info->{"kind"} // ""
l!l
3324280
MC/DC50%1/2
$info and $KIND{$info->{"kind"} // ""}
$info $KIND{$info->{"kind"} // ""}
MC/DC100%1/1
$info->{"kind"} // ""
$info->{"kind"}
Truth table66%2/3
A and B
A= $infoB= $KIND{$info->{"kind"} // ""}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $info->{"kind"}
Aresult
11
00
31 }
32  
33 # The views among the entries of a tables section.
34 1001 ▶sub _views {
Subroutine _views: called CC=1 SCAR=0.0
35 1001 my ($tables) = @_;
36 1001 return { map { $_ => $tables->{$_} } grep { __PACKAGE__->is_view($tables->{$_}) } keys %$tables };
37 }
38  
39 # A definition as it is compared and written: pg_get_viewdef hands it back
40 # with a blank ahead of it and a semicolon after it.
41 200 ▶sub _definition {
Subroutine _definition: called CC=2 SCAR=6.9
42 200 my ($info) = @_;
43 200 ▶ my $definition = $info->{view_definition} // '';
Condition100%2/2
$info->{"view_definition"} // ""
l!l
1973
MC/DC100%1/1
$info->{"view_definition"} // ""
$info->{"view_definition"}
Truth table100%2/2
A
A= $info->{"view_definition"}
Aresult
11
00
44 200 $definition =~ s/\A\s+//;
45 200 return trim_end($definition, ';');
46 }
47  
48 # The keys of $views in the order of dependency: each after the views it
49 # stands on.
50 485 ▶sub _in_dependency_order {
Subroutine _in_dependency_order: called CC=2 SCAR=6.9
51 485 my ($views) = @_;
52 485 my (%seen, @order);
53 485 my $visit;
54 219 ▶ $visit = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
55 219 my ($key) = @_;
56 219 ▶ return if $seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key}++ 39 180 219
57 180 ▶ $visit->($_) for sort grep { $views->{$_} } @{ $views->{$key}{depends_on} // [] };
Condition100%2/2
$views->{$key}{"depends_on"} // []
l!l
1755
MC/DC100%1/1
$views->{$key}{"depends_on"} // []
$views->{$key}{"depends_on"}
Truth table100%2/2
A
A= $views->{$key}{"depends_on"}
Aresult
11
00
58 180 push @order, $key;
59 485 };
60 485 $visit->($_) for sort keys %$views;
61 485 undef $visit;
62 485 return @order;
63 }
64  
65  
66 # The closure of @todo over the views that stand on them.
67 256 ▶sub _with_standing_on {
Subroutine _with_standing_on: called CC=7 SCAR=19.5
68 256 my ($src, @todo) = @_;
69 256 my %dependents;
70 256 for my $key (keys %$src) {
71 66 ▶ push @{ $dependents{$_} }, $key for @{ $src->{$key}{depends_on} // [] };
Condition100%2/2
$src->{$key}{"depends_on"} // []
l!l
651
MC/DC100%1/1
$src->{$key}{"depends_on"} // []
$src->{$key}{"depends_on"}
Truth table100%2/2
A
A= $src->{$key}{"depends_on"}
Aresult
11
00
72 }
73 256 my %rebuilt;
74 256 ▶ while (defined(my $key = shift @todo)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = shift @todo)) 42 256 298
75 42 ▶ next if $rebuilt{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $rebuilt{$key}++ 3 39 42
76 39 ▶ push @todo, @{ $dependents{$key} // [] };
Condition100%2/2
$dependents{$key} // []
l!l
1227
MC/DC100%1/1
$dependents{$key} // []
$dependents{$key}
Truth table100%2/2
A
A= $dependents{$key}
Aresult
11
00
77 }
78 256 return \%rebuilt;
79 }
80  
81 # The views whose definition differs in the target.
82 256 ▶sub _changed_definition {
Subroutine _changed_definition: called CC=4 SCAR=13.9
83 256 my ($src, $tgt) = @_;
84 return grep {
85 256 $tgt->{$_}
86 && $src->{$_}{kind} eq 'v' && $tgt->{$_}{kind} eq 'v'
87 66 ▶ && _definition($src->{$_}) ne _definition($tgt->{$_})
Condition88%8/9
$tgt->{$_} && $src->{$_}{'kind'} eq 'v' && $tgt->{$_}{'kind'} eq 'v' && _definition($src->{$_}) ne _definition($tgt->{$_})
!ll&&!rl&&r
163713
$tgt->{$_} and $src->{$_}{"kind"} eq "v" and $tgt->{$_}{"kind"} eq "v"
!ll&&!rl&&r
16050
$tgt->{$_} and $src->{$_}{"kind"} eq "v"
!ll&&!rl&&r
9750
MC/DC75%3/4
$tgt->{$_} && $src->{$_}{'kind'} eq 'v' && $tgt->{$_}{'kind'} eq 'v' && _definition($src->{$_}) ne _definition($tgt->{$_})
$tgt->{$_} $src->{$_}{"kind"} eq "v" $tgt->{$_}{"kind"} eq "v" _definition($src->{$_}) ne _definition($tgt->{$_})
Truth table80%4/5
A and B and C && D
A= $tgt->{$_}B= $src->{$_}{"kind"} eq "v"C= $tgt->{$_}{"kind"} eq "v"D= _definition($src->{$_}) ne _definition($tgt->{$_})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
88 } keys %$src;
89 }
90  
91 256 ▶sub rebuilt {
Subroutine rebuilt: called CC=7 SCAR=20.1
Pod documented
92 256 my ($class, $source, $target, $source_cols, $target_cols) = @_;
93 256 ▶ my $src = _views($source // {});
Condition100%2/2
$source // {}
l!l
24511
MC/DC100%1/1
$source // {}
$source
Truth table100%2/2
A
A= $source
Aresult
11
00
94 256 ▶ my $tgt = _views($target // {});
Condition100%2/2
$target // {}
l!l
24511
MC/DC100%1/1
$target // {}
$target
Truth table100%2/2
A
A= $target
Aresult
11
00
95  
96 256 my @todo = _changed_definition($src, $tgt);
97  
98 # A column that changes its type is the one change PostgreSQL does not make
99 # under a view: the views that read it are rebuilt around it. Only views the
100 # target has; one it does not have, and a materialized view, are refused
101 # where the column is (Diff::Column).
102 256 ▶ if ($source_cols && $target_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_cols and $target_cols) 242 14 256
Condition66%2/3
$source_cols and $target_cols
!ll&&!rl&&r
140242
MC/DC50%1/2
$source_cols and $target_cols
$source_cols $target_cols
Truth table66%2/3
A and B
A= $source_colsB= $target_cols
ABresult
0X0
100
111
103 242 my $reading = DBIO::PostgreSQL::Diff::Column->type_changed_readers(
104 $source_cols, $target_cols, $source, $target);
105 push @todo, grep {
106 242 ▶ $tgt->{$_} && $src->{$_}{kind} eq 'v' && $tgt->{$_}{kind} eq 'v'
Condition33%2/6
$tgt->{$_} && $src->{$_}{'kind'} eq 'v' && $tgt->{$_}{'kind'} eq 'v'
!ll&&!rl&&r
0017
$tgt->{$_} and $src->{$_}{"kind"} eq "v"
!ll&&!rl&&r
0017
MC/DC0%0/3
$tgt->{$_} && $src->{$_}{'kind'} eq 'v' && $tgt->{$_}{'kind'} eq 'v'
$tgt->{$_} $src->{$_}{"kind"} eq "v" $tgt->{$_}{'kind'} eq 'v'
Truth table25%1/4
A and B && C
A= $tgt->{$_}B= $src->{$_}{"kind"} eq "v"C= $tgt->{$_}{'kind'} eq 'v'
ABCresult
0XX0
10X0
1100
1111
107 } keys %$reading;
108 }
109  
110 256 return _with_standing_on($src, @todo);
111 }
112  
113  
114 29 ▶sub with_dependents {
Subroutine with_dependents: called CC=9 SCAR=22.1
Pod documented
115 29 my ($class, $tables, @keys) = @_;
116 29 ▶ my $views = _views($tables // {});
Condition50%1/2
$tables // {}
l!l
290
MC/DC0%0/1
$tables // {}
$tables
Truth table50%1/2
A
A= $tables
Aresult
11
00
117  
118 29 my %dependents;
119 29 for my $key (keys %$views) {
120 58 ▶ push @{ $dependents{$_} }, $key for @{ $views->{$key}{depends_on} // [] };
Condition50%1/2
$views->{$key}{"depends_on"} // []
l!l
580
MC/DC0%0/1
$views->{$key}{"depends_on"} // []
$views->{$key}{"depends_on"}
Truth table50%1/2
A
A= $views->{$key}{"depends_on"}
Aresult
11
00
121 }
122  
123 29 my %found;
124 29 ▶ while (defined(my $key = shift @keys)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $key = shift @keys)) 44 29 73
125 44 ▶ next if !$views->{$key} || $found{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if not $views->{$key} or $found{$key}++ 1 43 44
Condition66%2/3
not $views->{$key} or $found{$key}++
l!l&&r!l&&!r
0143
MC/DC50%1/2
not $views->{$key} or $found{$key}++
not $views->{$key} $found{$key}++
Truth table66%2/3
not(A) or B
A= not $views->{$key}B= $found{$key}++
ABresult
1X1
011
000
126 43 ▶ push @keys, @{ $dependents{$key} // [] };
Condition100%2/2
$dependents{$key} // []
l!l
1132
MC/DC100%1/1
$dependents{$key} // []
$dependents{$key}
Truth table100%2/2
A
A= $dependents{$key}
Aresult
11
00
127 }
128 29 return grep { $found{$_} } reverse _in_dependency_order($views);
129 }
130  
131  
132 8 ▶sub drop_sql {
Subroutine drop_sql: called CC=1 SCAR=0.0
Pod documented
133 8 my ($class, $info) = @_;
134 return sprintf 'DROP %s %s;', $KIND{ $info->{kind} },
135 8 quote_qualified(@{$info}{qw( schema_name table_name )});
136 }
137  
138  
139 4 ▶sub _name {
Subroutine _name: called CC=1 SCAR=0.0
140 4 my ($info) = @_;
141 4 return "$info->{schema_name}.$info->{table_name}";
142 }
143  
144 1 ▶sub _refuse_changed_matview {
Subroutine _refuse_changed_matview: called CC=1 SCAR=0.0
145 1 my ($info) = @_;
146 1 my $name = _name($info);
147 die join "\n",
148 "refusing to diff: materialized view $name has another definition in the target",
149 '',
150 'A materialized view cannot be given another definition in place: it has to be',
151 'dropped and created again, which builds its data anew and takes the indexes on',
152 'it with it. No statement was generated.',
153 '',
154 'Drop it yourself when that is acceptable,',
155 1 ' DROP MATERIALIZED VIEW ' . quote_qualified(@{$info}{qw( schema_name table_name )}) . ';',
156 'and diff again: the diff then creates it as the target has it, with the indexes',
157 'the target has on it.',
158 '';
159 }
160  
161 1 ▶sub _refuse_matview_on_rebuilt_view {
Subroutine _refuse_matview_on_rebuilt_view: called CC=1 SCAR=0.0
162 1 my ($info, @views) = @_;
163 1 my $name = _name($info);
164 die join "\n",
165 sprintf(
166 'refusing to diff: materialized view %s stands on %s, which changes, and would have to be dropped and created again with it',
167 1 $name, join(', ', map { "view $_" } @views),
168 ),
169 '',
170 'A view with another definition is dropped and created again, and so is',
171 'everything that reads it. For a materialized view that builds its data anew',
172 'and takes the indexes on it with it. No statement was generated.',
173 '',
174 'Drop it yourself when that is acceptable,',
175 1 ' DROP MATERIALIZED VIEW ' . quote_qualified(@{$info}{qw( schema_name table_name )}) . ';',
176 'and diff again: the diff then creates it as the target has it, with the indexes',
177 'the target has on it.',
178 '';
179 }
180  
181 2 ▶sub _refuse_without_definition {
Subroutine _refuse_without_definition: called CC=1 SCAR=0.0
182 2 my ($info, $in_database) = @_;
183 DBIO::PostgreSQL::Diff::NoStatement->refuse_without_definition(
184 kind => lc $KIND{ $info->{kind} },
185 2 name => _name($info),
186 in_database => $in_database,
187 from => 'pg_get_viewdef',
188 );
189 }
190  
191 80 ▶sub _op {
Subroutine _op: called CC=1 SCAR=0.0
192 80 my ($class, $action, $info, @more) = @_;
193 return $class->new(
194 action => $action,
195 schema_name => $info->{schema_name},
196 table_name => $info->{table_name},
197 80 table_info => $info,
198 @more,
199 );
200 }
201  
202 230 ▶sub diff {
Subroutine diff: called CC=19 SCAR=29.5
Pod documented
203 230 my ($class, $source, $target, $source_cols, $target_cols) = @_;
204 230 ▶ $source //= {};
Condition50%1/2
$source //= {}
l!l
2300
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
205 230 ▶ $target //= {};
Condition50%1/2
$target //= {}
l!l
2300
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
206 230 my $src = _views($source);
207 230 my $tgt = _views($target);
208  
209 230 for my $key (sort keys %$tgt) {
210 68 my ($old, $new) = ($src->{$key}, $tgt->{$key});
211 68 ▶ next unless $old && $old->{kind} eq 'm' && $new->{kind} eq 'm';
Branch: 1 branch
BranchTrueFalseTotal
unless $old and $old->{"kind"} eq "m" and $new->{"kind"} eq "m" 60 8 68
Condition83%5/6
$old and $old->{"kind"} eq "m" and $new->{"kind"} eq "m"
!ll&&!rl&&r
6008
$old and $old->{"kind"} eq "m"
!ll&&!rl&&r
16448
MC/DC66%2/3
$old and $old->{"kind"} eq "m" and $new->{"kind"} eq "m"
$old $old->{"kind"} eq "m" $new->{"kind"} eq "m"
Truth table75%3/4
A and B and C
A= $oldB= $old->{"kind"} eq "m"C= $new->{"kind"} eq "m"
ABCresult
0XX0
10X0
1100
1111
212 8 ▶ _refuse_changed_matview($new) if _definition($old) ne _definition($new);
Branch: 1 branch
BranchTrueFalseTotal
if _definition($old) ne _definition($new) 1 7 8
213 }
214  
215 229 my $rebuilt = $class->rebuilt($source, $target, $source_cols, $target_cols);
216 229 for my $key (sort keys %$rebuilt) {
217 32 ▶ next unless $src->{$key}{kind} eq 'm' && $tgt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $src->{$key}{"kind"} eq "m" and $tgt->{$key} 31 1 32
Condition100%3/3
$src->{$key}{"kind"} eq "m" and $tgt->{$key}
!ll&&!rl&&r
3011
MC/DC100%2/2
$src->{$key}{"kind"} eq "m" and $tgt->{$key}
$src->{$key}{"kind"} eq "m" $tgt->{$key}
Truth table100%3/3
A and B
A= $src->{$key}{"kind"} eq "m"B= $tgt->{$key}
ABresult
0X0
100
111
218 _refuse_matview_on_rebuilt_view(
219 1 ▶ $src->{$key}, grep { $rebuilt->{$_} } @{ $src->{$key}{depends_on} // [] },
Condition50%1/2
$src->{$key}{"depends_on"} // []
l!l
10
MC/DC0%0/1
$src->{$key}{"depends_on"} // []
$src->{$key}{"depends_on"}
Truth table50%1/2
A
A= $src->{$key}{"depends_on"}
Aresult
11
00
220 );
221 }
222  
223 # What goes, each view ahead of the ones it stands on: the rebuilt ones to
224 # stay where they are, the rest for the removals.
225 228 my (@ahead, @gone);
226 228 for my $key (reverse _in_dependency_order($src)) {
227 57 ▶ if ($rebuilt->{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rebuilt->{$key}) { } 31 26 57
elsif (not exists $target->{$key}) { } 6 20 26
228 31 push @ahead, $class->_op(drop => $src->{$key}, replaced => 1);
229 }
230 elsif (!exists $target->{$key}) {
231 6 push @gone, $class->_op(drop => $src->{$key});
232 }
233 }
234  
235 # What comes, each view after the ones it stands on: the views the source
236 # does not have, and the rebuilt ones the target has.
237 228 my @create;
238 228 for my $key (_in_dependency_order($tgt)) {
239 65 ▶ next if exists $source->{$key} && !$rebuilt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $source->{$key} and not $rebuilt->{$key} 20 45 65
Condition100%3/3
exists $source->{$key} and not $rebuilt->{$key}
!ll&&!rl&&r
162920
MC/DC100%2/2
exists $source->{$key} and not $rebuilt->{$key}
exists $source->{$key} not $rebuilt->{$key}
Truth table100%3/3
A and not(B)
A= exists $source->{$key}B= not $rebuilt->{$key}
ABresult
0X0
100
111
240 _refuse_without_definition($tgt->{$key}, exists $source->{$key})
241 45 ▶ unless length _definition($tgt->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless length _definition($tgt->{$key}) 2 43 45
242 43 push @create, $class->_op(create => $tgt->{$key});
243 }
244  
245 226 return (@ahead, @create, @gone);
246 }
247  
248  
249 5 ▶sub qualified_name {
Subroutine qualified_name: called CC=1 SCAR=0.0
Pod documented
250 5 my ($self) = @_;
251 5 return $self->schema_name . '.' . $self->table_name;
252 }
253  
254  
255 73 ▶sub as_sql {
Subroutine as_sql: called CC=9 SCAR=22.1
Pod documented
256 73 my ($self) = @_;
257 73 ▶ my $info = $self->table_info // {};
Condition50%1/2
$self->table_info // {}
l!l
730
MC/DC0%0/1
$self->table_info // {}
$self->table_info
Truth table50%1/2
A
A= $self->table_info
Aresult
11
00
258 73 ▶ my $action = $self->action // '';
Condition50%1/2
$self->action // ""
l!l
730
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
Aresult
11
00
259 73 ▶ my $kind = $KIND{ $info->{kind} // '' };
Condition50%1/2
$info->{"kind"} // ""
l!l
730
MC/DC0%0/1
$info->{"kind"} // ""
$info->{"kind"}
Truth table50%1/2
A
A= $info->{"kind"}
Aresult
11
00
260 my $object = sprintf '%s %s', lc($kind // 'relation'),
261 73 ▶ join '.', grep { defined } $self->schema_name, $self->table_name;
Condition100%2/2
$kind // "relation"
l!l
721
MC/DC100%1/1
$kind // "relation"
$kind
Truth table100%2/2
A
A= $kind
Aresult
11
00
262  
263 73 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $kind 1 72 73
264 $self, $object, 'its table_info is not a view or a materialized view',
265 ) unless $kind;
266 72 my $name = quote_qualified($self->schema_name, $self->table_name);
267  
268 72 ▶ if ($action eq 'create') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'create') { } 39 33 72
elsif ($action eq 'drop') { } 32 1 33
269 39 my $definition = _definition($info);
270 39 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless length $definition 1 38 39
271 $self, $object, 'its table_info has no definition',
272 ) unless length $definition;
273 38 return sprintf 'CREATE %s %s AS %s;', $kind, $name, $definition;
274 }
275 elsif ($action eq 'drop') {
276 32 return sprintf 'DROP %s %s;', $kind, $name;
277 }
278 1 DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object);
279 }
280  
281  
282 5 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
283 5 my ($self) = @_;
284 return sprintf ' %s%s: %s',
285 ($self->action eq 'create' ? '+' : '-'),
286 5 ▶ lc $KIND{ $self->table_info->{kind} }, $self->qualified_name;
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 3 2 5
287 }
288  
289 120 1;
290  
291 __END__
« PostgreSQL/Diff/ForeignKey.pm ↑ Summary PostgreSQL/Diff/CaseOnly.pm »