blib/lib/DBIO/PostgreSQL/Diff/Extension.pm

filter: Statementstmt 100% 82 / 82 Branchbran 87.5% 35 / 40 Conditioncond 73.6% 28 / 38 MC/DCmcdc 52.3% 11 / 21 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 87.1% 170 / 195 CC 42 SCAR 24.6
CC
42
Cov
91%
summary
17.6
diff
14.1
as_sql
11.0
CRAP 11.7
« blib/lib/DBIO/PostgreSQL/Introspect/Extensions.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/Policy.pm »
1 package DBIO::PostgreSQL::Diff::Extension;
2 # ABSTRACT: Diff operations for PostgreSQL extensions
3  
4 103 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 103 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 103 our $VERSION = '0.900007';
8  
9 103 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 103 ▶use DBIO::PostgreSQL::Diff::NoStatement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 103 ▶use DBIO::PostgreSQL::Util qw(quote_ident quote_literal create_schema_if_missing);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 103 __PACKAGE__->mk_diff_accessors(qw(
15 extension_name extension_info old_version new_version old_schema new_schema
16 ));
17  
18  
19  
20  
21  
22  
23  
24  
25  
26 # The $names by name, except that each comes after the names $after->{$name}
27 # lists. A listed name that is not among $names is passed over. PostgreSQL
28 # allows no cycle between extensions; one in a model is cut where it closes.
29 146 ▶sub _in_order {
Subroutine _in_order: called CC=2 SCAR=6.9
30 146 my ($names, $after) = @_;
31 146 my %pending = map { $_ => 1 } @$names;
32 146 my @ordered;
33 146 _place($_, $after, \%pending, \@ordered) for sort @$names;
34 146 return @ordered;
35 }
36  
37 113 ▶sub _place {
Subroutine _place: called CC=4 SCAR=13.9
38 113 my ($name, $after, $pending, $ordered) = @_;
39 113 ▶ return unless delete $pending->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless delete $pending->{$name} 11 102 113
40 102 ▶ _place($_, $after, $pending, $ordered) for sort @{ $after->{$name} // [] };
Condition100%2/2
$after->{$name} // []
l!l
4260
MC/DC100%1/1
$after->{$name} // []
$after->{$name}
Truth table100%2/2
A
A= $after->{$name}
Aresult
11
00
41 102 push @$ordered, $name;
42 }
43  
44 2 ▶sub _refuse_not_relocatable {
Subroutine _refuse_not_relocatable: called CC=1 SCAR=0.0
45 2 my ($name, $have, $declared) = @_;
46 2 my $ext = quote_ident($name);
47 2 die join "\n",
48 "refusing to diff: extension $name is installed in schema $have and pg_extensions declares schema $declared, but it is not relocatable",
49 '',
50 "PostgreSQL moves an extension with ALTER EXTENSION ... SET SCHEMA only if its",
51 "control file says relocatable; the control file of $name does not. No statement was generated.",
52 '',
53 "Declare the schema it is in ($have) or none, or drop and create the extension",
54 "yourself in the schema you want (DROP EXTENSION $ext CASCADE takes every",
55 "object that uses it, the data of its columns included), and diff again.",
56 '';
57 }
58  
59 74 ▶sub diff {
Subroutine diff: called CC=14 SCAR=26.4
Pod documented
60 74 my ($class, $source, $target) = @_;
61 74 my @ops;
62  
63 # Created or updated after what it requires.
64 74 my %requires = map { $_ => $target->{$_}{requires} } keys %$target;
65  
66 74 for my $name (_in_order([ keys %$target ], \%requires)) {
67 73 ▶ if (!exists $source->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $source->{$name}) 41 32 73
68 push @ops, $class->new(
69 action => 'create',
70 extension_name => $name,
71 41 extension_info => $target->{$name},
72 );
73 41 next;
74 }
75 # Version changes
76 32 ▶ my $src_ver = $source->{$name}{version} // '';
Condition50%1/2
$source->{$name}{"version"} // ""
l!l
320
MC/DC0%0/1
$source->{$name}{"version"} // ""
$source->{$name}{"version"}
Truth table50%1/2
A
A= $source->{$name}{"version"}
Aresult
11
00
77 32 ▶ my $tgt_ver = $target->{$name}{version} // '';
Condition50%1/2
$target->{$name}{"version"} // ""
l!l
320
MC/DC0%0/1
$target->{$name}{"version"} // ""
$target->{$name}{"version"}
Truth table50%1/2
A
A= $target->{$name}{"version"}
Aresult
11
00
78 32 ▶ if ($src_ver ne $tgt_ver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_ver ne $tgt_ver) 14 18 32
79 push @ops, $class->new(
80 action => 'update',
81 extension_name => $name,
82 14 extension_info => $target->{$name},
83 old_version => $src_ver,
84 new_version => $tgt_ver,
85 );
86 }
87 # Schema changes: only a schema the target declares.
88 32 my $declared = $target->{$name}{declared_schema};
89 32 my $have = $source->{$name}{schema_name};
90 32 ▶ if (defined $declared && defined $have && $declared ne $have) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $declared and defined $have and $declared ne $have) 7 25 32
Condition83%5/6
defined $declared and defined $have and $declared ne $have
!ll&&!rl&&r
2417
defined $declared and defined $have
!ll&&!rl&&r
2408
MC/DC66%2/3
defined $declared and defined $have and $declared ne $have
defined $declared defined $have $declared ne $have
Truth table75%3/4
A and B and C
A= defined $declaredB= defined $haveC= $declared ne $have
ABCresult
0XX0
10X0
1100
1111
91 _refuse_not_relocatable($name, $have, $declared)
92 7 ▶ unless $source->{$name}{relocatable};
Branch: 1 branch
BranchTrueFalseTotal
unless $source->{$name}{"relocatable"} 2 5 7
93 push @ops, $class->new(
94 action => 'move',
95 extension_name => $name,
96 5 extension_info => $target->{$name},
97 old_schema => $have,
98 new_schema => $declared,
99 );
100 }
101 }
102  
103 # Dropped after what requires it.
104 72 my %required_by;
105 72 for my $name (keys %$source) {
106 59 ▶ push @{ $required_by{$_} }, $name for @{ $source->{$name}{requires} // [] };
Condition100%2/2
$source->{$name}{"requires"} // []
l!l
3128
MC/DC100%1/1
$source->{$name}{"requires"} // []
$source->{$name}{"requires"}
Truth table100%2/2
A
A= $source->{$name}{"requires"}
Aresult
11
00
107 }
108 72 my @gone = grep { !exists $target->{$_} } keys %$source;
109  
110 72 for my $name (_in_order(\@gone, \%required_by)) {
111 push @ops, $class->new(
112 action => 'drop',
113 extension_name => $name,
114 29 extension_info => $source->{$name},
115 );
116 }
117  
118 72 return @ops;
119 }
120  
121  
122 89 ▶sub as_sql {
Subroutine as_sql: called CC=11 SCAR=24.0
Pod documented
123 89 my ($self) = @_;
124 89 my $extension = quote_ident($self->extension_name);
125 89 ▶ if ($self->action eq 'create') {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->action eq 'create') { } 50 39 89
elsif ($self->action eq 'move') { } 3 36 39
elsif ($self->action eq 'drop') { } 29 7 36
elsif ($self->action eq 'update') { } 6 1 7
126 50 ▶ my $info = $self->extension_info // {};
Condition50%1/2
$self->extension_info // {}
l!l
500
MC/DC0%0/1
$self->extension_info // {}
$self->extension_info
Truth table50%1/2
A
A= $self->extension_info
Aresult
11
00
127 50 ▶ my $schema = $info->{declared_schema} // $info->{install_schema};
Condition100%3/3
$info->{"declared_schema"} // $info->{"install_schema"}
l!l&&r!l&&!r
7835
MC/DC100%2/2
$info->{"declared_schema"} // $info->{"install_schema"}
$info->{"declared_schema"} $info->{"install_schema"}
Truth table100%3/3
A // B
A= $info->{"declared_schema"}B= $info->{"install_schema"}
ABresult
1X1
011
000
128 return sprintf("%s;\nCREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;",
129 create_schema_if_missing($schema), $extension, quote_ident($schema))
130 50 ▶ if defined $schema && $info->{create_schema};
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema and $info->{"create_schema"} 3 47 50
Condition100%3/3
defined $schema and $info->{"create_schema"}
!ll&&!rl&&r
35123
MC/DC100%2/2
defined $schema and $info->{"create_schema"}
defined $schema $info->{"create_schema"}
Truth table100%3/3
A and B
A= defined $schemaB= $info->{"create_schema"}
ABresult
0X0
100
111
131 47 ▶ return defined $schema
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 12 35 47
132 ? sprintf('CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;', $extension, quote_ident($schema))
133 : sprintf('CREATE EXTENSION IF NOT EXISTS %s;', $extension);
134 }
135 elsif ($self->action eq 'move') {
136 3 return sprintf 'ALTER EXTENSION %s SET SCHEMA %s;',
137 $extension, quote_ident($self->new_schema);
138 }
139 elsif ($self->action eq 'drop') {
140 29 return sprintf 'DROP EXTENSION %s;', $extension;
141 }
142 elsif ($self->action eq 'update') {
143 6 return sprintf 'ALTER EXTENSION %s UPDATE TO %s;',
144 $extension, quote_literal($self->new_version);
145 }
146 1 ▶ DBIO::PostgreSQL::Diff::NoStatement->throw($self, 'extension ' . ($self->extension_name // ''));
Condition50%1/2
$self->extension_name // ""
l!l
10
MC/DC0%0/1
$self->extension_name // ""
$self->extension_name
Truth table50%1/2
A
A= $self->extension_name
Aresult
11
00
147 }
148  
149  
150 16 ▶sub summary {
Subroutine summary: called CC=15 SCAR=28.7
Pod documented
151 16 my ($self) = @_;
152 16 ▶ if ($self->action eq 'update') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "update") 1 15 16
153 1 return sprintf ' ~extension: %s (%s -> %s)',
154 $self->extension_name, $self->old_version, $self->new_version;
155 }
156 15 ▶ if ($self->action eq 'move') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "move") 2 13 15
157 2 return sprintf ' ~extension: %s (schema %s -> %s)',
158 $self->extension_name, $self->old_schema, $self->new_schema;
159 }
160 13 ▶ my $info = $self->extension_info // {};
Condition50%1/2
$self->extension_info // {}
l!l
130
MC/DC0%0/1
$self->extension_info // {}
$self->extension_info
Truth table50%1/2
A
A= $self->extension_info
Aresult
11
00
161 13 ▶ if ($self->action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "create") 8 5 13
162 8 ▶ my $schema = $info->{declared_schema} // $info->{install_schema} // $info->{control_schema};
Condition100%6/6
($info->{"declared_schema"} // $info->{"install_schema"}) // $info->{"control_schema"}
l!l&&r!l&&!r
215
$info->{"declared_schema"} // $info->{"install_schema"}
l!l&&r!l&&!r
116
MC/DC100%3/3
($info->{"declared_schema"} // $info->{"install_schema"}) // $info->{"control_schema"}
$info->{"declared_schema"} $info->{"install_schema"} $info->{"control_schema"}
Truth table100%4/4
A // B // C
A= $info->{"declared_schema"}B= $info->{"install_schema"}C= $info->{"control_schema"}
ABCresult
1XX1
01X1
0011
0000
163 my @facts = (
164 8 ▶ (defined $info->{version} && length $info->{version} ? ($info->{version}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $info->{'version'} && length $info->{'version'} ? : 8 0 8
defined $schema ? : 3 5 8
Condition33%1/3
defined $info->{"version"} and length $info->{"version"}
!ll&&!rl&&r
008
MC/DC0%0/2
defined $info->{"version"} and length $info->{"version"}
defined $info->{"version"} length $info->{"version"}
Truth table33%1/3
A and B
A= defined $info->{"version"}B= length $info->{"version"}
ABresult
0X0
100
111
165 (defined $schema ? ("schema $schema") : ()),
166 );
167 8 ▶ return sprintf ' +extension: %s%s', $self->extension_name,
Branch: 1 branch
BranchTrueFalseTotal
@facts ? : 8 0 8
168 @facts ? ' (' . join(', ', @facts) . ')' : '';
169 }
170 # A drop: the extension is installed and the schema does not declare it.
171 # DROP EXTENSION (no CASCADE) takes the objects the extension owns; what an
172 # extension made at runtime is no member of it, and is named by the
173 # operations of its own, where the diff has them in its model.
174 my @facts = (
175 (defined $info->{version} && length $info->{version} ? ($info->{version}) : ()),
176 5 ▶ (defined $info->{schema_name} ? ("schema $info->{schema_name}") : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $info->{'version'} && length $info->{'version'} ? : 5 0 5
defined $info->{'schema_name'} ? : 5 0 5
Condition33%1/3
defined $info->{"version"} and length $info->{"version"}
!ll&&!rl&&r
005
MC/DC0%0/2
defined $info->{"version"} and length $info->{"version"}
defined $info->{"version"} length $info->{"version"}
Truth table33%1/3
A and B
A= defined $info->{"version"}B= length $info->{"version"}
ABresult
0X0
100
111
177 );
178 5 ▶ return sprintf ' -extension: %s (%snot declared, DROP EXTENSION takes the objects it owns)',
Branch: 1 branch
BranchTrueFalseTotal
@facts ? : 5 0 5
179 $self->extension_name, @facts ? join(', ', @facts) . '; ' : '';
180 }
181  
182 103 1;
183  
184 __END__
« blib/lib/DBIO/PostgreSQL/Introspect/Extensions.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Diff/Policy.pm »