blib/lib/DBIO/PostgreSQL/Deploy.pm

filter: Statementstmt 85.0% 314 / 369 Branchbran 72.7% 112 / 154 Conditioncond 57.3% 86 / 150 MC/DCmcdc 38.7% 31 / 80 Subroutinesub 92.1% 59 / 64 Podpod 100% 5 / 5 totaltotal 73.8% 607 / 822 CC 153 SCAR 31.5
CC
153
Cov
76%
_refuse_unfillable
195.8
_check_temp_db_empty
56.0
_installed_extension_schemas
23.9
CRAP 23.3
« blib/lib/DBIO/PostgreSQL/Diff/Column.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/OutsideDependents.pm »
1 package DBIO::PostgreSQL::Deploy;
2 # ABSTRACT: Deploy and upgrade PostgreSQL schemas via test-deploy-and-compare
3  
4 66 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 66 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 66 our $VERSION = '0.900007';
8  
9 66 ▶use base 'DBIO::Deploy::Base::TempDatabase';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 66 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 66 ▶use Hash::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 66 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 66 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 66 ▶use Crypt::URandom ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 66 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 66 ▶use DBIO::Extension::List ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 66 ▶use DBIO::PostgreSQL::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 66 ▶use DBIO::PostgreSQL::Extension ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 66 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 66 ▶use DBIO::PostgreSQL::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 66 ▶use DBIO::PostgreSQL::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25  
26 13 ▶sub manage_extensions_when_restricted { $_[0]->{manage_extensions_when_restricted} }
Subroutine manage_extensions_when_restricted: called CC=1 SCAR=0.0
Pod documented
27  
28  
29 31 ▶sub foreign_keys_not_valid { $_[0]->{foreign_keys_not_valid} }
Subroutine foreign_keys_not_valid: called CC=1 SCAR=0.0
Pod documented
30  
31 # --- class-name hooks for DBIO::Deploy::Base -------------------------------
32  
33 2 ▶sub _ddl_class { 'DBIO::PostgreSQL::DDL' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
34 3 ▶sub _introspect_class { 'DBIO::PostgreSQL::Introspect' }
Subroutine _introspect_class: called CC=1 SCAR=0.0
35 51 ▶sub _diff_class { 'DBIO::PostgreSQL::Diff' }
Subroutine _diff_class: called CC=1 SCAR=0.0
36  
37  
38 0 ▶sub install_schema {
Subroutine install_schema: not called CC=1 SCAR=6.9
Pod documented
39 0 my ($self, $schema_name) = @_;
40 0 my $dbh = $self->_dbh;
41 0 $dbh->do(sprintf 'CREATE SCHEMA IF NOT EXISTS %s',
42 DBIO::PostgreSQL::Util::quote_ident($schema_name));
43 0 return 1;
44 }
45  
46 # --- extension prerequisites -------------------------------------------------
47  
48  
49 2 ▶sub install {
Subroutine install: called CC=1 SCAR=0.0
Pod documented
50 2 my ($self) = @_;
51 2 $self->_resolve_extension_list;
52 2 $self->_check_extensions_available;
53 2 $self->_extension_control_schemas;
54 2 return $self->SUPER::install;
55 }
56  
57  
58 0 ▶sub _deploy_and_introspect_temp {
Subroutine _deploy_and_introspect_temp: not called CC=1 SCAR=6.9
59 0 my ($self, $temp_db) = @_;
60 0 $self->_check_extensions_available;
61 0 $self->_check_temp_db_empty($temp_db);
62 0 local $self->{_temp_extension_schemas} = $self->_installed_extension_schemas;
63 0 return $self->SUPER::_deploy_and_introspect_temp($temp_db);
64 }
65  
66  
67 0 ▶sub _check_temp_db_empty {
Subroutine _check_temp_db_empty: not called CC=7 SCAR=40.3
68 0 my ($self, $temp_db) = @_;
69 0 my ($dsn, $user, $pass) = $self->_temp_connect_info($temp_db);
70 0 ▶ my $dbh = eval {
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = eval { do { "DBI"->connect($dsn, $user, $pass, {"RaiseError", 1, "PrintError", 0, "AutoCommit", 1}) } } or die DBIO::Util::mask_connect_secrets($@ || "Cannot connect to temp database: $DBI::errstr", [$dsn, $user, $pass]) 0 0 0
71 0 ▶ DBI->connect($dsn, $user, $pass, {
Condition0%0/2
$@ || "Cannot connect to temp database: $DBI::errstr"
l!l
00
MC/DC0%0/1
$@ || "Cannot connect to temp database: $DBI::errstr"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
72 RaiseError => 1, PrintError => 0, AutoCommit => 1,
73 })
74 } or die DBIO::Util::mask_connect_secrets(
75 $@ || "Cannot connect to temp database: $DBI::errstr", [ $dsn, $user, $pass ],
76 );
77  
78 0 my $err;
79 0 ▶ eval { $self->_check_database_empty($dbh); 1 } or $err = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_check_database_empty($dbh); 1 } } or $err = $@ 0 0 0
80 0 ▶ eval { $dbh->disconnect; 1 } or $err ||= $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbh->disconnect; 1 } } or $err ||= $@ 0 0 0
Condition0%0/3
$err ||= $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$err ||= $@
$err $@
Truth table0%0/3
A ||= B
A= $errB= $@
ABresult
1X1
011
000
81 0 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
82 0 return;
83 }
84  
85  
86 2 ▶sub _check_database_empty {
Subroutine _check_database_empty: called CC=2 SCAR=6.9
87 2 my ($self, $dbh) = @_;
88 2 my $model = $self->_introspect_class->new(
89 dbh => $dbh,
90 qualified_names => 1,
91 )->model;
92 2 my @found = $self->_foreign_objects($model);
93 2 ▶ return unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 1 1 2
94 1 DBIO::Exception->throw(
95 'the temporary database is not empty, the target model is only what the schema declares; found: '
96 . join(', ', @found));
97 }
98  
99  
100 10 ▶sub _foreign_objects {
Subroutine _foreign_objects: called CC=8 SCAR=20.8
101 10 my ($self, $model) = @_;
102 10 my @found;
103 10 for my $kind (
104 [ schema => 'schemas' ],
105 [ extension => 'extensions' ],
106 [ type => 'types' ],
107 [ table => 'tables' ],
108 [ sequence => 'sequences' ],
109 [ function => 'functions' ],
110 ) {
111 60 my ($label, $section) = @$kind;
112 60 ▶ for my $name (sort keys %{ $model->{$section} // {} }) {
Condition50%1/2
$model->{$section} // {}
l!l
600
MC/DC0%0/1
$model->{$section} // {}
$model->{$section}
Truth table50%1/2
A
A= $model->{$section}
Aresult
11
00
113 30 ▶ next if $section eq 'schemas' && $name eq 'public';
Branch: 1 branch
BranchTrueFalseTotal
if $section eq "schemas" and $name eq "public" 10 20 30
Condition100%3/3
$section eq "schemas" and $name eq "public"
!ll&&!rl&&r
19110
MC/DC100%2/2
$section eq "schemas" and $name eq "public"
$section eq "schemas" $name eq "public"
Truth table100%3/3
A and B
A= $section eq "schemas"B= $name eq "public"
ABresult
0X0
100
111
114 20 ▶ next if $section eq 'extensions' && $name eq 'plpgsql';
Branch: 1 branch
BranchTrueFalseTotal
if $section eq "extensions" and $name eq "plpgsql" 10 10 20
Condition100%3/3
$section eq "extensions" and $name eq "plpgsql"
!ll&&!rl&&r
9110
MC/DC100%2/2
$section eq "extensions" and $name eq "plpgsql"
$section eq "extensions" $name eq "plpgsql"
Truth table100%3/3
A and B
A= $section eq "extensions"B= $name eq "plpgsql"
ABresult
0X0
100
111
115 10 push @found, "$label $name";
116 }
117 }
118 10 return @found;
119 }
120  
121  
122 12 ▶sub _installed_extension_schemas {
Subroutine _installed_extension_schemas: called CC=12 SCAR=31.7
123 12 my ($self) = @_;
124 12 ▶ my @declared = DBIO::PostgreSQL::DDL::_schema_decl($self->schema, 'pg_extensions')
Branch: 1 branch
BranchTrueFalseTotal
my(@declared) = DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extensions") or return {} 12 0 12
125 or return {};
126 12 ▶ my $given = DBIO::PostgreSQL::DDL::_schema_decl($self->schema, 'pg_extension_schemas') // {};
Condition50%1/2
DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas") // {}
l!l
120
MC/DC0%0/1
DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas") // {}
DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas")
Truth table50%1/2
A
A= DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas")
Aresult
11
00
127 12 my $fixed = $self->_extension_control_schemas;
128 12 my $namespace = DBIO::PostgreSQL::DDL::_connection_namespace($self->schema);
129 12 my $state = $self->_installed_extensions(\@declared);
130 12 my %held;
131 12 for my $name (@declared) {
132 21 ▶ next if defined $given->{$name} || defined $fixed->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given->{$name} or defined $fixed->{$name} 11 10 21
Condition100%3/3
defined $given->{$name} or defined $fixed->{$name}
l!l&&r!l&&!r
7410
MC/DC100%2/2
defined $given->{$name} or defined $fixed->{$name}
defined $given->{$name} defined $fixed->{$name}
Truth table100%3/3
A or B
A= defined $given->{$name}B= defined $fixed->{$name}
ABresult
1X1
011
000
133 10 my $e = $state->{$name};
134 10 ▶ next unless $e && $e->{installed} && defined $e->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $e and $e->{"installed"} and defined $e->{"schema"} 10 0 10
Condition33%2/6
$e and $e->{"installed"} and defined $e->{"schema"}
!ll&&!rl&&r
1000
$e and $e->{"installed"}
!ll&&!rl&&r
0100
MC/DC0%0/3
$e and $e->{"installed"} and defined $e->{"schema"}
$e $e->{"installed"} defined $e->{"schema"}
Truth table25%1/4
A and B and C
A= $eB= $e->{"installed"}C= defined $e->{"schema"}
ABCresult
0XX0
10X0
1100
1111
135 0 ▶ next if $e->{schema} eq 'public';
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"schema"} eq "public" 0 0 0
136 0 ▶ next if defined $namespace && $e->{schema} eq $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace and $e->{"schema"} eq $namespace 0 0 0
Condition0%0/3
defined $namespace and $e->{"schema"} eq $namespace
!ll&&!rl&&r
000
MC/DC0%0/2
defined $namespace and $e->{"schema"} eq $namespace
defined $namespace $e->{"schema"} eq $namespace
Truth table0%0/3
A and B
A= defined $namespaceB= $e->{"schema"} eq $namespace
ABresult
0X0
100
111
137 0 $held{$name} = $e->{schema};
138 }
139 12 return \%held;
140 }
141  
142  
143 17 ▶sub _execute_ddl {
Subroutine _execute_ddl: called CC=10 SCAR=26.2
144 17 my ($self, $dbh, @rest) = @_;
145  
146 17 my $storage = $self->_driver_storage;
147 17 ▶ if ($storage && $storage->can('_declared_search_path')
Branch: 1 branch
BranchTrueFalseTotal
if ($storage and $storage->can("_declared_search_path") and not defined $storage->connection_schema) 14 3 17
Condition83%5/6
$storage and $storage->can("_declared_search_path") and not defined $storage->connection_schema
!ll&&!rl&&r
3014
$storage and $storage->can("_declared_search_path")
!ll&&!rl&&r
2114
MC/DC66%2/3
$storage and $storage->can("_declared_search_path") and not defined $storage->connection_schema
$storage $storage->can("_declared_search_path") not defined $storage->connection_schema
Truth table75%3/4
A and B and not(C)
A= $storageB= $storage->can("_declared_search_path")C= not defined $storage->connection_schema
ABCresult
0XX0
10X0
1100
1111
148 && !defined $storage->connection_schema) {
149 14 my @path = $storage->_declared_search_path;
150 # An extension the temporary database holds in a schema of its own
151 # (_installed_extension_schemas) is found behind the rest of the path, as
152 # the live session finds it; the default path ahead of it when none is
153 # declared.
154 14 my %once;
155 0 ▶ my @held = sort grep { !$once{$_}++ } values %{ $self->{_temp_extension_schemas} // {} };
Condition50%1/2
$self->{"_temp_extension_schemas"} // {}
l!l
014
MC/DC0%0/1
$self->{"_temp_extension_schemas"} // {}
$self->{"_temp_extension_schemas"}
Truth table50%1/2
A
A= $self->{"_temp_extension_schemas"}
Aresult
11
00
156 14 ▶ if (@held) {
Branch: 1 branch
BranchTrueFalseTotal
if (@held) 0 14 14
157 0 ▶ @path = ('$user', 'public') unless @path;
Branch: 1 branch
BranchTrueFalseTotal
unless @path 0 0 0
158 0 my %seen = map { $_ => 1 } @path;
159 0 push @path, grep { !$seen{$_}++ } @held;
160 }
161 # Asked last: it connects, and a schema with nothing to set needs none.
162 14 ▶ if (@path && Scalar::Util::refaddr($dbh) != (Scalar::Util::refaddr($self->_dbh) // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path and builtin::refaddr($dbh) != (builtin::refaddr($self->_dbh) // 0)) 1 13 14
Condition60%3/5
@path and builtin::refaddr($dbh) != (builtin::refaddr($self->_dbh) // 0)
!ll&&!rl&&r
1301
builtin::refaddr($self->_dbh) // 0
l!l
10
MC/DC50%1/2
@path and builtin::refaddr($dbh) != (builtin::refaddr($self->_dbh) // 0)
@path builtin::refaddr($dbh) != (builtin::refaddr($self->_dbh) // 0)
MC/DC0%0/1
builtin::refaddr($self->_dbh) // 0
builtin::refaddr($self->_dbh)
Truth table66%2/3
A and B
A= @pathB= builtin::refaddr($dbh) != (builtin::refaddr($self->_dbh) // 0)
ABresult
0X0
100
111
Truth table50%1/2
A
A= builtin::refaddr($self->_dbh)
Aresult
11
00
163 $dbh->do('SET search_path TO ' . join ', ',
164 1 map { DBIO::PostgreSQL::Util::quote_search_path_entry($_) } @path);
165 }
166 }
167  
168 17 return $self->SUPER::_execute_ddl($dbh, @rest);
169 }
170  
171  
172 22 ▶sub _extension_control_schemas {
Subroutine _extension_control_schemas: called CC=2 SCAR=6.9
173 22 my ($self) = @_;
174 22 my @declared = DBIO::PostgreSQL::DDL::_schema_decl($self->schema, 'pg_extensions');
175 22 ▶ return {} unless @declared;
Branch: 1 branch
BranchTrueFalseTotal
unless @declared 20 2 22
176  
177 2 my $rows = $self->_dbh->selectall_arrayref(
178 DBIO::PostgreSQL::DDL::control_schemas_sql(@declared));
179 2 my %fixed = map { $_->[0] => $_->[1] } @$rows;
180  
181 2 DBIO::PostgreSQL::DDL::check_control_schemas($self->schema, \%fixed);
182 1 return \%fixed;
183 }
184  
185  
186 2 ▶sub _install_ddl {
Subroutine _install_ddl: called CC=2 SCAR=7.2
187 2 my ($self) = @_;
188 return $self->_ddl_class->install_ddl($self->schema,
189 extension_control_schemas => $self->_extension_control_schemas,
190 2 ▶ extension_installed_schemas => $self->{_temp_extension_schemas} // {});
Condition50%1/2
$self->{"_temp_extension_schemas"} // {}
l!l
02
MC/DC0%0/1
$self->{"_temp_extension_schemas"} // {}
$self->{"_temp_extension_schemas"}
Truth table50%1/2
A
A= $self->{"_temp_extension_schemas"}
Aresult
11
00
191 }
192  
193  
194 9 ▶sub _check_extensions_available {
Subroutine _check_extensions_available: called CC=3 SCAR=11.0
195 9 my ($self) = @_;
196 9 my @declared = DBIO::PostgreSQL::DDL::_schema_decl($self->schema, 'pg_extensions');
197 9 ▶ return 1 unless @declared;
Branch: 1 branch
BranchTrueFalseTotal
unless @declared 5 4 9
198  
199 4 my $dbh = $self->_dbh;
200 4 my $available = $dbh->selectcol_arrayref(
201 'SELECT name FROM pg_catalog.pg_available_extensions'
202 );
203 4 my %available = map { $_ => 1 } @$available;
204  
205 4 my @missing = grep { !$available{$_} } @declared;
206 4 ▶ DBIO::Exception->throw($self->_missing_extension_message(@missing)) if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 3 1 4
207 1 return 1;
208 }
209  
210  
211 5 ▶sub _missing_extension_message {
Subroutine _missing_extension_message: called CC=1 SCAR=0.0
212 5 my ($class, @missing) = @_;
213 5 my $list = join ', ', map { qq{"$_"} } @missing;
214 my $fix = join "\n",
215 5 map { ' CREATE EXTENSION ' . DBIO::PostgreSQL::Util::quote_ident($_) . ';' } @missing;
216 5 return "Declared PostgreSQL extension(s) not available on this server: $list\n"
217 . "Install them on the server (e.g. as a superuser) and retry:\n"
218 . "$fix\n"
219 . "The deploy needs these extensions to exist before it can create "
220 . "extension-backed types (e.g. vector(N) columns).";
221 }
222  
223 # --- PostgreSQL-specific overrides -----------------------------------------
224  
225  
226 3 ▶sub _new_introspect {
Subroutine _new_introspect: called CC=1 SCAR=0.0
227 3 my ($self, $dbh) = @_;
228 3 return $self->_introspect_class->new(
229 dbh => $dbh,
230 schema_filter => $self->_schema_filter,
231 qualified_names => 1,
232 );
233 }
234  
235  
236 0 ▶sub _build_target_model {
Subroutine _build_target_model: not called CC=1 SCAR=6.9
237 0 my ($self) = @_;
238 0 return $self->_mark_namespace(
239 $self->_mark_extension_schemas($self->SUPER::_build_target_model));
240 }
241  
242  
243 12 ▶sub _mark_extension_schemas {
Subroutine _mark_extension_schemas: called CC=13 SCAR=25.8
244 12 my ($self, $model) = @_;
245 12 ▶ my $declared = DBIO::PostgreSQL::DDL::_schema_decl($self->schema, 'pg_extension_schemas') // {};
Condition50%1/2
DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas") // {}
l!l
120
MC/DC0%0/1
DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas") // {}
DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas")
Truth table50%1/2
A
A= DBIO::PostgreSQL::DDL::_schema_decl($self->schema, "pg_extension_schemas")
Aresult
11
00
246 12 for my $name (keys %$declared) {
247 7 ▶ my $entry = $model->{extensions}{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $model->{"extensions"}{$name} or next 7 0 7
248 7 $entry->{declared_schema} = $declared->{$name};
249 }
250 12 my $fixed = $self->_extension_control_schemas;
251 12 my $held = $self->_installed_extension_schemas;
252 12 my $namespace = DBIO::PostgreSQL::DDL::_connection_namespace($self->schema);
253 12 for my $name (DBIO::PostgreSQL::DDL::_schema_decl($self->schema, 'pg_extensions')) {
254 21 ▶ my $entry = $model->{extensions}{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $model->{"extensions"}{$name} or next 21 0 21
255 21 ▶ if (defined $declared->{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $declared->{$name}) { } 7 14 21
elsif (defined $fixed->{$name}) { } 4 10 14
256 7 $entry->{install_schema} = $declared->{$name};
257 }
258 elsif (defined $fixed->{$name}) {
259 4 $entry->{control_schema} = $fixed->{$name};
260 }
261 else {
262 10 ▶ $entry->{install_schema} = $held->{$name} // 'public';
Condition50%1/2
$held->{$name} // "public"
l!l
010
MC/DC0%0/1
$held->{$name} // "public"
$held->{$name}
Truth table50%1/2
A
A= $held->{$name}
Aresult
11
00
263 }
264 # The deploy of a connection that names its namespace sees that
265 # namespace alone, so it cannot know whether the schema of the extension
266 # is there (karr k131): the create makes it.
267 21 ▶ my $in = $entry->{declared_schema} // $entry->{install_schema};
Condition100%3/3
$entry->{"declared_schema"} // $entry->{"install_schema"}
l!l&&r!l&&!r
7104
MC/DC100%2/2
$entry->{"declared_schema"} // $entry->{"install_schema"}
$entry->{"declared_schema"} $entry->{"install_schema"}
Truth table100%3/3
A // B
A= $entry->{"declared_schema"}B= $entry->{"install_schema"}
ABresult
1X1
011
000
268 21 ▶ $entry->{create_schema} = 1 if defined $namespace && defined $in && $in ne 'public';
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace and defined $in and $in ne "public" 2 19 21
Condition100%6/6
defined $namespace and defined $in and $in ne "public"
!ll&&!rl&&r
1632
defined $namespace and defined $in
!ll&&!rl&&r
1515
MC/DC100%3/3
defined $namespace and defined $in and $in ne "public"
defined $namespace defined $in $in ne "public"
Truth table100%4/4
A and B and C
A= defined $namespaceB= defined $inC= $in ne "public"
ABCresult
0XX0
10X0
1100
1111
269 }
270 12 return $model;
271 }
272  
273  
274 1 ▶sub _mark_namespace {
Subroutine _mark_namespace: called CC=4 SCAR=16.4
275 1 my ($self, $model) = @_;
276 1 my $namespace = DBIO::PostgreSQL::DDL::_connection_namespace($self->schema);
277 $model->{schemas}{$namespace}{create_if_missing} = 1
278 1 ▶ if defined $namespace && $model->{schemas} && $model->{schemas}{$namespace};
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace and $model->{"schemas"} and $model->{"schemas"}{$namespace} 1 0 1
Condition33%2/6
defined $namespace and $model->{"schemas"} and $model->{"schemas"}{$namespace}
!ll&&!rl&&r
001
defined $namespace and $model->{"schemas"}
!ll&&!rl&&r
001
MC/DC0%0/3
defined $namespace and $model->{"schemas"} and $model->{"schemas"}{$namespace}
defined $namespace $model->{"schemas"} $model->{"schemas"}{$namespace}
Truth table25%1/4
A and B and C
A= defined $namespaceB= $model->{"schemas"}C= $model->{"schemas"}{$namespace}
ABCresult
0XX0
10X0
1100
1111
279 1 return $model;
280 }
281  
282  
283 53 ▶sub _schema_filter {
Subroutine _schema_filter: called CC=3 SCAR=11.0
284 53 my ($self) = @_;
285 53 my $namespace = DBIO::PostgreSQL::DDL::_connection_namespace($self->schema);
286 53 ▶ return [ $namespace ] if defined $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace 7 46 53
287 46 my @schemas = DBIO::PostgreSQL::DDL::_schema_decl($self->schema, 'pg_schemas');
288 46 ▶ return @schemas ? \@schemas : undef;
Branch: 1 branch
BranchTrueFalseTotal
@schemas ? : 19 27 46
289 }
290  
291  
292 32 ▶sub diff {
Subroutine diff: called CC=8 SCAR=20.8
Pod documented
293 32 my ($self, $opts) = @_;
294 32 $self->_extension_control_schemas; # a refusal, before a temporary database is made
295 # The option stands in new, ahead of the first operations call; a value the
296 # caller gave stays. A non-hash $opts goes to the core, which refuses it.
297 31 ▶ $opts = { foreign_keys_not_valid => 1, %{ $opts // {} } }
Branch: 1 branch
BranchTrueFalseTotal
if $self->foreign_keys_not_valid and !defined($opts) || ref $opts eq "HASH" 3 28 31
Condition75%6/8
$self->foreign_keys_not_valid and !defined($opts) || ref $opts eq "HASH"
!ll&&!rl&&r
2803
not defined $opts or ref $opts eq "HASH"
l!l&&r!l&&!r
210
$opts // {}
l!l
12
MC/DC33%1/3
$self->foreign_keys_not_valid and !defined($opts) || ref $opts eq "HASH"
$self->foreign_keys_not_valid not defined $opts ref $opts eq "HASH"
MC/DC100%1/1
$opts // {}
$opts
Truth table75%3/4
A and not(B) or C
A= $self->foreign_keys_not_validB= not defined $optsC= ref $opts eq "HASH"
ABCresult
0XX0
1000
11X1
1011
Truth table100%2/2
A
A= $opts
Aresult
11
00
298 if $self->foreign_keys_not_valid && (!defined $opts || ref $opts eq 'HASH');
299 31 my $diff = $self->SUPER::diff($opts);
300 28 $diff->operations;
301 27 ▶ return $diff unless $self->_schema_filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_schema_filter 14 13 27
302 13 ▶ return $diff if $self->manage_extensions_when_restricted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->manage_extensions_when_restricted 2 11 13
303 return $self->_diff_class->new(
304 source => $diff->source,
305 target => $diff->target,
306 kept_foreign_keys => $diff->{kept_foreign_keys},
307 operations => [
308 26 ▶ grep { !($_->isa('DBIO::PostgreSQL::Diff::Extension') && $_->action eq 'drop') }
Condition100%3/3
$_->isa('DBIO::PostgreSQL::Diff::Extension') && $_->action eq 'drop'
!ll&&!rl&&r
3176
MC/DC100%2/2
$_->isa('DBIO::PostgreSQL::Diff::Extension') && $_->action eq 'drop'
$_->isa('DBIO::PostgreSQL::Diff::Extension') $_->action eq 'drop'
Truth table100%3/3
A && B
A= $_->isa('DBIO::PostgreSQL::Diff::Extension')B= $_->action eq 'drop'
ABresult
0X0
100
111
309 11 @{ $diff->operations }
310 ],
311 );
312 }
313  
314  
315 1 ▶sub _check_before_apply {
Subroutine _check_before_apply: called CC=1 SCAR=0.0
316 1 my ($self, $diff) = @_;
317 1 $self->_refuse_unfillable($diff, $self->_dbh);
318 1 return $self->SUPER::_check_before_apply($diff);
319 }
320  
321  
322 1 ▶sub _refuse_unfillable {
Subroutine _refuse_unfillable: called CC=16 SCAR=52.8
323 1 my ($self, $diff, $dbh) = @_;
324  
325 1 for my $op (@{ $diff->operations }) {
326 7 ▶ next unless $op->isa('DBIO::PostgreSQL::Diff::Column');
Branch: 1 branch
BranchTrueFalseTotal
unless $op->isa("DBIO::PostgreSQL::Diff::Column") 7 0 7
327 0 ▶ my $new = $op->new_info or next;
Branch: 1 branch
BranchTrueFalseTotal
my $new = $op->new_info or next 0 0 0
328 0 ▶ next unless $new->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
unless $new->{"not_null"} 0 0 0
329  
330 my $adds = $op->action eq 'add'
331 && !(defined $new->{default_value} && length $new->{default_value})
332 && !DBIO::PostgreSQL::Introspect->identity_kind($new->{identity})
333 0 ▶ && !DBIO::PostgreSQL::Introspect->generated_kind($new->{generated});
Condition0%0/12
$op->action eq 'add' && !(defined $new->{'default_value'} && length $new->{'default_value'}) && !'DBIO::PostgreSQL::Introspect'->identity_kind($new->{'identity'}) && !'DBIO::PostgreSQL::Introspect'->generated_kind($new->{'generated'})
!ll&&!rl&&r
000
$op->action eq "add" and not defined $new->{"default_value"} && length $new->{"default_value"} and not "DBIO::PostgreSQL::Introspect"->identity_kind($new->{"identity"})
!ll&&!rl&&r
000
$op->action eq "add" and not defined $new->{"default_value"} && length $new->{"default_value"}
!ll&&!rl&&r
000
defined $new->{'default_value'} && length $new->{'default_value'}
!ll&&!rl&&r
000
MC/DC0%0/5
$op->action eq 'add' && !(defined $new->{'default_value'} && length $new->{'default_value'}) && !'DBIO::PostgreSQL::Introspect'->identity_kind($new->{'identity'}) && !'DBIO::PostgreSQL::Introspect'->generated_kind($new->{'generated'})
$op->action eq "add" defined $new->{'default_value'} length $new->{'default_value'} not "DBIO::PostgreSQL::Introspect"->identity_kind($new->{"identity"}) !'DBIO::PostgreSQL::Introspect'->generated_kind($new->{'generated'})
Truth table0%0/8
A and not(B && C) and not(D) && not(E)
A= $op->action eq "add"B= defined $new->{'default_value'}C= length $new->{'default_value'}D= not "DBIO::PostgreSQL::Introspect"->identity_kind($new->{"identity"})E= !'DBIO::PostgreSQL::Introspect'->generated_kind($new->{'generated'})
ABCDEresult
0XXXX0
111XX0
10X0X0
1100X0
10X100
110100
10X111
110111
334 0 ▶ my $sets = $op->action eq 'alter' && $op->old_info && !$op->old_info->{not_null};
Condition0%0/6
$op->action eq 'alter' && $op->old_info && !$op->old_info->{'not_null'}
!ll&&!rl&&r
000
$op->action eq "alter" and $op->old_info
!ll&&!rl&&r
000
MC/DC0%0/3
$op->action eq 'alter' && $op->old_info && !$op->old_info->{'not_null'}
$op->action eq "alter" $op->old_info !$op->old_info->{'not_null'}
Truth table0%0/4
A and B && not(C)
A= $op->action eq "alter"B= $op->old_infoC= !$op->old_info->{'not_null'}
ABCresult
0XX0
10X0
1100
1111
335 0 ▶ next unless $adds || $sets;
Branch: 1 branch
BranchTrueFalseTotal
unless $adds or $sets 0 0 0
Condition0%0/3
$adds or $sets
l!l&&r!l&&!r
000
MC/DC0%0/2
$adds or $sets
$adds $sets
Truth table0%0/3
A or B
A= $addsB= $sets
ABresult
1X1
011
000
336  
337 0 my @names = DBIO::PostgreSQL::Introspect->key_names($op->table_key, table_name => $op);
338 0 my $table = DBIO::PostgreSQL::Util::quote_qualified(@names);
339 0 my $label = join '.', @names;
340 0 my $column = $op->column_name;
341  
342 0 ▶ if ($adds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adds) 0 0 0
343 0 ▶ next unless $dbh->selectrow_array("SELECT 1 FROM $table LIMIT 1");
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->selectrow_array("SELECT 1 FROM $table LIMIT 1") 0 0 0
344 0 DBIO::Exception->throw(sprintf
345 'PostgreSQL refuses to add the column %s.%s: it is NOT NULL without a '
346 . 'default, and table %s holds rows that would have no value for it',
347 $label, $column, $label);
348 }
349 0 ▶ next unless $dbh->selectrow_array(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->selectrow_array(sprintf("SELECT 1 FROM %s WHERE %s IS NULL LIMIT 1", $table, DBIO::PostgreSQL::Util::quote_ident($column))) 0 0 0
350 'SELECT 1 FROM %s WHERE %s IS NULL LIMIT 1',
351 $table, DBIO::PostgreSQL::Util::quote_ident($column));
352 0 DBIO::Exception->throw(sprintf
353 'PostgreSQL refuses to make the column %s.%s NOT NULL: table %s holds '
354 . 'rows where it is NULL',
355 $label, $column, $label);
356 }
357  
358 1 return;
359 }
360  
361  
362 43 ▶sub _resolve_extension_list {
Subroutine _resolve_extension_list: called CC=1 SCAR=0.0
363 43 my ($self) = @_;
364 43 return DBIO::PostgreSQL::Extension->resolve_list($self->schema);
365 }
366  
367  
368 31 ▶sub _extension_list {
Subroutine _extension_list: called CC=2 SCAR=6.9
369 31 my ($self) = @_;
370 31 ▶ my $list = $self->_resolve_extension_list or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $list = $self->_resolve_extension_list or return [] 16 15 31
371  
372 16 $list->set_installed($self->_installed_extensions([ $list->names ]));
373  
374 16 return [ $list->entries ];
375 }
376  
377  
378 0 ▶sub _installed_extensions {
Subroutine _installed_extensions: not called CC=2 SCAR=17.9
379 0 my ($self, $names) = @_;
380 0 my $rows = $self->_dbh->selectall_arrayref(q{
381 SELECT e.extname AS name, e.extversion AS version, n.nspname AS schema
382 FROM pg_catalog.pg_extension e
383 JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace
384 WHERE e.extname = ANY($1)
385 }, { Slice => {} }, $names);
386  
387 0 my %state = map { $_ => { installed => 0 } } @$names;
388 $state{ $_->{name} } = { installed => 1, version => $_->{version}, schema => $_->{schema} }
389 0 for @$rows;
390 0 return \%state;
391 }
392  
393  
394 43 ▶sub _extension_driver_helpers {
Subroutine _extension_driver_helpers: called CC=1 SCAR=0.0
395 43 return DBIO::PostgreSQL::Extension->driver_helpers;
396 }
397  
398  
399 41 ▶sub _extension_connection_schema {
Subroutine _extension_connection_schema: called CC=2 SCAR=6.9
400 41 my ($self) = @_;
401 41 ▶ return undef unless $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"schema"} 25 16 41
402 16 return DBIO::PostgreSQL::DDL::_connection_namespace($self->schema);
403 }
404  
405  
406 41 ▶sub _extension_restricted {
Subroutine _extension_restricted: called CC=3 SCAR=11.0
407 41 my ($self) = @_;
408 41 ▶ return 0 unless $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"schema"} 25 16 41
409 16 ▶ return defined $self->_schema_filter ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_schema_filter ? : 7 9 16
410 }
411  
412 # --- extension protocol: applying the answers to a model ----------------------
413  
414 66 my %_CLAIM = (
415 schema => \&_claim_schema,
416 table => \&_claim_table,
417 column => \&_claim_column,
418 type => \&_claim_type,
419 );
420  
421 2 ▶sub _unknown_address {
Subroutine _unknown_address: called CC=2 SCAR=7.1
422 2 my ($what, $address) = @_;
423 DBIO::Exception->throw(sprintf
424 "%s: the address [%s] is of a kind this deploy does not know (schema, table, column, type)",
425 2 ▶ $what, join(', ', map { defined $_ ? $_ : 'undef' } @$address));
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 4 0 4
426 }
427  
428  
429 58 ▶sub _apply_extension_claims {
Subroutine _apply_extension_claims: called CC=4 SCAR=13.9
430 58 my ($self, $model, $addresses) = @_;
431 58 for my $address (@$addresses) {
432 66 my ($kind, @args) = @$address;
433 66 ▶ my $claim = $_CLAIM{$kind // ''} or _unknown_address('extension claim', $address);
Branch: 1 branch
BranchTrueFalseTotal
my $claim = $_CLAIM{$kind // ""} or _unknown_address("extension claim", $address) 65 1 66
Condition50%1/2
$kind // ""
l!l
660
MC/DC0%0/1
$kind // ""
$kind
Truth table50%1/2
A
A= $kind
Aresult
11
00
434 65 $claim->($model, @args);
435 }
436 57 return;
437 }
438  
439 53 ▶sub _claim_schema {
Subroutine _claim_schema: called CC=9 SCAR=22.6
440 53 my ($model, $name) = @_;
441 53 ▶ delete $model->{schemas}{$name} if $model->{schemas};
Branch: 1 branch
BranchTrueFalseTotal
if $model->{"schemas"} 53 0 53
442 53 ▶ my @tables = grep { ($model->{tables}{$_}{schema_name} // '') eq $name } keys %{ $model->{tables} || {} };
Condition50%2/4
$model->{"tables"}{$_}{"schema_name"} // ""
l!l
1200
$model->{'tables'} || {}
l!l
530
MC/DC0%0/1
$model->{"tables"}{$_}{"schema_name"} // ""
$model->{"tables"}{$_}{"schema_name"}
MC/DC0%0/1
$model->{'tables'} || {}
$model->{'tables'}
Truth table50%1/2
A
A= $model->{"tables"}{$_}{"schema_name"}
Aresult
11
00
Truth table50%1/2
A
A= $model->{'tables'}
Aresult
11
00
443 53 _claim_table($model, $_) for sort @tables;
444 53 for my $section (qw( types functions sequences )) {
445 159 ▶ my $objects = $model->{$section} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $objects = $model->{$section} or next 27 132 159
446 delete $objects->{$_}
447 27 ▶ for grep { ($objects->{$_}{schema_name} // '') eq $name } keys %$objects;
Condition50%1/2
$objects->{$_}{"schema_name"} // ""
l!l
610
MC/DC0%0/1
$objects->{$_}{"schema_name"} // ""
$objects->{$_}{"schema_name"}
Truth table50%1/2
A
A= $objects->{$_}{"schema_name"}
Aresult
11
00
448 }
449 53 return;
450 }
451  
452 # The sequences that belong to the table, or to one of its columns.
453 67 ▶sub _owned_sequences {
Subroutine _owned_sequences: called CC=7 SCAR=19.5
454 67 my ($model, $table_key, $column) = @_;
455 67 ▶ my $table = $model->{tables}{$table_key} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $table = $model->{"tables"}{$table_key} or return () 64 3 67
456 64 my $quoted = DBIO::PostgreSQL::Util::quote_qualified($table->{schema_name}, $table->{table_name});
457 64 ▶ my $sequences = $model->{sequences} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $sequences = $model->{"sequences"} or return () 20 44 64
458 return grep {
459 20 my $s = $sequences->{$_};
460 defined $s->{owned_by_table} && $s->{owned_by_table} eq $quoted
461 77 ▶ && (!defined $column || ($s->{owned_by_column} // '') eq $column)
Condition90%10/11
defined $s->{'owned_by_table'} && $s->{'owned_by_table'} eq $quoted && (!defined($column) || ($s->{'owned_by_column'} // '') eq $column)
!ll&&!rl&&r
62510
defined $s->{"owned_by_table"} and $s->{"owned_by_table"} eq $quoted
!ll&&!rl&&r
194315
not defined $column or ($s->{"owned_by_column"} // "") eq $column
l!l&&r!l&&!r
915
$s->{"owned_by_column"} // ""
l!l
60
MC/DC50%2/4
defined $s->{'owned_by_table'} && $s->{'owned_by_table'} eq $quoted && (!defined($column) || ($s->{'owned_by_column'} // '') eq $column)
defined $s->{"owned_by_table"} $s->{"owned_by_table"} eq $quoted not defined $column $s->{"owned_by_column"}
Truth table80%4/5
A and B && not(C) or D
A= defined $s->{"owned_by_table"}B= $s->{"owned_by_table"} eq $quotedC= not defined $columnD= $s->{"owned_by_column"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
462 } keys %$sequences;
463 }
464  
465 63 ▶sub _claim_table {
Subroutine _claim_table: called CC=3 SCAR=11.0
466 63 my ($model, $key) = @_;
467 63 delete $model->{sequences}{$_} for _owned_sequences($model, $key);
468 delete $model->{$_}{$key}
469 63 for grep { $model->{$_} } qw( tables columns indexes triggers policies foreign_keys check_constraints );
470 63 return;
471 }
472  
473 4 ▶sub _claim_column {
Subroutine _claim_column: called CC=3 SCAR=11.0
474 4 my ($model, $table_key, $column) = @_;
475 4 delete $model->{sequences}{$_} for _owned_sequences($model, $table_key, $column);
476 4 ▶ my $columns = $model->{columns}{$table_key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $columns = $model->{"columns"}{$table_key} or return 3 1 4
477 3 @$columns = grep { $_->{column_name} ne $column } @$columns;
478 3 return;
479 }
480  
481 2 ▶sub _claim_type {
Subroutine _claim_type: called CC=2 SCAR=7.1
482 2 my ($model, $key) = @_;
483 2 ▶ delete $model->{types}{$key} if $model->{types};
Branch: 1 branch
BranchTrueFalseTotal
if $model->{"types"} 2 0 2
484 2 return;
485 }
486  
487  
488 44 ▶sub _apply_extension_annotations {
Subroutine _apply_extension_annotations: called CC=6 SCAR=17.9
489 44 my ($self, $model, $triples) = @_;
490 44 for my $triple (@$triples) {
491 86 my ($address, $attribute, $value) = @$triple;
492 86 my ($kind, @args) = @$address;
493 86 ▶ my $object = $self->_annotated_object($model, $address) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $object = $self->_annotated_object($model, $address) or next 83 2 85
494 83 my $where = join '.', @args;
495 DBIO::Exception->throw(sprintf
496 "extension annotation: %s '%s' already has the attribute '%s' with another value (an extension adds to an object, it does not rewrite what the introspection read)",
497 $kind, $where, $attribute)
498 83 ▶ if exists $object->{$attribute} && _differs($object->{$attribute}, $value);
Branch: 1 branch
BranchTrueFalseTotal
if exists $object->{$attribute} and _differs($object->{$attribute}, $value) 3 80 83
Condition100%3/3
exists $object->{$attribute} and _differs($object->{$attribute}, $value)
!ll&&!rl&&r
7913
MC/DC100%2/2
exists $object->{$attribute} and _differs($object->{$attribute}, $value)
exists $object->{$attribute} _differs($object->{$attribute}, $value)
Truth table100%3/3
A and B
A= exists $object->{$attribute}B= _differs($object->{$attribute}, $value)
ABresult
0X0
100
111
499 80 ▶ $object->{$attribute} = ref $value ? Storable::dclone($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 1 79 80
500 }
501 40 return;
502 }
503  
504 4 ▶sub _differs {
Subroutine _differs: called CC=1 SCAR=0.0
505 4 my ($have, $want) = @_;
506 4 local $Storable::canonical = 1;
507 4 return Storable::nfreeze(\$have) ne Storable::nfreeze(\$want);
508 }
509  
510 # The model's entry of the object an address names, or undef when the model
511 # does not hold it.
512 86 ▶sub _annotated_object {
Subroutine _annotated_object: called CC=11 SCAR=24.8
513 86 my ($self, $model, $address) = @_;
514 86 my ($kind, @args) = @$address;
515 86 ▶ return $model->{schemas}{ $args[0] } if $kind eq 'schema' && $model->{schemas};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "schema" and $model->{"schemas"} 1 85 86
Condition66%2/3
$kind eq "schema" and $model->{"schemas"}
!ll&&!rl&&r
8501
MC/DC50%1/2
$kind eq "schema" and $model->{"schemas"}
$kind eq "schema" $model->{"schemas"}
Truth table66%2/3
A and B
A= $kind eq "schema"B= $model->{"schemas"}
ABresult
0X0
100
111
516 85 ▶ return $model->{tables}{ $args[0] } if $kind eq 'table' && $model->{tables};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "table" and $model->{"tables"} 2 83 85
Condition66%2/3
$kind eq "table" and $model->{"tables"}
!ll&&!rl&&r
8302
MC/DC50%1/2
$kind eq "table" and $model->{"tables"}
$kind eq "table" $model->{"tables"}
Truth table66%2/3
A and B
A= $kind eq "table"B= $model->{"tables"}
ABresult
0X0
100
111
517 83 ▶ return $model->{types}{ $args[0] } if $kind eq 'type' && $model->{types};
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "type" and $model->{"types"} 1 82 83
Condition66%2/3
$kind eq "type" and $model->{"types"}
!ll&&!rl&&r
8201
MC/DC50%1/2
$kind eq "type" and $model->{"types"}
$kind eq "type" $model->{"types"}
Truth table66%2/3
A and B
A= $kind eq "type"B= $model->{"types"}
ABresult
0X0
100
111
518 82 ▶ if ($kind eq 'column') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "column") 81 1 82
519 81 ▶ my ($found) = grep { $_->{column_name} eq $args[1] } @{ $model->{columns}{ $args[0] } || [] };
Condition50%1/2
$model->{'columns'}{$args[0]} || []
l!l
810
MC/DC0%0/1
$model->{'columns'}{$args[0]} || []
$model->{'columns'}{$args[0]}
Truth table50%1/2
A
A= $model->{'columns'}{$args[0]}
Aresult
11
00
520 81 return $found;
521 }
522 1 ▶ _unknown_address('extension annotation', $address) unless $_CLAIM{$kind // ''};
Branch: 1 branch
BranchTrueFalseTotal
unless $_CLAIM{$kind // ""} 1 0 1
Condition50%1/2
$kind // ""
l!l
10
MC/DC0%0/1
$kind // ""
$kind
Truth table50%1/2
A
A= $kind
Aresult
11
00
523 0 return undef;
524 }
525  
526  
527 47 ▶sub _apply_extension_states {
Subroutine _apply_extension_states: called CC=1 SCAR=0.0
528 47 my ($self, $model, $states) = @_;
529 47 $model->{extension_states} = $states;
530 47 _lock_deep($model->{extension_states});
531 47 return;
532 }
533  
534 # Read-only all the way down: hashes (keys and values) and the arrays in them.
535 278 ▶sub _lock_deep {
Subroutine _lock_deep: called CC=6 SCAR=17.9
536 278 my ($x) = @_;
537 278 ▶ if (ref $x eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $x eq 'HASH') { } 139 139 278
elsif (ref $x eq 'ARRAY') { } 47 92 139
538 139 _lock_deep($_) for values %$x;
539 139 Hash::Util::lock_hashref($x);
540 }
541 elsif (ref $x eq 'ARRAY') {
542 47 _lock_deep($_) for @$x;
543 47 Internals::SvREADONLY($_, 1) for @$x;
544 47 Internals::SvREADONLY(@$x, 1);
545 }
546 278 return;
547 }
548  
549  
550 9 ▶sub _temp_dsn {
Subroutine _temp_dsn: called CC=11 SCAR=24.2
551 9 my ($self, $dsn, $temp_db) = @_;
552 9 my $quoted = DBIO::PostgreSQL::Util::quote_dsn_value($temp_db);
553  
554 9 my ($prefix) = $dsn =~ /\A(dbi:\w*(?:\([^)]*\))?:)/i;
555 9 ▶ $prefix //= '';
Condition100%2/2
$prefix //= ""
l!l
81
MC/DC100%1/1
$prefix //= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
556 9 my $rest = substr($dsn, length $prefix);
557  
558 9 my $out;
559 9 ▶ if ($rest =~ m{\A(\s*postgres(?:ql)?://[^/?#]*)(?:/[^?#]*)?(?:\?([^#]*))?(\#.*)?\z}is) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ m[\A(\s*postgres(?:ql)?://[^/?#]*)(?:/[^?#]*)?(?:\?([^#]*))?(\#.*)?\z]is) { } 1 8 9
560 1 my ($authority, $query, $fragment) = ($1, $2, $3);
561 1 ▶ my @query = grep { !/\A(?:dbname|database|db)=/i } split /&/, $query // '';
Condition50%1/2
$query // ""
l!l
10
MC/DC0%0/1
$query // ""
$query
Truth table50%1/2
A
A= $query
Aresult
11
00
562 0 (my $path = $temp_db) =~ s/([^A-Za-z0-9._~-])/sprintf '%%%02X', ord $1/ge;
563 1 ▶ $out = $prefix . "$authority/$path"
Branch: 1 branch
BranchTrueFalseTotal
@query ? : 1 0 1
Condition50%1/2
$fragment // ""
l!l
01
MC/DC0%0/1
$fragment // ""
$fragment
Truth table50%1/2
A
A= $fragment
Aresult
11
00
564 . (@query ? '?' . join('&', @query) : '') . ($fragment // '');
565 }
566 else {
567 8 my @params;
568 8 pos($rest) = 0;
569 8 ▶ while ($rest =~ /\G[\s;]*([^\s;='\\]+)[\s;]*=[\s;]*((?:'(?:[^'\\]|\\.)*'|(?:\\.|[^\s;'\\])+)?)/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\G[\s;]*([^\s;='\\]+)[\s;]*=[\s;]*((?:'(?:[^'\\]|\\.)*'|(?:\\.|[^\s;'\\])+)?)/cgs) 16 8 24
570 16 my ($key, $value) = ($1, $2);
571 16 ▶ next if grep { lc $key eq $_ } qw(db database dbname);
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $key eq $_;} "db", "database", "dbname" 6 10 16
572 10 push @params, "$key=$value";
573 }
574 8 $rest =~ /\G[\s;]*/gc;
575 8 ▶ DBIO::Exception->throw('_temp_dsn: the DSN of the live connection is not a list of key=value parameters')
Branch: 1 branch
BranchTrueFalseTotal
unless (pos $rest // 0) == length $rest 1 7 8
Condition50%1/2
pos $rest // 0
l!l
80
MC/DC0%0/1
pos $rest // 0
pos $rest
Truth table50%1/2
A
A= pos $rest
Aresult
11
00
576 unless (pos($rest) // 0) == length $rest;
577 7 $out = $prefix . join(';', @params, "dbname=$quoted");
578 }
579  
580 8 ▶ DBIO::Exception->throw('_temp_dsn: the DSN of the temporary database names no target')
Branch: 1 branch
BranchTrueFalseTotal
unless DBIO::PostgreSQL::Util::dsn_names_target($out) 0 8 8
581 unless DBIO::PostgreSQL::Util::dsn_names_target($out);
582 8 return $out;
583 }
584  
585  
586 12 ▶sub _refuse_open_transaction {
Subroutine _refuse_open_transaction: called CC=2 SCAR=6.9
587 12 my ($self, $dbh, $method) = @_;
588 12 ▶ return if $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"AutoCommit"} 10 2 12
589 2 DBIO::Exception->throw(sprintf
590 '%s: the handle is inside a transaction (AutoCommit is off) and CREATE/DROP DATABASE cannot run in one; '
591 . 'no statement was run. DBIO::Deploy::Base::TempDatabase passes a handle with AutoCommit on (_admin_dbh); pass one like it',
592 $method);
593 }
594  
595 9 ▶sub _create_temp_db {
Subroutine _create_temp_db: called CC=3 SCAR=11.1
596 9 my ($self, $dbh) = @_;
597 9 my $name = $self->temp_db_prefix . $$ . '_' . time() . '_'
598 . unpack('H8', Crypt::URandom::urandom(4));
599 9 ▶ DBIO::Exception->throw('_create_temp_db: temp_db_prefix leaves the database name longer than 63 bytes')
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 63 0 9 9
600 if length($name) > 63;
601 9 $self->_refuse_open_transaction($dbh, '_create_temp_db');
602 8 my $version = $dbh->{pg_server_version};
603 8 ▶ my $row = $dbh->selectrow_hashref($self->_locale_catalog_sql($version))
Branch: 1 branch
BranchTrueFalseTotal
my $row = $dbh->selectrow_hashref($self->_locale_catalog_sql($version)) or "DBIO::Exception"->throw("_create_temp_db: the live database is not in pg_database") 8 0 8
604 or DBIO::Exception->throw('_create_temp_db: the live database is not in pg_database');
605 8 $dbh->do($self->_create_database_sql($name, $version, $row));
606 8 return $name;
607 }
608  
609  
610 12 ▶sub _locale_catalog_sql {
Subroutine _locale_catalog_sql: called CC=5 SCAR=16.1
611 12 my ($self, $version) = @_;
612 12 ▶ my $provider = $version >= 150000 ? 'datlocprovider' : 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
$version >= 150000 ? : 3 9 12
613 12 ▶ my $locale = $version >= 170000 ? 'datlocale'
Branch: 2 branches
BranchTrueFalseTotal
$version >= 170000 ? : 1 11 12
$version >= 150000 ? : 2 9 11
614 : $version >= 150000 ? 'daticulocale' : 'NULL';
615 12 ▶ my $rules = $version >= 160000 ? 'daticurules' : 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
$version >= 160000 ? : 2 10 12
616 12 return 'SELECT pg_catalog.pg_encoding_to_char(encoding) AS encoding,'
617 . " datcollate AS collate, datctype AS ctype,"
618 . " $provider AS provider, $locale AS locale, $rules AS icu_rules"
619 . ' FROM pg_catalog.pg_database WHERE datname = pg_catalog.current_database()';
620 }
621  
622  
623 66 my %PROVIDER = (c => 'libc', i => 'icu', b => 'builtin');
624  
625 19 ▶sub _create_database_sql {
Subroutine _create_database_sql: called CC=11 SCAR=24.2
626 19 my ($self, $name, $version, $row) = @_;
627 19 my $q = \&DBIO::PostgreSQL::Util::quote_literal;
628 my @sql = (
629 'CREATE DATABASE ' . DBIO::PostgreSQL::Util::quote_ident($name),
630 'TEMPLATE template0',
631 'ENCODING ' . $q->($row->{encoding}),
632 'LC_COLLATE ' . $q->($row->{collate}),
633 19 'LC_CTYPE ' . $q->($row->{ctype}),
634 );
635 19 ▶ if ($version >= 150000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 150000) 7 12 19
636 7 ▶ my $provider = $PROVIDER{ $row->{provider} // 'c' }
Branch: 1 branch
BranchTrueFalseTotal
my $provider = $PROVIDER{$row->{"provider"} // "c"} or "DBIO::Exception"->throw(qq[_create_database_sql: unknown locale provider '$row->{"provider"}' in pg_database]) 7 0 7
Condition50%1/2
$row->{"provider"} // "c"
l!l
70
MC/DC0%0/1
$row->{"provider"} // "c"
$row->{"provider"}
Truth table50%1/2
A
A= $row->{"provider"}
Aresult
11
00
637 or DBIO::Exception->throw(
638 "_create_database_sql: unknown locale provider '$row->{provider}' in pg_database");
639 7 push @sql, "LOCALE_PROVIDER $provider";
640 push @sql, 'ICU_LOCALE ' . $q->($row->{locale})
641 7 ▶ if $provider eq 'icu' && defined $row->{locale};
Branch: 1 branch
BranchTrueFalseTotal
if $provider eq "icu" and defined $row->{"locale"} 5 2 7
Condition66%2/3
$provider eq "icu" and defined $row->{"locale"}
!ll&&!rl&&r
205
MC/DC50%1/2
$provider eq "icu" and defined $row->{"locale"}
$provider eq "icu" defined $row->{"locale"}
Truth table66%2/3
A and B
A= $provider eq "icu"B= defined $row->{"locale"}
ABresult
0X0
100
111
642 push @sql, 'BUILTIN_LOCALE ' . $q->($row->{locale})
643 7 ▶ if $provider eq 'builtin' && defined $row->{locale};
Branch: 1 branch
BranchTrueFalseTotal
if $provider eq "builtin" and defined $row->{"locale"} 1 6 7
Condition66%2/3
$provider eq "builtin" and defined $row->{"locale"}
!ll&&!rl&&r
601
MC/DC50%1/2
$provider eq "builtin" and defined $row->{"locale"}
$provider eq "builtin" defined $row->{"locale"}
Truth table66%2/3
A and B
A= $provider eq "builtin"B= defined $row->{"locale"}
ABresult
0X0
100
111
644 push @sql, 'ICU_RULES ' . $q->($row->{icu_rules})
645 7 ▶ if $version >= 160000 && defined $row->{icu_rules} && length $row->{icu_rules};
Branch: 1 branch
BranchTrueFalseTotal
if $version >= 160000 and defined $row->{"icu_rules"} and length $row->{"icu_rules"} 2 5 7
Condition100%6/6
$version >= 160000 and defined $row->{"icu_rules"} and length $row->{"icu_rules"}
!ll&&!rl&&r
412
$version >= 160000 and defined $row->{"icu_rules"}
!ll&&!rl&&r
223
MC/DC100%3/3
$version >= 160000 and defined $row->{"icu_rules"} and length $row->{"icu_rules"}
$version >= 160000 defined $row->{"icu_rules"} length $row->{"icu_rules"}
Truth table100%4/4
A and B and C
A= $version >= 160000B= defined $row->{"icu_rules"}C= length $row->{"icu_rules"}
ABCresult
0XX0
10X0
1100
1111
646 }
647 19 return join ' ', @sql;
648 }
649  
650  
651 3 ▶sub _drop_temp_db {
Subroutine _drop_temp_db: called CC=1 SCAR=0.0
652 3 my ($self, $dbh, $name) = @_;
653 3 $self->_refuse_open_transaction($dbh, '_drop_temp_db');
654 2 $dbh->do(sprintf 'DROP DATABASE IF EXISTS %s', DBIO::PostgreSQL::Util::quote_ident($name));
655 }
656  
657  
658 66 1;
659  
660 __END__
« blib/lib/DBIO/PostgreSQL/Diff/Column.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/OutsideDependents.pm »