blib/lib/DBIO/PostgreSQL/DDL.pm

filter: Statementstmt 98.6% 449 / 455 Branchbran 87.2% 225 / 258 Conditioncond 71.3% 164 / 230 MC/DCmcdc 46.9% 61 / 130 Subroutinesub 100% 34 / 34 Podpod 100% 4 / 4 totaltotal 84.3% 937 / 1111 CC 243 SCAR 47.6
CC
243
Cov
89%
install_ddl
189.3
_pg_column_type
12.5
_setting_key
11.9
CRAP 116.5
« blib/lib/DBIO/PostgreSQL/Introspect/Indexes.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/ForeignKeys.pm »
1 package DBIO::PostgreSQL::DDL;
2 # ABSTRACT: Generate PostgreSQL DDL from DBIO schema classes
3  
4 124 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 124 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 124 our $VERSION = '0.900007';
8  
9 124 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 124 ▶use DBIO::PostgreSQL::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 quote_ident quote_qualified quote_literal quote_role
12 create_schema_if_missing
13 type_takes_modifier type_with_modifier
14 connection_search_path deferrable_clause
15 124 );
16 124 ▶use DBIO::Schema::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 124 ▶use DBIO::PostgreSQL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 124 ▶use DBIO::PostgreSQL::Adapter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 124 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 124 my $ADAPTER = DBIO::PostgreSQL::Adapter->new;
22  
23  
24  
25  
26  
27  
28  
29  
30 222 ▶sub install_ddl {
Subroutine install_ddl: called CC=166 SCAR=52.4
Pod documented
31 222 my ($class, $schema, %opts) = @_;
32 222 ▶ my $control = $opts{extension_control_schemas} // {};
Condition100%2/2
$opts{"extension_control_schemas"} // {}
l!l
5217
MC/DC100%1/1
$opts{"extension_control_schemas"} // {}
$opts{"extension_control_schemas"}
Truth table100%2/2
A
A= $opts{"extension_control_schemas"}
Aresult
11
00
33 222 ▶ my $installed = $opts{extension_installed_schemas} // {};
Condition100%2/2
$opts{"extension_installed_schemas"} // {}
l!l
7215
MC/DC100%1/1
$opts{"extension_installed_schemas"} // {}
$opts{"extension_installed_schemas"}
Truth table100%2/2
A
A= $opts{"extension_installed_schemas"}
Aresult
11
00
34 222 my @stmts;
35 my @views;
36 222 my %view_of_class;
37 222 my %seen_table;
38 222 my %seen_view;
39 222 my %enum_type;
40  
41 # 1. Extensions. One that names a schema is created WITH SCHEMA. The
42 # schema must be one pg_schemas declares (or public): a deploy sees no
43 # other, and creating one the declaration does not name would be an
44 # interface of its own (karr k120, point 8). It is created here, ahead of
45 # the extension, and left out of step 2 -- step 2 comes later.
46 #
47 # Every extension is created WITH SCHEMA (karr k141): an unqualified
48 # CREATE EXTENSION lands in the first schema of the session's search_path
49 # (ag_catalog after the activation of age; the namespace on a connection
50 # with the connect key schema, karr k128), and an extension is the
51 # database's, not that schema's. The schema is the declared one; else
52 # the one the control file of the extension fixes, where the caller
53 # knows it (extension_control_schemas, read from the server by the
54 # deploy); else public. For a fixed schema the statement is written
55 # without the clause: the server installs the extension there whatever
56 # the search_path, creates the schema as part of the extension when it
57 # is missing, and refuses WITH SCHEMA for a schema that does not exist
58 # yet (a deploy restricted by pg_schemas does not create it either).
59 222 my %created_ns;
60 222 ▶ my $ext_schemas = _schema_decl($schema, 'pg_extension_schemas') // {};
Condition50%1/2
_schema_decl($schema, "pg_extension_schemas") // {}
l!l
2220
MC/DC0%0/1
_schema_decl($schema, "pg_extension_schemas") // {}
_schema_decl($schema, "pg_extension_schemas")
Truth table50%1/2
A
A= _schema_decl($schema, "pg_extension_schemas")
Aresult
11
00
61 222 my @declared_ns = _schema_decl($schema, 'pg_schemas');
62 222 my @extensions = _schema_decl($schema, 'pg_extensions');
63 222 my $namespace = _connection_namespace($schema);
64 222 ▶ _refuse_namespace_declarations($schema, $namespace) if defined $namespace;
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace 14 208 222
65 # A schema a connection with the connect key creates is looked up in
66 # pg_namespace first: CREATE SCHEMA IF NOT EXISTS needs CREATE on the
67 # database even for a namespace that is there (karr k131, d).
68 51 ▶ my $create_schema = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
69 51 ▶ defined $namespace
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace ? : 16 35 51
70 ? create_schema_if_missing($_[0]) . ';'
71 : sprintf('CREATE SCHEMA IF NOT EXISTS %s;', quote_ident($_[0]));
72 219 };
73 219 for my $ext (@extensions) {
74 40 my $ns = $ext_schemas->{$ext};
75 40 my $fixed = $control->{$ext};
76 40 ▶ refuse_control_schema($ext, $ns, $fixed)
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns and defined $fixed and $ns ne $fixed 1 39 40
Condition83%5/6
defined $ns and defined $fixed and $ns ne $fixed
!ll&&!rl&&r
3901
defined $ns and defined $fixed
!ll&&!rl&&r
21181
MC/DC66%2/3
defined $ns and defined $fixed and $ns ne $fixed
defined $ns defined $fixed $ns ne $fixed
Truth table75%3/4
A and B and C
A= defined $nsB= defined $fixedC= $ns ne $fixed
ABCresult
0XX0
10X0
1100
1111
77 if defined $ns && defined $fixed && $ns ne $fixed;
78 39 ▶ my $from_control = !defined $ns && defined $fixed;
Condition100%3/3
!defined($ns) && defined $fixed
!ll&&!rl&&r
18192
MC/DC100%2/2
!defined($ns) && defined $fixed
!defined($ns) defined $fixed
Truth table100%3/3
not(A) && B
A= !defined($ns)B= defined $fixed
ABresult
0X0
100
111
79 # Neither declared nor fixed: the schema the database under diff holds it
80 # in, where the caller knows it (karr k144); else public.
81 39 ▶ my $from_database = !defined $ns && !defined $fixed && defined $installed->{$ext};
Condition100%6/6
!(defined $ns || defined $fixed) && defined $installed->{$ext}
!ll&&!rl&&r
20172
defined $ns || defined $fixed
l!l&&r!l&&!r
18219
MC/DC100%3/3
!(defined $ns || defined $fixed) && defined $installed->{$ext}
defined $ns defined $fixed defined $installed->{$ext}
Truth table100%4/4
not(A || B) && C
A= defined $nsB= defined $fixedC= defined $installed->{$ext}
ABCresult
1XX0
01X0
0000
0011
82 39 ▶ $ns //= $fixed // $installed->{$ext} // 'public';
Condition100%7/7
$ns //= ($fixed // $installed->{$ext}) // "public"
l!l
1821
($fixed // $installed->{$ext}) // "public"
l!l
417
$fixed // $installed->{$ext}
l!l&&r!l&&!r
2217
MC/DC100%1/1
$ns //= ($fixed // $installed->{$ext}) // "public"
$ns
Truth table100%2/2
A
A= $ns
Aresult
11
00
83 39 ▶ die sprintf
Branch: 1 branch
BranchTrueFalseTotal
if defined $namespace and $ns eq $namespace 2 37 39
Condition100%3/3
defined $namespace and $ns eq $namespace
!ll&&!rl&&r
3072
MC/DC100%2/2
defined $namespace and $ns eq $namespace
defined $namespace $ns eq $namespace
Truth table100%3/3
A and B
A= defined $namespaceB= $ns eq $namespace
ABresult
0X0
100
111
84 "refusing to write the install DDL: extension %s would be created in "
85 . "schema %s, the namespace this connection works in (connect key "
86 . "schema). An extension belongs to the database and is not installed "
87 . "into the namespace of one connection; name another schema for it, "
88 . "pg_extensions(%s => { schema => ... }).\n",
89 $ext, $ns, $ext
90 if defined $namespace && $ns eq $namespace;
91 37 ▶ if ($from_control) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_control) 2 35 37
92 2 push @stmts, sprintf 'CREATE EXTENSION IF NOT EXISTS %s;', quote_ident($ext);
93 2 next;
94 }
95 die sprintf
96 "refusing to write the install DDL: extension %s is declared in schema "
97 . "%s, which pg_schemas does not declare (%s). A schema an extension "
98 . "names has to be one the deploy manages; add '%s' to pg_schemas. "
99 . "Declaring pg_schemas makes the deploy restricted: it drops no "
100 . "extension unless built with manage_extensions_when_restricted.\n",
101 $ext, $ns, (@declared_ns ? join(', ', @declared_ns) : 'none declared'), $ns
102 35 ▶ if $ns ne 'public' && !$from_database && !grep { $_ eq $ns } @declared_ns;
Branch: 2 branches
BranchTrueFalseTotal
if $ns ne "public" and not $from_database and not grep({$_ eq $ns;} @declared_ns) 2 33 35
@declared_ns ? : 1 1 2
Condition100%6/6
$ns ne "public" and not $from_database and not grep({$_ eq $ns;} @declared_ns)
!ll&&!rl&&r
19142
$ns ne "public" and not $from_database
!ll&&!rl&&r
18116
MC/DC100%3/3
$ns ne "public" and not $from_database and not grep({$_ eq $ns;} @declared_ns)
$ns ne "public" not $from_database not grep({$_ eq $ns;} @declared_ns)
Truth table100%4/4
A and not(B) and not(C)
A= $ns ne "public"B= not $from_databaseC= not grep({$_ eq $ns;} @declared_ns)
ABCresult
0XX0
10X0
1100
1111
103 33 ▶ if ($ns ne 'public' and !$created_ns{$ns}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns ne "public" and not $created_ns{$ns}++) 15 18 33
Condition66%2/3
$ns ne "public" and not $created_ns{$ns}++
!ll&&!rl&&r
18015
MC/DC50%1/2
$ns ne "public" and not $created_ns{$ns}++
$ns ne "public" not $created_ns{$ns}++
Truth table66%2/3
A and not(B)
A= $ns ne "public"B= not $created_ns{$ns}++
ABresult
0X0
100
111
104 15 push @stmts, $create_schema->($ns);
105 }
106 33 push @stmts, sprintf 'CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;',
107 quote_ident($ext), quote_ident($ns);
108 }
109  
110 # 1b. The namespace of the connection, and its search_path: from here on
111 # an unqualified name is created in the namespace. Created first,
112 # because the server puts an unqualified object into the first schema
113 # of the path that exists -- without the namespace that is the one
114 # behind it, and nothing says so. Set here, in the script, because the
115 # temporary database of a diff is reached by a connection of its own.
116 214 my $connection_path;
117 214 ▶ if (defined $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespace) 9 205 214
118 push @stmts, $create_schema->($namespace)
119 9 ▶ unless $namespace eq 'public' or $created_ns{$namespace}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $namespace eq "public" or $created_ns{$namespace}++ 9 0 9
Condition33%1/3
$namespace eq "public" or $created_ns{$namespace}++
l!l&&r!l&&!r
009
MC/DC0%0/2
$namespace eq "public" or $created_ns{$namespace}++
$namespace eq "public" $created_ns{$namespace}++
Truth table33%1/3
A or B
A= $namespace eq "public"B= $created_ns{$namespace}++
ABresult
1X1
011
000
120 $connection_path = sprintf 'SET search_path TO %s;', join ', ',
121 9 map { quote_ident($_) }
122 connection_search_path($namespace, \@extensions, $ext_schemas, [], $control);
123 9 push @stmts, $connection_path;
124 }
125  
126 # 2. Schemas (namespaces)
127 214 for my $ns (_namespaces($schema)) {
128 49 ▶ next if $ns eq 'public'; # public always exists
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "public" 7 42 49
129 42 ▶ next if $created_ns{$ns}; # created ahead of the extension that names it
Branch: 1 branch
BranchTrueFalseTotal
if $created_ns{$ns} 15 27 42
130 27 push @stmts, $create_schema->($ns);
131 }
132  
133 # 3. Types from PgSchema classes
134 214 for my $ns (_namespaces($schema)) {
135 49 my $pg_schema_class = _schema_decl($schema, pg_schema_class => $ns);
136 49 ▶ next unless $pg_schema_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg_schema_class 42 7 49
137  
138 # Enums
139 7 for my $def (@{ $pg_schema_class->_pg_enum_defs }) {
140 6 my ($name, $values) = @$def;
141 6 my $vals = join ', ', map { quote_literal($_) } @$values;
142 6 push @stmts, sprintf "CREATE TYPE %s AS ENUM (%s);",
143 quote_qualified($ns, $name), $vals;
144 }
145  
146 # Composite types
147 7 for my $def (@{ $pg_schema_class->_pg_type_defs }) {
148 2 my ($name, $fields) = @$def;
149 2 my @attrs;
150 2 for my $fname (sort keys %$fields) {
151 4 push @attrs, sprintf ' %s %s', quote_ident($fname), $fields->{$fname};
152 }
153 2 push @stmts, sprintf "CREATE TYPE %s AS (\n%s\n);",
154 quote_qualified($ns, $name), join(",\n", @attrs);
155 }
156  
157 # Functions
158 7 for my $def (@{ $pg_schema_class->_pg_function_defs }) {
159 1 my ($name, $sql) = @$def;
160 1 push @stmts, $sql;
161 }
162 }
163  
164 # 3b. Auto-generate CREATE TYPE for Cake-style enum columns
165 # (data_type => 'enum' with extra->{list} but no pg_enum_type)
166 {
167 214 my @sources = $schema->sources;
168 214 for my $source_name (sort @sources) {
169 278 my $source = $schema->source($source_name);
170 278 my $result_class = $source->result_class;
171 278 my $table_name = $source->name;
172  
173 # Only real tables carry enum columns worth a CREATE TYPE; views and
174 # subquery sources (scalar-ref name) have no backing table.
175 278 ▶ next if $source->isa('DBIO::ResultSource::View') || ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIO::ResultSource::View") or ref $table_name 30 248 278
Condition66%2/3
$source->isa("DBIO::ResultSource::View") or ref $table_name
l!l&&r!l&&!r
300248
MC/DC50%1/2
$source->isa("DBIO::ResultSource::View") or ref $table_name
$source->isa("DBIO::ResultSource::View") ref $table_name
Truth table66%2/3
A or B
A= $source->isa("DBIO::ResultSource::View")B= ref $table_name
ABresult
1X1
011
000
176  
177 248 ▶ my $pg_schema_name = $result_class->can('pg_schema')
Branch: 1 branch
BranchTrueFalseTotal
$result_class->can('pg_schema') ? : 238 10 248
178 ? $result_class->pg_schema : undef;
179  
180 248 for my $col_name ($source->columns) {
181 648 my $info = $source->column_info($col_name);
182 648 ▶ next unless ($info->{data_type} // '') eq 'enum';
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"data_type"} // "") eq "enum" 640 8 648
Condition50%1/2
$info->{"data_type"} // ""
l!l
6480
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
183 8 ▶ next if $info->{pg_enum_type}; # already declared via PgSchema
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"pg_enum_type"} 0 8 8
184 8 ▶ next unless $info->{extra} && $info->{extra}{list}; # need values
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"extra"} and $info->{"extra"}{"list"} 0 8 8
Condition33%1/3
$info->{"extra"} and $info->{"extra"}{"list"}
!ll&&!rl&&r
008
MC/DC0%0/2
$info->{"extra"} and $info->{"extra"}{"list"}
$info->{"extra"} $info->{"extra"}{"list"}
Truth table33%1/3
A and B
A= $info->{"extra"}B= $info->{"extra"}{"list"}
ABresult
0X0
100
111
185  
186 # Generate a type name: {table}_{column}_enum, in the schema of the
187 # table -- the last of the table's name parts gets the suffix.
188 8 my @type_parts = _relation_parts($pg_schema_name, $table_name);
189 8 $type_parts[-1] .= "_${col_name}_enum";
190 8 my $type = quote_qualified(@type_parts);
191  
192 8 my $vals = join ', ', map { quote_literal($_) } @{ $info->{extra}{list} };
193 8 push @stmts, sprintf "CREATE TYPE %s AS ENUM (%s);", $type, $vals;
194  
195 # The column below is of exactly this type. Remembered here, not
196 # written into the column info: install_ddl reads the schema, and a
197 # pg_enum_type left behind made the next call skip this CREATE TYPE.
198 8 $enum_type{$source_name}{$col_name} = $type;
199 }
200 }
201 }
202  
203 # 4. Tables from Result classes
204 214 my @sources = $schema->sources;
205 214 for my $source_name (sort @sources) {
206 274 my $source = $schema->source($source_name);
207 274 my $result_class = $source->result_class;
208  
209 274 my $table_name = $source->name;
210 274 my $pg_schema_name;
211 274 ▶ if ($result_class->can('pg_schema')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_schema")) 264 10 274
212 264 $pg_schema_name = $result_class->pg_schema;
213 }
214  
215 # Views: CREATE VIEW comes after all tables and their foreign keys (6.
216 # below); skip virtual views. Which view a result class is, is noted for
217 # every one of them: that is what a deploy_depends_on names.
218 274 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 30 244 274
219 30 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 2 28 30
Condition66%2/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
0282
MC/DC50%1/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table66%2/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
220 28 ▶ next if ref $table_name && ref $table_name ne 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name and ref $table_name ne "SCALAR" 0 28 28
Condition33%1/3
ref $table_name and ref $table_name ne "SCALAR"
!ll&&!rl&&r
2800
MC/DC0%0/2
ref $table_name and ref $table_name ne "SCALAR"
ref $table_name ref $table_name ne "SCALAR"
Truth table33%1/3
A and B
A= ref $table_nameB= ref $table_name ne "SCALAR"
ABresult
0X0
100
111
221 28 my $vqual = _relation_sql($pg_schema_name, $table_name);
222 28 ▶ $view_of_class{$result_class} //= $vqual;
Condition33%1/3
$view_of_class{$result_class} //= $vqual
l!l&&r!l&&!r
0280
MC/DC0%0/2
$view_of_class{$result_class} //= $vqual
$view_of_class{$result_class} $vqual
Truth table33%1/3
A //= B
A= $view_of_class{$result_class}B= $vqual
ABresult
1X1
011
000
223 28 ▶ if (my $earlier = $seen_view{$vqual}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $earlier = $seen_view{$vqual}) 2 26 28
224 2 $source->throw_exception(sprintf
225 "refusing to write the install DDL: the view %s is declared by two "
226 . "sources, %s (%s) and %s (%s); the server holds one view of a "
227 . "name -- declare it once",
228 $vqual, $earlier->[0], $earlier->[1], $source_name, $result_class);
229 }
230 26 $seen_view{$vqual} = [ $source_name, $result_class ];
231 26 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 26 0 26
232 26 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 1 25 26
Condition66%2/3
defined $def and length $def
!ll&&!rl&&r
1025
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
233 25 push @views, {
234 relation => $vqual,
235 class => $result_class,
236 depends_on => [ _view_depends_on($source) ],
237 sql => sprintf('CREATE VIEW %s AS %s', $vqual, $def),
238 };
239 25 next;
240 }
241  
242 # A scalar-ref name is either a literal table name (\'cd') or an inline
243 # subquery source; the former is written as given, the latter skipped.
244 244 ▶ next if ref $table_name eq 'SCALAR' && $$table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name eq "SCALAR" and $$table_name =~ /\s|\(/ 0 244 244
Condition33%1/3
ref $table_name eq "SCALAR" and $$table_name =~ /\s|\(/
!ll&&!rl&&r
24400
MC/DC0%0/2
ref $table_name eq "SCALAR" and $$table_name =~ /\s|\(/
ref $table_name eq "SCALAR" $$table_name =~ /\s|\(/
Truth table33%1/3
A and B
A= ref $table_name eq "SCALAR"B= $$table_name =~ /\s|\(/
ABresult
0X0
100
111
245 244 ▶ next if ref $table_name && ref $table_name ne 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name and ref $table_name ne "SCALAR" 0 244 244
Condition33%1/3
ref $table_name and ref $table_name ne "SCALAR"
!ll&&!rl&&r
24400
MC/DC0%0/2
ref $table_name and ref $table_name ne "SCALAR"
ref $table_name ref $table_name ne "SCALAR"
Truth table33%1/3
A and B
A= ref $table_nameB= ref $table_name ne "SCALAR"
ABresult
0X0
100
111
246  
247 244 my $qualified = _relation_sql($pg_schema_name, $table_name);
248  
249 # Multiple result sources can map to one physical table; emit it once.
250 244 ▶ next if $seen_table{$qualified}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$qualified}++ 0 244 244
251  
252 # Column definitions
253 244 my @col_defs;
254 244 for my $col_name ($source->columns) {
255 644 my $info = $source->column_info($col_name);
256 644 ▶ my $type = $enum_type{$source_name}{$col_name} // _pg_column_type($info, (ref $table_name ? $source_name : $table_name) . ".$col_name");
Branch: 1 branch
BranchTrueFalseTotal
ref $table_name ? : 0 636 636
Condition66%2/3
$enum_type{$source_name}{$col_name} // _pg_column_type($info, (ref $table_name ? $source_name : $table_name) . ".$col_name")
l!l&&r!l&&!r
86360
MC/DC0%0/2
$enum_type{$source_name}{$col_name} // _pg_column_type($info, (ref $table_name ? $source_name : $table_name) . ".$col_name")
$enum_type{$source_name}{$col_name} _pg_column_type($info, (ref $table_name ? $source_name : $table_name) . ".$col_name")
Truth table66%2/3
A // B
A= $enum_type{$source_name}{$col_name}B= _pg_column_type($info, (ref $table_name ? $source_name : $table_name) . ".$col_name")
ABresult
1X1
011
000
257 639 my $def = sprintf ' %s %s', quote_ident($col_name), $type;
258 639 ▶ $def .= ' NOT NULL' if !$info->{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"is_nullable"} 450 189 639
259 639 ▶ if ($info->{is_auto_increment} && $type !~ /^(?:big)?serial$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->{'is_auto_increment'} and not $type =~ /^(?:big)?serial$/) { } 100 539 639
elsif (defined $info->{'default_value'}) { } 7 532 539
Condition100%3/3
$info->{"is_auto_increment"} and not $type =~ /^(?:big)?serial$/
!ll&&!rl&&r
5372100
MC/DC100%2/2
$info->{"is_auto_increment"} and not $type =~ /^(?:big)?serial$/
$info->{"is_auto_increment"} not $type =~ /^(?:big)?serial$/
Truth table100%3/3
A and not(B)
A= $info->{"is_auto_increment"}B= not $type =~ /^(?:big)?serial$/
ABresult
0X0
100
111
260 # DBIO convention: auto-increment columns are GENERATED ALWAYS AS
261 # IDENTITY unless the user explicitly opts into BY DEFAULT via
262 # extra->{identity} = 'd'. ALWAYS prevents accidental client-side
263 # inserts, matches the DBIx::Class::ResultSource default, and is
264 # what DBIO::PostgreSQL::Diff::target_from_compiled synthesizes
265 # (so the round-trip diff against the introspected source is
266 # empty for the default schema).
267 my $kind = DBIO::PostgreSQL::Introspect->identity_kind(
268 $info->{extra} && $info->{extra}{identity}
269 100 ▶ ) // 'ALWAYS';
Condition40%2/5
"DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"}) // "ALWAYS"
l!l
0100
$info->{'extra'} && $info->{'extra'}{'identity'}
!ll&&!rl&&r
10000
MC/DC0%0/1
"DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"}) // "ALWAYS"
"DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"})
MC/DC0%0/2
$info->{'extra'} && $info->{'extra'}{'identity'}
$info->{'extra'} $info->{'extra'}{'identity'}
Truth table50%1/2
A
A= "DBIO::PostgreSQL::Introspect"->identity_kind($info->{"extra"} && $info->{"extra"}{"identity"})
Aresult
11
00
Truth table33%1/3
A && B
A= $info->{'extra'}B= $info->{'extra'}{'identity'}
ABresult
0X0
100
111
270 100 $def .= " GENERATED $kind AS IDENTITY";
271 }
272 elsif (defined $info->{default_value}) {
273 7 my $dv = $info->{default_value};
274 7 ▶ if (ref $dv eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dv eq 'SCALAR') { } 3 4 7
275 3 $def .= " DEFAULT $$dv";
276 } else {
277 4 $def .= ' DEFAULT ' . quote_literal($dv);
278 }
279 }
280 639 push @col_defs, $def;
281 }
282  
283 # Primary key
284 239 my @pk = $source->primary_columns;
285 239 ▶ if (@pk) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk) 229 10 239
286 push @col_defs, sprintf ' PRIMARY KEY (%s)',
287 229 join(', ', map { quote_ident($_) } @pk);
288 }
289  
290 # Unique constraints declared via add_unique_constraint / Cake's `unique`.
291 # The 'primary' constraint is auto-added by set_primary_key and already
292 # emitted above as PRIMARY KEY, so it must be skipped here.
293 my %unique_options = $result_class->can('pg_unique_options')
294 239 ▶ ? %{ $result_class->pg_unique_options } : ();
Branch: 1 branch
BranchTrueFalseTotal
$result_class->can('pg_unique_options') ? : 229 10 239
295 239 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 238 1 239
296 238 my %uniques = $source->unique_constraints;
297 238 for my $oname (sort keys %unique_options) {
298 $source->throw_exception(sprintf
299 "refusing to write the install DDL: pg_unique_options of %s names '%s', "
300 . "which is no UNIQUE constraint of the table",
301 6 ▶ $table_name, $oname) unless $oname ne 'primary' && $uniques{$oname};
Branch: 1 branch
BranchTrueFalseTotal
unless $oname ne "primary" and $uniques{$oname} 1 5 6
Condition66%2/3
$oname ne "primary" and $uniques{$oname}
!ll&&!rl&&r
015
MC/DC50%1/2
$oname ne "primary" and $uniques{$oname}
$oname ne "primary" $uniques{$oname}
Truth table66%2/3
A and B
A= $oname ne "primary"B= $uniques{$oname}
ABresult
0X0
100
111
302 }
303 237 for my $uname (sort keys %uniques) {
304 246 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 226 20 246
305 20 my $cols = $uniques{$uname};
306 push @col_defs, sprintf ' CONSTRAINT %s UNIQUE (%s)%s',
307 quote_ident($uname),
308 27 join(', ', map { quote_ident($_) } @$cols),
309 20 _unique_timing($source, $table_name, $uname, $unique_options{$uname});
310 }
311 }
312  
313 236 push @stmts, sprintf "CREATE TABLE %s (\n%s\n);",
314 $qualified, join(",\n", @col_defs);
315  
316 # Indexes: the ones written by hand with pg_index, and the canonical
317 # declarations of the result source (Cake idx, indices; ADR 0061 in
318 # core, karr k124). A canonical declaration of the same name wins over
319 # the hand-written one. Its name, columns and unique are the entry's own;
320 # the rest -- using, expression, opclasses, include, with, where -- is
321 # its driver->{pg} block. A schema is bound to one driver, so what the
322 # DDL would not write is refused (karr k129): options, a type other than
323 # unique, an entry with only another driver's block, a key the pg block
324 # does not know. An own, empty pg block is the plain index.
325 {
326 236 my %indexes;
327 236 ▶ if ($result_class->can('pg_indexes')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_indexes")) 226 10 236
328 226 my $hand = $result_class->pg_indexes;
329 226 $indexes{$_} = $hand->{$_} for keys %$hand;
330 }
331 236 ▶ my @declared = $source->can('index_declarations_for')
Branch: 1 branch
BranchTrueFalseTotal
$source->can('index_declarations_for') ? : 235 1 236
332 ? $source->index_declarations_for('pg',
333 [qw(where using with expression include opclasses)])
334 : ();
335 232 for my $decl (@declared) {
336 $source->throw_exception(sprintf
337 "index '%s' of %s: options are not read by the PostgreSQL driver; "
338 . "declare what the index needs in its pg => { ... } block",
339 $decl->{name}, $source->name)
340 52 ▶ if defined $decl->{options};
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{"options"} 1 51 52
341 $source->throw_exception(sprintf
342 "index '%s' of %s: type '%s' is not read by the PostgreSQL driver; "
343 . "only type => 'unique' is (or unique => 1); "
344 . "use pg => { using => ... } for an access method",
345 $decl->{name}, $source->name, $decl->{type})
346 51 ▶ if defined $decl->{type} and $decl->{type} !~ /\A unique \z/xi;
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{"type"} and not $decl->{"type"} =~ /\A unique \z/xi 1 50 51
Condition100%3/3
defined $decl->{"type"} and not $decl->{"type"} =~ /\A unique \z/xi
!ll&&!rl&&r
4371
MC/DC100%2/2
defined $decl->{"type"} and not $decl->{"type"} =~ /\A unique \z/xi
defined $decl->{"type"} not $decl->{"type"} =~ /\A unique \z/xi
Truth table100%3/3
A and not(B)
A= defined $decl->{"type"}B= not $decl->{"type"} =~ /\A unique \z/xi
ABresult
0X0
100
111
347 $indexes{ $decl->{name} } = {
348 50 ▶ %{ $decl->{driver}{pg} || {} },
Branch: 1 branch
BranchTrueFalseTotal
$decl->{'unique'} ? : 10 40 50
Condition100%2/2
$decl->{'driver'}{'pg'} || {}
l!l
3317
MC/DC100%1/1
$decl->{'driver'}{'pg'} || {}
$decl->{'driver'}{'pg'}
Truth table100%2/2
A
A= $decl->{'driver'}{'pg'}
Aresult
11
00
349 columns => $decl->{columns},
350 50 ($decl->{unique} ? (unique => 1) : ()),
351 };
352 }
353 230 my $indexes = \%indexes;
354  
355 # The columns of every unique constraint of the table, written above as
356 # a CONSTRAINT ... UNIQUE line, which the server backs with an index of
357 # its own. An index over exactly those columns, in that order, with the
358 # default access method and nothing else, is a second one for the same
359 # lookups and is not written, plain or declared unique (karr k127). An
360 # expression, a predicate, another access method, an operator class,
361 # INCLUDE columns or storage parameters make it another object.
362 230 my %uniq_cols;
363 230 ▶ if ($source->can('unique_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("unique_constraints")) 229 1 230
364 229 my %uniques = $source->unique_constraints;
365 229 for my $uname (keys %uniques) {
366 238 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 220 18 238
367 18 $uniq_cols{ join "\0", @{ $uniques{$uname} } } = 1;
368 }
369 }
370  
371 230 for my $idx_name (sort keys %$indexes) {
372 94 my $idx = $indexes->{$idx_name};
373 94 ▶ next if $uniq_cols{ join "\0", @{ $idx->{columns} // [] } }
Condition75%6/8
$idx->{"columns"} // []
l!l
931
not defined $idx->{"using"} or lc $idx->{"using"} eq "btree"
l!l&&r!l&&!r
502
$idx->{'include'} && @{$idx->{'include'};}
!ll&&!rl&&r
401
MC/DC100%1/1
$idx->{"columns"} // []
$idx->{"columns"}
MC/DC50%1/2
not defined $idx->{"using"} or lc $idx->{"using"} eq "btree"
not defined $idx->{"using"} lc $idx->{"using"} eq "btree"
MC/DC50%1/2
$idx->{'include'} && @{$idx->{'include'};}
$idx->{'include'} @{$idx->{'include'};}
Truth table100%2/2
A
A= $idx->{"columns"}
Aresult
11
00
Truth table66%2/3
not(A) or B
A= not defined $idx->{"using"}B= lc $idx->{"using"} eq "btree"
ABresult
1X1
011
000
Truth table66%2/3
A && B
A= $idx->{'include'}B= @{$idx->{'include'};}
ABresult
0X0
100
111
374 && !defined $idx->{expression} && !$idx->{where}
375 && (!defined $idx->{using} || lc $idx->{using} eq 'btree')
376 1 ▶ && !($idx->{include} && @{ $idx->{include} })
Condition33%1/3
$idx->{'with'} && %{$idx->{'with'};}
!ll&&!rl&&r
400
MC/DC0%0/2
$idx->{'with'} && %{$idx->{'with'};}
$idx->{'with'} %{$idx->{'with'};}
Truth table33%1/3
A && B
A= $idx->{'with'}B= %{$idx->{'with'};}
ABresult
0X0
100
111
377 0 && !($idx->{with} && %{ $idx->{with} })
378 0 ▶ && !grep { defined && length } values %{ $idx->{opclasses} || {} };
Branch: 1 branch
BranchTrueFalseTotal
if $uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"} and not $idx->{"where"} and !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree" and not $idx->{"include"} && @{$idx->{"include"};} and not $idx->{"with"} && %{$idx->{"with"};} and not grep({defined $_ and length $_;} values %{$idx->{"opclasses"} or {};}) 4 90 94
Condition73%17/23
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"} and not $idx->{"where"} and !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree" and not $idx->{"include"} && @{$idx->{"include"};} and not $idx->{"with"} && %{$idx->{"with"};} and not grep({defined $_ and length $_;} values %{$idx->{"opclasses"} or {};})
!ll&&!rl&&r
9004
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"} and not $idx->{"where"} and !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree" and not $idx->{"include"} && @{$idx->{"include"};} and not $idx->{"with"} && %{$idx->{"with"};}
!ll&&!rl&&r
9004
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"} and not $idx->{"where"} and !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree" and not $idx->{"include"} && @{$idx->{"include"};}
!ll&&!rl&&r
8914
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"} and not $idx->{"where"} and !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree"
!ll&&!rl&&r
8725
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"} and not $idx->{"where"}
!ll&&!rl&&r
8617
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"}
!ll&&!rl&&r
8518
defined $_ && length $_
!ll&&!rl&&r
000
$idx->{'opclasses'} || {}
l!l
04
MC/DC0%0/7
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} and not defined $idx->{"expression"} and not $idx->{"where"} and !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree" and not $idx->{"include"} && @{$idx->{"include"};} and not $idx->{"with"} && %{$idx->{"with"};} and not grep({defined $_ and length $_;} values %{$idx->{"opclasses"} or {};})
$uniq_cols{join "\0", @{$idx->{"columns"} // [];}} not defined $idx->{"expression"} not $idx->{"where"} !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree" not $idx->{"include"} && @{$idx->{"include"};} not $idx->{"with"} && %{$idx->{"with"};} not grep({defined $_ and length $_;} values %{$idx->{"opclasses"} or {};})
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
MC/DC0%0/1
$idx->{'opclasses'} || {}
$idx->{'opclasses'}
Truth table37%3/8
A and not(B) and not(C) and D and not(E) and not(F) and not(G)
A= $uniq_cols{join "\0", @{$idx->{"columns"} // [];}}B= not defined $idx->{"expression"}C= not $idx->{"where"}D= !defined($idx->{"using"}) || lc $idx->{"using"} eq "btree"E= not $idx->{"include"} && @{$idx->{"include"};}F= not $idx->{"with"} && %{$idx->{"with"};}G= not grep({defined $_ and length $_;} values %{$idx->{"opclasses"} or {};})
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table50%1/2
A
A= $idx->{'opclasses'}
Aresult
11
00
379 90 ▶ my $using = $idx->{using} ? " USING $idx->{using}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'using'} ? : 22 68 90
380 90 my $columns;
381 90 ▶ if ($idx->{expression}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx->{'expression'}) { } 3 87 90
382 3 $columns = $idx->{expression};
383 } else {
384 # Per-column operator class, appended after the column name.
385 # Mandatory for pgvector hnsw: it has no default opclass for vector,
386 # so USING hnsw (embedding) alone is rejected by PostgreSQL. ivfflat
387 # defaults to vector_l2_ops; there it selects any other distance.
388 87 ▶ my $opclasses = $idx->{opclasses} || {};
Condition100%2/2
$idx->{'opclasses'} || {}
l!l
2166
MC/DC100%1/1
$idx->{'opclasses'} || {}
$idx->{'opclasses'}
Truth table100%2/2
A
A= $idx->{'opclasses'}
Aresult
11
00
389 $columns = join(', ', map {
390 97 ▶ defined $opclasses->{$_} && length $opclasses->{$_}
Branch: 1 branch
BranchTrueFalseTotal
defined $opclasses->{$_} && length $opclasses->{$_} ? : 20 77 97
Condition100%3/3
defined $opclasses->{$_} and length $opclasses->{$_}
!ll&&!rl&&r
76120
MC/DC100%2/2
defined $opclasses->{$_} and length $opclasses->{$_}
defined $opclasses->{$_} length $opclasses->{$_}
Truth table100%3/3
A and B
A= defined $opclasses->{$_}B= length $opclasses->{$_}
ABresult
0X0
100
111
391 ? quote_ident($_) . " $opclasses->{$_}" : quote_ident($_)
392 87 ▶ } @{ $idx->{columns} // [] });
Condition50%1/2
$idx->{"columns"} // []
l!l
870
MC/DC0%0/1
$idx->{"columns"} // []
$idx->{"columns"}
Truth table50%1/2
A
A= $idx->{"columns"}
Aresult
11
00
393 }
394 90 ▶ my $unique = $idx->{unique} ? 'UNIQUE ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$idx->{'unique'} ? : 12 78 90
395 90 my $sql = sprintf 'CREATE %sINDEX %s ON %s%s (%s)',
396 $unique, quote_ident($idx_name), $qualified, $using, $columns;
397 # Non-key payload columns of a covering index. They follow the key
398 # list and precede WITH/WHERE, which is the only order PostgreSQL
399 # accepts.
400 90 ▶ if ($idx->{include} && @{ $idx->{include} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx->{"include"} and @{$idx->{"include"};}) 13 77 90
Condition100%3/3
$idx->{"include"} and @{$idx->{"include"};}
!ll&&!rl&&r
76113
MC/DC100%2/2
$idx->{"include"} and @{$idx->{"include"};}
$idx->{"include"} @{$idx->{"include"};}
Truth table100%3/3
A and B
A= $idx->{"include"}B= @{$idx->{"include"};}
ABresult
0X0
100
111
401 13 $sql .= ' INCLUDE (' . join(', ', map { quote_ident($_) } @{ $idx->{include} }) . ')';
402 }
403 90 ▶ if ($idx->{with}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx->{"with"}) 10 80 90
404 10 my @with_parts;
405 10 for my $k (sort keys %{ $idx->{with} }) {
406 15 ▶ $source->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /\A[A-Za-z_][A-Za-z0-9_\$]*(?:\.[A-Za-z_][A-Za-z0-9_\$]*)?\z/ 1 14 15
407 "refusing to write the install DDL: index '%s' of %s: a key of with is a "
408 . "storage parameter name (letters, digits, underscores, one optional dot)",
409 $idx_name, $source->name)
410 unless $k =~ /\A[A-Za-z_][A-Za-z0-9_\$]*(?:\.[A-Za-z_][A-Za-z0-9_\$]*)?\z/;
411 14 push @with_parts, "$k = $idx->{with}{$k}";
412 }
413 9 $sql .= ' WITH (' . join(', ', @with_parts) . ')';
414 }
415 89 ▶ $sql .= " WHERE $idx->{where}" if $idx->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $idx->{"where"} 9 80 89
416 89 push @stmts, "$sql;";
417 }
418 }
419  
420 # Triggers
421 229 ▶ if ($result_class->can('pg_triggers')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_triggers")) 219 10 229
422 219 my $triggers = $result_class->pg_triggers;
423 219 for my $trg_name (sort keys %$triggers) {
424 6 my $trg = $triggers->{$trg_name};
425 6 ▶ my $for_each = uc($trg->{for_each} || 'ROW');
Condition100%2/2
$trg->{'for_each'} || 'ROW'
l!l
33
MC/DC100%1/1
$trg->{'for_each'} || 'ROW'
$trg->{'for_each'}
Truth table100%2/2
A
A= $trg->{'for_each'}
Aresult
11
00
426 6 ▶ $source->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless $for_each eq "ROW" or $for_each eq "STATEMENT" 1 5 6
Condition100%3/3
$for_each eq "ROW" or $for_each eq "STATEMENT"
l!l&&r!l&&!r
321
MC/DC100%2/2
$for_each eq "ROW" or $for_each eq "STATEMENT"
$for_each eq "ROW" $for_each eq "STATEMENT"
Truth table100%3/3
A or B
A= $for_each eq "ROW"B= $for_each eq "STATEMENT"
ABresult
1X1
011
000
427 "refusing to write the install DDL: trigger '%s' of %s: for_each is ROW or STATEMENT",
428 $trg_name, $source->name)
429 unless $for_each eq 'ROW' || $for_each eq 'STATEMENT';
430 push @stmts, sprintf 'CREATE TRIGGER %s %s %s ON %s FOR EACH %s EXECUTE FUNCTION %s;',
431 quote_ident($trg_name), $trg->{when}, $trg->{event},
432 5 $qualified, $for_each, $trg->{execute};
433 }
434 }
435  
436 # RLS
437 228 ▶ if ($result_class->can('pg_rls') && $result_class->pg_rls) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_rls") and $result_class->pg_rls) 7 221 228
Condition100%3/3
$result_class->can("pg_rls") and $result_class->pg_rls
!ll&&!rl&&r
102117
MC/DC100%2/2
$result_class->can("pg_rls") and $result_class->pg_rls
$result_class->can("pg_rls") $result_class->pg_rls
Truth table100%3/3
A and B
A= $result_class->can("pg_rls")B= $result_class->pg_rls
ABresult
0X0
100
111
438 7 my $rls = $result_class->pg_rls;
439 7 ▶ if ($rls->{enable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rls->{"enable"}) 7 0 7
440 7 push @stmts, sprintf 'ALTER TABLE %s ENABLE ROW LEVEL SECURITY;', $qualified;
441 }
442 7 ▶ if ($rls->{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rls->{"force"}) 3 4 7
443 3 push @stmts, sprintf 'ALTER TABLE %s FORCE ROW LEVEL SECURITY;', $qualified;
444 }
445 7 ▶ if ($rls->{policies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rls->{"policies"}) 7 0 7
446 7 for my $pol_name (sort keys %{ $rls->{policies} }) {
447 12 my $pol = $rls->{policies}{$pol_name};
448 12 my $sql = sprintf 'CREATE POLICY %s ON %s',
449 quote_ident($pol_name), $qualified;
450 12 ▶ $sql .= ' AS RESTRICTIVE' if $pol->{restrictive};
Branch: 1 branch
BranchTrueFalseTotal
if $pol->{"restrictive"} 2 10 12
451 12 ▶ my $for = uc($pol->{for} || 'ALL');
Condition100%2/2
$pol->{'for'} || 'ALL'
l!l
84
MC/DC100%1/1
$pol->{'for'} || 'ALL'
$pol->{'for'}
Truth table100%2/2
A
A= $pol->{'for'}
Aresult
11
00
452 12 ▶ $source->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless $for =~ /\A(?:SELECT|INSERT|UPDATE|DELETE|ALL)\z/ 1 11 12
453 "refusing to write the install DDL: policy '%s' of %s: for is SELECT, INSERT, UPDATE, DELETE or ALL",
454 $pol_name, $source->name)
455 unless $for =~ /\A(?:SELECT|INSERT|UPDATE|DELETE|ALL)\z/;
456 11 ▶ $sql .= " FOR $for" if $for ne 'ALL';
Branch: 1 branch
BranchTrueFalseTotal
if $for ne "ALL" 5 6 11
457 11 ▶ if ($pol->{roles} && @{ $pol->{roles} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pol->{"roles"} and @{$pol->{"roles"};}) 6 5 11
Condition66%2/3
$pol->{"roles"} and @{$pol->{"roles"};}
!ll&&!rl&&r
506
MC/DC50%1/2
$pol->{"roles"} and @{$pol->{"roles"};}
$pol->{"roles"} @{$pol->{"roles"};}
Truth table66%2/3
A and B
A= $pol->{"roles"}B= @{$pol->{"roles"};}
ABresult
0X0
100
111
458 6 $sql .= sprintf ' TO %s', join(', ', map { quote_role($_) } @{ $pol->{roles} });
459 }
460 11 ▶ $sql .= sprintf ' USING (%s)', $pol->{using} if $pol->{using};
Branch: 1 branch
BranchTrueFalseTotal
if $pol->{"using"} 10 1 11
461 11 ▶ $sql .= sprintf ' WITH CHECK (%s)', $pol->{with_check} if $pol->{with_check};
Branch: 1 branch
BranchTrueFalseTotal
if $pol->{"with_check"} 2 9 11
462 11 push @stmts, "$sql;";
463 }
464 }
465 }
466  
467 # CHECK constraints
468 227 ▶ if ($result_class->can('pg_check_constraints')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_check_constraints")) 217 10 227
469 217 my $checks = $result_class->pg_check_constraints;
470 217 for my $name (sort keys %$checks) {
471 2 my $entry = $checks->{$name};
472 2 my $expr = $entry->{definition};
473 2 ▶ $expr = "CHECK ($expr)" unless $expr =~ /^\s*CHECK\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $expr =~ /^\s*CHECK\b/i 2 0 2
474 2 push @stmts, sprintf 'ALTER TABLE %s ADD CONSTRAINT %s %s;',
475 $qualified, quote_ident($name), $expr;
476 }
477 }
478  
479 # Raw trailing DDL (e.g. cross-table FKs, unless the class opts into
480 # pg_fk_constraints_from_rels below)
481 227 ▶ if ($result_class->can('pg_extra_ddl')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class->can("pg_extra_ddl")) 217 10 227
482 217 my $extra = $result_class->pg_extra_ddl;
483 217 push @stmts, @$extra;
484 }
485 }
486  
487 # 5. Foreign key constraints from belongs_to (on by default, off per result
488 # class via pg_fk_constraints_from_rels(0) -- see pg_extra_ddl for
489 # the always-available manual alternative). Run as its own pass, after
490 # every CREATE TABLE above, so cross-table references always resolve
491 # regardless of source-iteration order.
492 195 my %fk_seen;
493 195 for my $source_name (sort @sources) {
494 249 my $source = $schema->source($source_name);
495 249 my $result_class = $source->result_class;
496 249 ▶ next unless $result_class->can('pg_fk_constraints_from_rels')
Branch: 1 branch
BranchTrueFalseTotal
unless $result_class->can("pg_fk_constraints_from_rels") and $result_class->pg_fk_constraints_from_rels 14 235 249
Condition100%3/3
$result_class->can("pg_fk_constraints_from_rels") and $result_class->pg_fk_constraints_from_rels
!ll&&!rl&&r
104235
MC/DC100%2/2
$result_class->can("pg_fk_constraints_from_rels") and $result_class->pg_fk_constraints_from_rels
$result_class->can("pg_fk_constraints_from_rels") $result_class->pg_fk_constraints_from_rels
Truth table100%3/3
A and B
A= $result_class->can("pg_fk_constraints_from_rels")B= $result_class->pg_fk_constraints_from_rels
ABresult
0X0
100
111
497 && $result_class->pg_fk_constraints_from_rels;
498 235 ▶ next if $source->isa('DBIO::ResultSource::View');
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIO::ResultSource::View") 26 209 235
499  
500 209 my $table_name = $source->name;
501 209 ▶ next if ref $table_name && ref $table_name ne 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name and ref $table_name ne "SCALAR" 0 209 209
Condition33%1/3
ref $table_name and ref $table_name ne "SCALAR"
!ll&&!rl&&r
20900
MC/DC0%0/2
ref $table_name and ref $table_name ne "SCALAR"
ref $table_name ref $table_name ne "SCALAR"
Truth table33%1/3
A and B
A= ref $table_nameB= ref $table_name ne "SCALAR"
ABresult
0X0
100
111
502  
503 209 ▶ my $pg_schema_name = $result_class->can('pg_schema') ? $result_class->pg_schema : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result_class->can('pg_schema') ? : 209 0 209
504 209 my $qualified = _relation_sql($pg_schema_name, $table_name);
505 209 ▶ $table_name = $$table_name if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 209 209
506  
507 209 for my $rel_name (sort $source->relationships) {
508 54 my $info = $source->relationship_info($rel_name);
509  
510 # attrs->{accessor} is not a reliable belongs_to marker: it is only
511 # 'filter' when the relationship name happens to equal an FK column
512 # name, and plain 'single' otherwise -- indistinguishable there from
513 # has_one/might_have. attrs->{is_depends_on} is hardcoded 1 only by
514 # DBIO::Relationship::BelongsTo (0 for has_one/might_have/has_many;
515 # this is the same signal DBIO::ResultSource's own _pk_depends_on and
516 # DBIO::ResultSet use to recognize FK-owning relationships).
517 54 ▶ next unless $info->{attrs}{is_depends_on};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"attrs"}{"is_depends_on"} 0 54 54
518  
519 # A belongs_to sets is_foreign_key_constraint to 1 unless told otherwise;
520 # a relationship that declares it 0 (or any false value) is no key.
521 next if exists $info->{attrs}{is_foreign_key_constraint}
522 54 ▶ && !$info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
if exists $info->{"attrs"}{"is_foreign_key_constraint"} and not $info->{"attrs"}{"is_foreign_key_constraint"} 2 52 54
Condition100%3/3
exists $info->{"attrs"}{"is_foreign_key_constraint"} and not $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
1512
MC/DC100%2/2
exists $info->{"attrs"}{"is_foreign_key_constraint"} and not $info->{"attrs"}{"is_foreign_key_constraint"}
exists $info->{"attrs"}{"is_foreign_key_constraint"} not $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table100%3/3
A and not(B)
A= exists $info->{"attrs"}{"is_foreign_key_constraint"}B= not $info->{"attrs"}{"is_foreign_key_constraint"}
ABresult
0X0
100
111
523  
524 52 my ($self_cols, $foreign_cols) = _fk_column_pairs($info->{cond});
525 52 ▶ unless ($self_cols) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self_cols) 4 48 52
526 4 push @stmts, _note("skipped FK for rel $rel_name: non-column condition");
527 4 next;
528 }
529  
530 48 my $f_class = $info->{source};
531  
532 # A belongs_to declared with an explicit hashref condition skips
533 # ensure_class_loaded, so the foreign class may never have been loaded
534 # at all. The class that asked for the foreign key declared a target
535 # that is not there: that is refused, not noted (karr k120).
536 48 ▶ unless ($f_class->can('table')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f_class->can("table")) 5 43 48
537 5 $source->throw_exception(sprintf
538 "refusing to write the install DDL: relationship '%s' of %s is "
539 . "declared as a foreign key (pg_fk_constraints_from_rels) to class "
540 . "%s, which is not loaded -- load the class, set "
541 . "is_foreign_key_constraint => 0 on the relationship, or leave "
542 . "pg_fk_constraints_from_rels off for the class",
543 $rel_name, $table_name, $f_class);
544 }
545  
546 43 ▶ my $f_pg_schema = $f_class->can('pg_schema') ? $f_class->pg_schema : undef;
Branch: 1 branch
BranchTrueFalseTotal
$f_class->can('pg_schema') ? : 43 0 43
547 43 my $f_qualified = _relation_sql($f_pg_schema, $f_class->table);
548  
549 43 ▶ unless ($seen_table{$f_qualified}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_table{$f_qualified}) 3 40 43
550 3 $source->throw_exception(sprintf
551 "refusing to write the install DDL: relationship '%s' of %s is "
552 . "declared as a foreign key (pg_fk_constraints_from_rels) to "
553 . "table %s of class %s, which is not part of this schema deploy "
554 . "-- register the class with the schema, set "
555 . "is_foreign_key_constraint => 0 on the relationship, or leave "
556 . "pg_fk_constraints_from_rels off for the class",
557 $rel_name, $table_name, $f_qualified, $f_class);
558 }
559  
560 # The actions the belongs_to declares, as declared; none declared is
561 # no clause and the server's default (karr k127).
562 my $actions = join '', map {
563 40 _fk_action($source, $table_name, $rel_name, $_->[0], $info->{attrs}{ $_->[1] })
564 } [ 'ON DELETE', 'on_delete' ], [ 'ON UPDATE', 'on_update' ];
565 $actions .= _timing_clause($source, "relationship '$rel_name' of $table_name",
566 36 ▶ { map { defined $info->{attrs}{$_} ? ($_ => $info->{attrs}{$_}) : () } qw( deferrable initially_deferred ) },
Branch: 1 branch
BranchTrueFalseTotal
defined $info->{'attrs'}{$_} ? : 6 66 72
567 qw( deferrable initially_deferred ));
568  
569 # The name is made of the table and the columns. Two relationships over
570 # one key with the same actions are one constraint and are written
571 # once; with other actions, or to another table, they would need one
572 # name twice, which the server refuses -- so the driver does, before
573 # it writes anything (karr k127).
574 35 my $fk_name = join('_', $table_name, @$self_cols, 'fkey');
575 35 my $fk_key = join "\0", $f_qualified, @$foreign_cols, lc $actions;
576 35 ▶ if (defined(my $earlier = $fk_seen{$qualified}{$fk_name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $earlier = $fk_seen{$qualified}{$fk_name})) 3 32 35
577 3 ▶ next if $earlier->[0] eq $fk_key;
Branch: 1 branch
BranchTrueFalseTotal
if $earlier->[0] eq $fk_key 2 1 3
578 1 $source->throw_exception(sprintf
579 "refusing to write the install DDL: relationships '%s' and '%s' of %s "
580 . "declare the foreign key on (%s) twice, as constraint %s, with "
581 . "another target or other actions; the server holds one "
582 . "constraint of a name -- declare one of them",
583 $earlier->[1], $rel_name, $table_name, join(', ', @$self_cols), $fk_name);
584 }
585 32 $fk_seen{$qualified}{$fk_name} = [ $fk_key, $rel_name ];
586  
587 push @stmts, sprintf 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;',
588 $qualified, quote_ident($fk_name),
589 32 join(', ', map { quote_ident($_) } @$self_cols),
590 $f_qualified,
591 32 join(', ', map { quote_ident($_) } @$foreign_cols),
592 $actions;
593 }
594 }
595  
596 # 6. Views, each behind the views its deploy_depends_on names. Here and not
597 # at the end: a view without a pg_schema is created where the session
598 # puts it, like a table without one, under the path of the session --
599 # where the tables it was declared next to are (karr k120).
600 181 push @stmts, map { $_->{sql} } _views_in_dependency_order(\@views, \%view_of_class);
601  
602 # 7. Settings
603 179 my $settings = _schema_decl($schema, 'pg_settings');
604 179 ▶ if ($settings && %$settings) {
Branch: 1 branch
BranchTrueFalseTotal
if ($settings and %$settings) 24 155 179
Condition100%3/3
$settings and %$settings
!ll&&!rl&&r
115424
MC/DC100%2/2
$settings and %$settings
$settings %$settings
Truth table100%3/3
A and B
A= $settingsB= %$settings
ABresult
0X0
100
111
605 24 push @stmts, map { _setting_sql($_, $settings->{$_}) } sort keys %$settings;
606 }
607  
608 # 8. No search path. The script does not end with SET search_path: the
609 # statement would set the path of the session that runs it, the
610 # caller's, and nothing above needs it -- every object is created
611 # before. pg_search_path is not written (karr k121, P13); its entries
612 # are still checked, so a declaration that was refused stays refused.
613 # A connection that names its namespace has its path at step 1b, for
614 # the temporary database of a diff.
615 171 ▶ _search_path_entry($_) for defined $connection_path ? () : _schema_decl($schema, 'pg_search_path');
Branch: 1 branch
BranchTrueFalseTotal
defined $connection_path ? : 9 162 171
616  
617 167 return join("\n\n", map { _terminated($_) } @stmts) . "\n";
618 }
619  
620  
621 # Ask the schema for one of the schema-level declarations of the
622 # DBIO::PostgreSQL component (pg_extensions, pg_schemas, pg_schema_class,
623 # pg_settings, pg_search_path). A schema that does not load the component has
624 # none of them -- deploy_async hands install_ddl such a schema on a connection
625 # opened with { async => ... } and no load_components('PostgreSQL')
626 # (dbio-postgresql-ev k36) -- and is answered by the component class itself:
627 # its defaults, so "not loaded" and "loaded, nothing declared" produce the
628 # same DDL by construction rather than by a second copy of the defaults here
629 # (karr k82). Per method, like the result-class can() checks in install_ddl.
630 # DBIO::PostgreSQL::Deploy asks its own pg_extensions/pg_schemas through this
631 # too, fully qualified, so both paths share one fallback (karr k87).
632 # The namespaces the script writes: the declared pg_schemas in declaration
633 # order, then -- name order -- a bound one no list names (a binding alone
634 # declares nothing and restricts nothing; only pg_schemas does).
635 428 ▶sub _namespaces {
Subroutine _namespaces: called CC=3 SCAR=11.1
636 428 my ($schema) = @_;
637 428 my @declared = _schema_decl($schema, 'pg_schemas');
638 428 my %have = map { $_ => 1 } @declared;
639 428 ▶ my $bound = _schema_decl($schema, '_pg_schema_classes') // {};
Condition50%1/2
_schema_decl($schema, "_pg_schema_classes") // {}
l!l
4280
MC/DC0%0/1
_schema_decl($schema, "_pg_schema_classes") // {}
_schema_decl($schema, "_pg_schema_classes")
Truth table50%1/2
A
A= _schema_decl($schema, "_pg_schema_classes")
Aresult
11
00
640 428 ▶ return (@declared, sort grep { !$have{$_} && defined $bound->{$_} } keys %$bound);
Condition66%2/3
!$have{$_} && defined $bound->{$_}
!ll&&!rl&&r
1202
MC/DC50%1/2
!$have{$_} && defined $bound->{$_}
!$have{$_} defined $bound->{$_}
Truth table66%2/3
not(A) && B
A= !$have{$_}B= defined $bound->{$_}
ABresult
0X0
100
111
641 }
642  
643  
644 3 ▶sub refuse_control_schema {
Subroutine refuse_control_schema: called CC=1 SCAR=0.0
Pod documented
645 3 my ($ext, $declared, $fixed) = @_;
646 3 die sprintf
647 "refusing to write the install DDL: extension %s is declared in schema "
648 . "%s, but its control file fixes schema %s and the server installs it "
649 . "nowhere else. Declare pg_extensions(%s => { schema => '%s' }) or no "
650 . "schema.\n",
651 $ext, $declared, $fixed, $ext, $fixed;
652 }
653  
654  
655 14 ▶sub control_schemas_sql {
Subroutine control_schemas_sql: called CC=1 SCAR=0.0
Pod documented
656 14 my (@names) = @_;
657 return q{SELECT v.name, v.schema
658 FROM pg_catalog.pg_available_extension_versions v
659 JOIN pg_catalog.pg_available_extensions a
660 ON a.name = v.name AND a.default_version = v.version
661 14 WHERE v.name IN (} . join(', ', map { quote_literal($_) } @names)
662 . q{) AND v.schema IS NOT NULL};
663 }
664  
665  
666 2 ▶sub check_control_schemas {
Subroutine check_control_schemas: called CC=6 SCAR=18.4
Pod documented
667 2 my ($schema, $fixed) = @_;
668 2 ▶ my $given = _schema_decl($schema, 'pg_extension_schemas') // {};
Condition50%1/2
_schema_decl($schema, "pg_extension_schemas") // {}
l!l
20
MC/DC0%0/1
_schema_decl($schema, "pg_extension_schemas") // {}
_schema_decl($schema, "pg_extension_schemas")
Truth table50%1/2
A
A= _schema_decl($schema, "pg_extension_schemas")
Aresult
11
00
669 2 for my $name (_schema_decl($schema, 'pg_extensions')) {
670 refuse_control_schema($name, $given->{$name}, $fixed->{$name})
671 if defined $given->{$name} && defined $fixed->{$name}
672 3 ▶ && $given->{$name} ne $fixed->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $given->{$name} and defined $fixed->{$name} and $given->{$name} ne $fixed->{$name} 1 2 3
Condition66%4/6
defined $given->{$name} and defined $fixed->{$name} and $given->{$name} ne $fixed->{$name}
!ll&&!rl&&r
201
defined $given->{$name} and defined $fixed->{$name}
!ll&&!rl&&r
201
MC/DC33%1/3
defined $given->{$name} and defined $fixed->{$name} and $given->{$name} ne $fixed->{$name}
defined $given->{$name} defined $fixed->{$name} $given->{$name} ne $fixed->{$name}
Truth table50%2/4
A and B and C
A= defined $given->{$name}B= defined $fixed->{$name}C= $given->{$name} ne $fixed->{$name}
ABCresult
0XX0
10X0
1100
1111
673 }
674 1 return;
675 }
676  
677 2884 ▶sub _schema_decl {
Subroutine _schema_decl: called CC=2 SCAR=6.9
678 2884 my ($schema, $method, @args) = @_;
679 2884 ▶ my $from = $schema->can($method) ? $schema : 'DBIO::PostgreSQL';
Branch: 1 branch
BranchTrueFalseTotal
$schema->can($method) ? : 2852 32 2884
680 2884 return $from->$method(@args);
681 }
682  
683 # The namespace the connection of $schema works in: what its connect info
684 # gave as schema (core ADR 0065), or undef -- for a connection without the
685 # key, a schema that is not connected and a schema class. It is a fact of
686 # the connection and is read from the storage, never from the class: the
687 # same class is connected for several namespaces at once.
688 # DBIO::PostgreSQL::Deploy asks through this too.
689 316 ▶sub _connection_namespace {
Subroutine _connection_namespace: called CC=4 SCAR=13.9
690 316 my ($schema) = @_;
691 316 ▶ my $storage = $schema->can('storage') ? $schema->storage : undef;
Branch: 1 branch
BranchTrueFalseTotal
$schema->can('storage') ? : 315 1 316
692 return undef
693 316 ▶ unless Scalar::Util::blessed($storage) && $storage->can('connection_schema');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($storage) and $storage->can("connection_schema") 241 75 316
Condition66%2/3
builtin::blessed($storage) and $storage->can("connection_schema")
!ll&&!rl&&r
241075
MC/DC50%1/2
builtin::blessed($storage) and $storage->can("connection_schema")
builtin::blessed($storage) $storage->can("connection_schema")
Truth table66%2/3
A and B
A= builtin::blessed($storage)B= $storage->can("connection_schema")
ABresult
0X0
100
111
694 75 return $storage->connection_schema;
695 }
696  
697 # On a connection that names its namespace the class names none: what it
698 # declares is written without a schema and lands where the connection works.
699 # A declaration that names a namespace of its own -- a pg_schema_class
700 # binding, the pg_schema of a result class, a source name that carries its
701 # schema -- says something else, and which of the two is meant is nobody's to
702 # guess: refused, naming both.
703 14 ▶sub _refuse_namespace_declarations {
Subroutine _refuse_namespace_declarations: called CC=9 SCAR=22.1
704 14 my ($schema, $namespace) = @_;
705  
706 14 ▶ my $bound = _schema_decl($schema, '_pg_schema_classes') // {};
Condition50%1/2
_schema_decl($schema, "_pg_schema_classes") // {}
l!l
140
MC/DC0%0/1
_schema_decl($schema, "_pg_schema_classes") // {}
_schema_decl($schema, "_pg_schema_classes")
Truth table50%1/2
A
A= _schema_decl($schema, "_pg_schema_classes")
Aresult
11
00
707 14 for my $ns (sort grep { defined $bound->{$_} } keys %$bound) {
708 die sprintf
709 "refusing to write the install DDL: pg_schema_class binds schema %s to "
710 . "%s, and this connection works in the namespace %s (connect key "
711 . "schema). A schema class that names its namespaces is not installed "
712 . "over a connection that names one: connect without the key, or leave "
713 . "the binding out of the class.\n",
714 1 $ns, $bound->{$ns}, $namespace;
715 }
716  
717 13 for my $source_name (sort $schema->sources) {
718 13 my $source = $schema->source($source_name);
719 13 my $result_class = $source->result_class;
720 13 my $name = $source->name;
721 13 ▶ my $pg_schema = $result_class->can('pg_schema') ? $result_class->pg_schema : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result_class->can('pg_schema') ? : 12 1 13
722  
723 13 ▶ die sprintf
Branch: 1 branch
BranchTrueFalseTotal
if defined $pg_schema and length $pg_schema 1 12 13
Condition66%2/3
defined $pg_schema and length $pg_schema
!ll&&!rl&&r
1201
MC/DC50%1/2
defined $pg_schema and length $pg_schema
defined $pg_schema length $pg_schema
Truth table66%2/3
A and B
A= defined $pg_schemaB= length $pg_schema
ABresult
0X0
100
111
724 "refusing to write the install DDL: source %s (%s) declares pg_schema "
725 . "%s, and this connection works in the namespace %s (connect key "
726 . "schema). A source that names its schema is not installed over a "
727 . "connection that names one: connect without the key, or leave the "
728 . "pg_schema out of the class.\n",
729 $source_name, $result_class, $pg_schema, $namespace
730 if defined $pg_schema && length $pg_schema;
731  
732 12 ▶ die sprintf
Branch: 1 branch
BranchTrueFalseTotal
if not ref $name and $name =~ /\./ 1 11 12
Condition66%2/3
not ref $name and $name =~ /\./
!ll&&!rl&&r
0111
MC/DC50%1/2
not ref $name and $name =~ /\./
not ref $name $name =~ /\./
Truth table66%2/3
not(A) and B
A= not ref $nameB= $name =~ /\./
ABresult
0X0
100
111
733 "refusing to write the install DDL: source %s (%s) is the table %s, a "
734 . "name that carries its schema, and this connection works in the "
735 . "namespace %s (connect key schema). A source that names its schema "
736 . "is not installed over a connection that names one: connect without "
737 . "the key, or name the table without a schema.\n",
738 $source_name, $result_class, $name, $namespace
739 if !ref $name && $name =~ /\./;
740 }
741  
742 11 return;
743 }
744  
745 # One piece of the script with the semicolon that ends it -- one, and where
746 # it does end the piece. install_ddl passes everything it has collected
747 # through here, so what is "a statement of the script" is said once.
748 #
749 # Declared SQL (pg_function, pg_extra_ddl, a view definition, an index's
750 # where) comes with or without a semicolon and may close with a line comment.
751 # A semicolon written behind that comment is part of the comment: the piece
752 # has no end then and runs into the statement after it -- for the server
753 # reading the script as one piece (psql -f, a single do()), and for the
754 # deploy, whose splitter reads a comment as the server does
755 # (DBIO::SQL::Util::_split_statements since core k326; the one before cut at
756 # every semicolon, in a comment or not, and was the only reader that did not
757 # notice). There the semicolon goes on a line of its own, which ends the
758 # piece for all of them.
759 495 ▶sub _terminated {
Subroutine _terminated: called CC=2 SCAR=6.9
760 495 my ($sql) = @_;
761 495 $sql =~ s/\A\s+//;
762 495 $sql =~ s/[\s;]+\z//;
763 495 ▶ return $sql . ($sql =~ /--[^\n]*\z/ ? "\n;" : ';');
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /--[^\n]*\z/ ? : 7 488 495
764 }
765  
766 # A note for whoever reads the script, as a piece of its own: a line comment,
767 # which _terminated closes with a semicolon on the next line. Read as one
768 # piece (psql -f, a single do()) that is an empty statement. The deploy sends
769 # the server no such piece, under either of its two ways of reading a script:
770 #
771 # - since core k326 the splitter reads comments: it takes one off the
772 # statement it stands ahead of and delivers no piece that holds nothing
773 # else. There the note would be harmless without a semicolon of its own;
774 # - before that DBIO::Deploy::Base::_execute_ddl cut at every semicolon and
775 # left out each piece that began with "--". Without its own semicolon
776 # the note was the head of the statement behind it, and that statement
777 # was left out with it: the next foreign key, the settings, the
778 # search_path (karr k120).
779 #
780 # The text is kept to one line, since past a line break it would be SQL for
781 # every reader. A semicolon in it is written out for the second reading
782 # alone, which cut the note there and ran the rest of it as a statement.
783 4 ▶sub _note {
Subroutine _note: called CC=1 SCAR=0.0
784 4 my ($text) = @_;
785 4 $text =~ s/\s*[\r\n]+\s*/ /g;
786 4 $text =~ s/;/<semicolon>/g;
787 4 return "-- $text";
788 }
789  
790 # One entry of pg_search_path. An entry is a name and is written by the rule
791 # of every other name, quote_ident -- so MySchema stays MySchema. $user is the
792 # one entry that is no name: it stands for the schema of the session's role
793 # and is written as it is. An entry the user has quoted already would be
794 # quoted twice into another schema, so it is refused with the way out.
795 168 ▶sub _search_path_entry {
Subroutine _search_path_entry: called CC=10 SCAR=23.3
796 168 my ($entry) = @_;
797 168 ▶ die sprintf
Branch: 3 branches
BranchTrueFalseTotal
unless defined $entry and not ref $entry and length $entry and not $entry =~ /\0/ 2 166 168
defined $entry && !ref($entry) ? : 1 1 2
defined $entry ? : 1 0 1
Condition75%9/12
defined $entry and not ref $entry and length $entry and not $entry =~ /\0/
!ll&&!rl&&r
20166
defined $entry and not ref $entry and length $entry
!ll&&!rl&&r
11166
defined $entry and not ref $entry
!ll&&!rl&&r
01167
defined $entry and not ref $entry
!ll&&!rl&&r
011
MC/DC50%2/4
defined $entry and not ref $entry and length $entry and not $entry =~ /\0/
defined $entry not ref $entry length $entry not $entry =~ /\0/
Truth table60%3/5
A and not(B) and C and not(D)
A= defined $entryB= not ref $entryC= length $entryD= not $entry =~ /\0/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
798 "refusing to write the install DDL: pg_search_path entry %s is not a "
799 . "name. Entries are schema names; write each as a plain string.\n",
800 defined $entry && !ref $entry ? "'$entry'" : (defined $entry ? 'a reference' : 'undef')
801 unless defined $entry && !ref $entry && length $entry && $entry !~ /\0/;
802 166 ▶ return $entry if $entry eq '$user';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "\$user" 1 165 166
803 165 ▶ _refuse_quoted_name('pg_search_path entry', $entry, substr($entry, 1, -1),
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /\A"/ 2 163 165
804 'Entries are names', 'another schema')
805 if $entry =~ /\A"/;
806 163 return quote_ident($entry);
807 }
808  
809 # One key of pg_settings, as SQL. A key is a name by the rule of
810 # pg_search_path, and a dotted one is a dotted name, as the server's grammar
811 # reads it (var_name: ColId, then '.' ColId): each part is written by
812 # quote_ident for itself, so app.current_user_id stays bare and DateStyle
813 # becomes "DateStyle". A part the user has quoted already is refused, as an
814 # entry of pg_search_path is.
815 28 ▶sub _setting_key {
Subroutine _setting_key: called CC=10 SCAR=24.8
816 28 my ($key) = @_;
817 28 ▶ my @parts = defined $key && !ref $key && $key !~ /\0/ ? split(/\./, $key, -1) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $key && !ref($key) && !($key =~ /\0/) ? : 27 1 28
Condition50%3/6
defined $key and not ref $key and not $key =~ /\0/
!ll&&!rl&&r
0127
defined $key and not ref $key
!ll&&!rl&&r
0028
MC/DC33%1/3
defined $key and not ref $key and not $key =~ /\0/
defined $key not ref $key not $key =~ /\0/
Truth table50%2/4
A and not(B) and not(C)
A= defined $keyB= not ref $keyC= not $key =~ /\0/
ABCresult
0XX0
10X0
1100
1111
818 die sprintf
819 "refusing to write the install DDL: pg_settings key %s is not a name. "
820 . "Keys are parameter names, parts joined by a dot; write each part as "
821 . "a plain, non-empty string.\n",
822 defined $key && !ref $key ? "'$key'" : (defined $key ? 'a reference' : 'undef')
823 28 ▶ if !@parts || grep { !length } @parts;
Branch: 3 branches
BranchTrueFalseTotal
if not @parts or grep {not length $_;} @parts 5 23 28
defined $key && !ref($key) ? : 5 0 5
defined $key ? : 0 0 0
Condition66%4/6
not @parts or grep {not length $_;} @parts
l!l&&r!l&&!r
2323
defined $key and not ref $key
!ll&&!rl&&r
005
MC/DC100%2/2
not @parts or grep {not length $_;} @parts
not @parts grep {not length $_;} @parts
MC/DC0%0/2
defined $key and not ref $key
defined $key not ref $key
Truth table100%3/3
not(A) or B
A= not @partsB= grep {not length $_;} @parts
ABresult
1X1
011
000
Truth table33%1/3
A and not(B)
A= defined $keyB= not ref $key
ABresult
0X0
100
111
824 _refuse_quoted_name('pg_settings key', $key, $key =~ tr/"//dr,
825 'Keys are names', 'another parameter')
826 23 ▶ if grep { /\A"/ } @parts;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/\A"/;} @parts 3 20 23
827 20 return join '.', map { quote_ident($_) } @parts;
828 }
829  
830 # A name a declaration gives that is in double quotes already: quoting it by
831 # the rule of every name would make another name of it.
832 5 ▶sub _refuse_quoted_name {
Subroutine _refuse_quoted_name: called CC=1 SCAR=0.0
833 5 my ($what, $name, $unquoted, $are, $into) = @_;
834 5 die sprintf
835 "refusing to write the install DDL: %s %s is already quoted. %s and are "
836 . "quoted by the driver, which would quote it a second time into %s; "
837 . "write the name without quotes (%s).\n",
838 $what, $name, $are, $into, $unquoted;
839 }
840  
841 # One entry of pg_settings as a statement. A setting is stored on a database
842 # with ALTER DATABASE <name> SET, which takes a name and nothing that stands
843 # for "this one"; the script is written without a connection and also runs in
844 # the temporary database of Deploy->diff, so it cannot hold that name. The
845 # server is asked for it where the statement runs: a DO block builds the
846 # ALTER DATABASE around current_database().
847 #
848 # The key is a name (_setting_key) and goes into the statement as SQL (%s),
849 # handed to format() as a literal; the value is a literal (%L).
850 #
851 # The block is dollar-quoted with a tag its text does not contain, so a value
852 # cannot end it; and it is one piece for the deploy, whose splitter has read
853 # dollar quoting before it read anything else that could hold a semicolon
854 # (literals and comments since core k326).
855 28 ▶sub _setting_sql {
Subroutine _setting_sql: called CC=2 SCAR=6.9
856 28 my ($key, $value) = @_;
857 28 my $body = sprintf
858 "BEGIN\n EXECUTE format('ALTER DATABASE %%I SET %%s = %%L', current_database(), %s, %s);\nEND",
859 quote_literal(_setting_key($key)), quote_literal($value);
860  
861 20 my ($tag, $n) = ('dbio', 0);
862 20 ▶ $tag = 'dbio' . ++$n while index($body, "\$$tag\$") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
index($body, "\$$tag\$") != -1 and do { $tag = 'dbio' . ++$n } 2 20 22
863 20 return "DO \$$tag\$\n$body\n\$$tag\$";
864 }
865  
866 # The result classes a view source names in deploy_depends_on. A class
867 # declaration hands DBIO::ResultSource::View->new an arrayref, which makes a
868 # hash of it; set on the source afterwards it stays what it was given.
869 25 ▶sub _view_depends_on {
Subroutine _view_depends_on: called CC=5 SCAR=20.9
870 25 my ($source) = @_;
871 25 my $on = $source->deploy_depends_on;
872 25 ▶ return sort keys %$on if ref $on eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $on eq "HASH" 24 1 25
873 1 ▶ return sort @$on if ref $on eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $on eq "ARRAY" 1 0 1
874 0 ▶ return defined $on && !ref $on ? ($on) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $on && !ref($on) ? : 0 0 0
Condition0%0/3
defined $on and not ref $on
!ll&&!rl&&r
000
MC/DC0%0/2
defined $on and not ref $on
defined $on not ref $on
Truth table0%0/3
A and not(B)
A= defined $onB= not ref $on
ABresult
0X0
100
111
875 }
876  
877 # The views in the order they can be created: each behind the views its
878 # deploy_depends_on names, and otherwise in the order they came in (that of
879 # their source names). $view_of_class says which view a result class is. A
880 # class that is no view of this script orders nothing -- a table is there
881 # before every view, and what the schema does not deploy is not the script's
882 # to create.
883 #
884 # deploy_depends_on is all there is to go by: a view definition is SQL as
885 # given, and which relations it reads only the server can say.
886 181 ▶sub _views_in_dependency_order {
Subroutine _views_in_dependency_order: called CC=2 SCAR=6.9
887 181 my ($views, $view_of_class) = @_;
888 my %state = (
889 181 view => { map { $_->{relation} => $_ } @$views },
890 class => $view_of_class,
891 done => {},
892 path => [],
893 order => [],
894 );
895 181 _visit_view(\%state, $_) for @$views;
896 179 return @{ $state{order} };
897 }
898  
899 32 ▶sub _visit_view {
Subroutine _visit_view: called CC=6 SCAR=17.9
900 32 my ($state, $view) = @_;
901 32 my $relation = $view->{relation};
902 32 ▶ return if $state->{done}{$relation};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"done"}{$relation} 6 26 32
903  
904 # Met again on the way down from itself: the views from there to here need
905 # one another in a circle.
906 26 my $path = $state->{path};
907 26 my ($from) = grep { $path->[$_]{relation} eq $relation } 0 .. $#$path;
908 26 ▶ _refuse_view_circle(@{$path}[ $from .. $#$path ]) if defined $from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from 2 24 26
909  
910 24 push @$path, $view;
911 24 for my $class (@{ $view->{depends_on} }) {
912 13 ▶ my $on = $state->{view}{ $state->{class}{$class} // '' } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $on = $state->{"view"}{$state->{"class"}{$class} // ""} or next 10 3 13
Condition100%2/2
$state->{"class"}{$class} // ""
l!l
103
MC/DC100%1/1
$state->{"class"}{$class} // ""
$state->{"class"}{$class}
Truth table100%2/2
A
A= $state->{"class"}{$class}
Aresult
11
00
913 10 _visit_view($state, $on);
914 }
915 20 pop @$path;
916  
917 20 $state->{done}{$relation} = 1;
918 20 push @{ $state->{order} }, $view;
919 }
920  
921 2 ▶sub _refuse_view_circle {
Subroutine _refuse_view_circle: called CC=1 SCAR=0.0
922 2 my (@circle) = @_;
923 my @needs = map {
924 2 "$circle[$_]{relation} needs $circle[ ($_ + 1) % @circle ]{relation}"
925 } 0 .. $#circle;
926 die join "\n",
927 'refusing to write the install DDL: deploy_depends_on runs in a circle through '
928 2 . join(', ', map { "view $_->{relation} ($_->{class})" } @circle),
929 '',
930 'A view is created behind the views its deploy_depends_on names. As declared,',
931 join(', ', @needs) . '.',
932 'No order creates these views, and PostgreSQL has no view that reads itself. No',
933 'script was written.',
934 '',
935 'Take out of a deploy_depends_on the class of a view that view does not read.',
936 '';
937 }
938  
939 # The names a relation is made of: the pg_schema of its result class, if it
940 # has one, then the source name taken apart at the name separator. That is
941 # how the SQL maker reads a source name for a query -- table('auth.users') is
942 # "auth"."users" there -- so the DDL arrives at the relation the queries read.
943 532 ▶sub _relation_parts {
Subroutine _relation_parts: called CC=2 SCAR=6.9
944 532 my ($pg_schema_name, $name) = @_;
945 532 ▶ return (($pg_schema_name ? $pg_schema_name : ()), split /\./, $name);
Branch: 1 branch
BranchTrueFalseTotal
$pg_schema_name ? : 28 504 532
946 }
947  
948 # A relation as it stands in a statement: every part of _relation_parts an
949 # identifier of its own. A scalar-ref source name is literal SQL -- the SQL
950 # maker writes it unquoted as well -- and stays as given.
951 524 ▶sub _relation_sql {
Subroutine _relation_sql: called CC=3 SCAR=15.4
952 524 my ($pg_schema_name, $name) = @_;
953 524 ▶ return quote_qualified(_relation_parts($pg_schema_name, $name))
Branch: 1 branch
BranchTrueFalseTotal
unless ref $name eq "SCALAR" 524 0 524
954 unless ref $name eq 'SCALAR';
955 0 ▶ return join '.', ($pg_schema_name ? quote_ident($pg_schema_name) : ()), $$name;
Branch: 1 branch
BranchTrueFalseTotal
$pg_schema_name ? : 0 0 0
956 }
957  
958 636 ▶sub _pg_column_type {
Subroutine _pg_column_type: called CC=11 SCAR=25.2
959 636 my ($info, $context) = @_;
960 636 my $type = $info->{data_type};
961  
962 # Handle enum types
963 636 ▶ if ($type eq 'enum' && $info->{pg_enum_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "enum" and $info->{"pg_enum_type"}) 0 636 636
Condition33%1/3
$type eq "enum" and $info->{"pg_enum_type"}
!ll&&!rl&&r
63600
MC/DC0%0/2
$type eq "enum" and $info->{"pg_enum_type"}
$type eq "enum" $info->{"pg_enum_type"}
Truth table33%1/3
A and B
A= $type eq "enum"B= $info->{"pg_enum_type"}
ABresult
0X0
100
111
964 0 return $info->{pg_enum_type};
965 }
966  
967 # Handle composite types
968 636 ▶ if ($type eq 'composite' && $info->{pg_type_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "composite" and $info->{"pg_type_name"}) 0 636 636
Condition33%1/3
$type eq "composite" and $info->{"pg_type_name"}
!ll&&!rl&&r
63600
MC/DC0%0/2
$type eq "composite" and $info->{"pg_type_name"}
$type eq "composite" $info->{"pg_type_name"}
Truth table33%1/3
A and B
A= $type eq "composite"B= $info->{"pg_type_name"}
ABresult
0X0
100
111
969 0 return $info->{pg_type_name};
970 }
971  
972 # Handle arrays
973 636 ▶ return $type if $type =~ /\[\]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\[\]$/ 1 635 636
974  
975 # Handle parameterized types (varchar(N), numeric(P,S), vector(N))
976 635 ▶ return $type if $type =~ /\(.+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\(.+\)$/ 4 631 635
977  
978 # Portable base types: delegate to the adapter (single source of truth).
979 # The canonical column carries a size for char and numeric only, so the
980 # declared one goes along here: the adapter writes it where the native type
981 # takes a modifier -- a timestamp's precision -- and nowhere else.
982 631 ▶ if (DBIO::Schema::Type::is_base_type(lc $type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (DBIO::Schema::Type::is_base_type(lc $type)) 494 137 631
983 494 my $canon = DBIO::Schema::Type::canonical_column('_', $info);
984 494 ▶ $canon->{size} = $info->{size} unless exists $canon->{size};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $canon->{"size"} 488 6 494
985 494 return $ADAPTER->to_native($canon, $context);
986 }
987  
988 # Legacy PG-dialect aliases: pass through as their native PG names
989 137 my %type_map = (
990 bigint => 'bigint',
991 smallint => 'smallint',
992 serial => 'serial',
993 bigserial => 'bigserial',
994 varchar => 'character varying',
995 float => 'double precision',
996 real => 'real',
997 date => 'date',
998 'timestamp with time zone' => 'timestamp with time zone',
999 timestamptz => 'timestamp with time zone',
1000 uuid => 'uuid',
1001 json => 'json',
1002 jsonb => 'jsonb',
1003 bytea => 'bytea',
1004 inet => 'inet',
1005 cidr => 'cidr',
1006 macaddr => 'macaddr',
1007 tsvector => 'tsvector',
1008 tsquery => 'tsquery',
1009 xml => 'xml',
1010 money => 'money',
1011 interval => 'interval',
1012 point => 'point',
1013 line => 'line',
1014 lseg => 'lseg',
1015 box => 'box',
1016 path => 'path',
1017 polygon => 'polygon',
1018 circle => 'circle',
1019 );
1020  
1021 137 ▶ my $native = $type_map{$type} // $type;
Condition66%2/3
$type_map{$type} // $type
l!l&&r!l&&!r
78590
MC/DC0%0/2
$type_map{$type} // $type
$type_map{$type} $type
Truth table66%2/3
A // B
A= $type_map{$type}B= $type
ABresult
1X1
011
000
1022  
1023 # Honor the `size` column-info attribute (DBIO::Core: "the length of your
1024 # column, if it is a column type that can have a size restriction";
1025 # DBIO::ResultSource: an ArrayRef selects precision/scale for decimal
1026 # types). Which type takes one, and where it is written, is the rule of
1027 # DBIO::PostgreSQL::Util -- every built-in type with a type modifier under
1028 # each of its names, and the pgvector types (karr k56, k58, k116). The
1029 # portable base types above reach the same rule through the adapter.
1030 #
1031 # Every other type keeps going without its size: a built-in one has no
1032 # modifier (integer(11) is a syntax error), and of a type nobody here
1033 # knows nothing can be said.
1034 137 ▶ return type_with_modifier($native, $info->{size}, $context)
Branch: 1 branch
BranchTrueFalseTotal
if type_takes_modifier($native) 101 36 137
1035 if type_takes_modifier($native);
1036  
1037 36 return $native;
1038 }
1039  
1040 # The timing clause of a UNIQUE constraint from the options of
1041 # pg_unique_options, or of a FOREIGN KEY from the attributes of its belongs_to
1042 # ($what names the one for the refusal). Not declared is no clause. Another
1043 # option than deferrable and initially_deferred (@known), and
1044 # initially_deferred without deferrable, are refused here, not left to the
1045 # server (karr k147).
1046 56 ▶sub _timing_clause {
Subroutine _timing_clause: called CC=5 SCAR=16.1
1047 56 my ($source, $what, $options, @known) = @_;
1048 56 ▶ $options //= {};
Condition100%2/2
$options //= {}
l!l
4115
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
1049 56 ▶ if (my @unknown = grep { my $k = $_; !grep { $k eq $_ } @known } sort keys %$options) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = grep({my $k = $_; not grep({$k eq $_;} @known);} sort(keys %$options))) 1 55 56
1050 1 $source->throw_exception(sprintf
1051 "refusing to write the install DDL: %s declares the option '%s', which is none of %s",
1052 $what, $unknown[0], join(', ', @known));
1053 }
1054 $source->throw_exception(sprintf
1055 "refusing to write the install DDL: %s declares initially_deferred without deferrable; "
1056 . "PostgreSQL has no constraint that is INITIALLY DEFERRED and NOT DEFERRABLE",
1057 55 ▶ $what) if $options->{initially_deferred} && !$options->{deferrable};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"initially_deferred"} and not $options->{"deferrable"} 2 53 55
Condition100%3/3
$options->{"initially_deferred"} and not $options->{"deferrable"}
!ll&&!rl&&r
5122
MC/DC100%2/2
$options->{"initially_deferred"} and not $options->{"deferrable"}
$options->{"initially_deferred"} not $options->{"deferrable"}
Truth table100%3/3
A and not(B)
A= $options->{"initially_deferred"}B= not $options->{"deferrable"}
ABresult
0X0
100
111
1058 53 return deferrable_clause($options->{deferrable}, $options->{initially_deferred});
1059 }
1060  
1061 20 ▶sub _unique_timing {
Subroutine _unique_timing: called CC=1 SCAR=0.0
1062 20 my ($source, $table_name, $name, $options) = @_;
1063 20 return _timing_clause($source, "pg_unique_options of constraint '$name' of $table_name",
1064 $options, qw( deferrable initially_deferred ));
1065 }
1066  
1067 # The " ON DELETE x" / " ON UPDATE x" tail of a FOREIGN KEY: the action the
1068 # belongs_to declares, as declared. Not declared -- absent, undef, '' -- is
1069 # no clause. The word is one of the five PostgreSQL has, in any case; another
1070 # is refused here, naming the relationship, and not left to be a syntax error
1071 # of the statement, or text that reaches the script (karr k140).
1072 76 ▶sub _fk_action {
Subroutine _fk_action: called CC=4 SCAR=13.9
1073 76 my ($source, $table_name, $rel_name, $event, $action) = @_;
1074 76 ▶ return '' unless defined $action && length $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action and length $action 54 22 76
Condition66%2/3
defined $action and length $action
!ll&&!rl&&r
54022
MC/DC50%1/2
defined $action and length $action
defined $action length $action
Truth table66%2/3
A and B
A= defined $actionB= length $action
ABresult
0X0
100
111
1075 22 (my $word = uc $action) =~ s/\s+/ /g;
1076 22 ▶ unless (grep { $word eq $_ } 'NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL', 'SET DEFAULT') {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$word eq $_;} "NO ACTION", "RESTRICT", "CASCADE", "SET NULL", "SET DEFAULT") 4 18 22
1077 4 (my $attr = lc $event) =~ tr/ /_/;
1078 4 $source->throw_exception(sprintf
1079 "refusing to write the install DDL: relationship '%s' of %s declares %s => '%s', "
1080 . "which is no action of a foreign key; PostgreSQL knows NO ACTION, RESTRICT, "
1081 . "CASCADE, SET NULL, SET DEFAULT",
1082 $rel_name, $table_name, $attr, $action);
1083 }
1084 18 return " $event $action";
1085 }
1086  
1087 # Reduce a belongs_to relationship_info condition to the column lists a
1088 # plain FOREIGN KEY can express. Returns ($self_cols, $foreign_cols), both
1089 # arrayrefs ordered self-column-name-sorted for determinism, or an empty
1090 # list if $cond is anything other than a hashref of plain
1091 # 'foreign.col' => 'self.col' pairs (e.g. a coderef or a hash with an
1092 # expression on either side) -- add_relationship already enforces the
1093 # foreign./self. prefixes, so the only real-world "complex" case is a
1094 # non-hash condition.
1095 52 ▶sub _fk_column_pairs {
Subroutine _fk_column_pairs: called CC=8 SCAR=21.6
1096 52 my ($cond) = @_;
1097 52 ▶ return unless ref $cond eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" 4 48 52
1098  
1099 48 my %self_to_foreign;
1100 48 for my $key (keys %$cond) {
1101 48 my $val = $cond->{$key};
1102 48 ▶ return if ref $key || ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $key or ref $val 0 48 48
Condition33%1/3
ref $key or ref $val
l!l&&r!l&&!r
0048
MC/DC0%0/2
ref $key or ref $val
ref $key ref $val
Truth table33%1/3
A or B
A= ref $keyB= ref $val
ABresult
1X1
011
000
1103 48 ▶ return unless $key =~ /^foreign\.(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^foreign\.(\w+)$/ 0 48 48
1104 48 my $f_col = $1;
1105 48 ▶ return unless $val =~ /^self\.(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^self\.(\w+)$/ 0 48 48
1106 48 $self_to_foreign{$1} = $f_col;
1107 }
1108 48 ▶ return unless %self_to_foreign;
Branch: 1 branch
BranchTrueFalseTotal
unless %self_to_foreign 0 48 48
1109  
1110 48 my @self_cols = sort keys %self_to_foreign;
1111 48 return (\@self_cols, [ map { $self_to_foreign{$_} } @self_cols ]);
1112 }
1113  
1114 124 1;
1115  
1116 __END__
« blib/lib/DBIO/PostgreSQL/Introspect/Indexes.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/ForeignKeys.pm »