blib/lib/DBIO/PostgreSQL/Diff.pm

filter: Statementstmt 90.0% 163 / 181 Branchbran 71.2% 57 / 80 Conditioncond 66.1% 43 / 65 MC/DCmcdc 45.9% 17 / 37 Subroutinesub 100% 27 / 27 Podpod 100% 4 / 4 totaltotal 78.9% 311 / 394 CC 70 SCAR 32.1
CC
70
Cov
81%
_format_pg_default
68.8
_build_operations
25.2
register_diff_class
12.5
CRAP 24.8
« blib/lib/DBIO/PostgreSQL/Introspect.pm ↑ Summary blib/lib/DBIO/PostgreSQL/SQLMaker.pm »
1 package DBIO::PostgreSQL::Diff;
2 # ABSTRACT: Compare two introspected PostgreSQL models
3  
4 103 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 103 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 103 our $VERSION = '0.900007';
8  
9 103 ▶use base 'DBIO::Diff::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 103 ▶use DBIO::PostgreSQL::Diff::Schema ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 103 ▶use DBIO::PostgreSQL::Diff::Table ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 103 ▶use DBIO::PostgreSQL::Diff::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 103 ▶use DBIO::PostgreSQL::Diff::View ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 103 ▶use DBIO::PostgreSQL::Diff::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 103 ▶use DBIO::PostgreSQL::Diff::Sequence ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 103 ▶use DBIO::PostgreSQL::Diff::SequenceOwner ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 103 ▶use DBIO::PostgreSQL::Diff::Index ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 103 ▶use DBIO::PostgreSQL::Diff::ForeignKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 103 ▶use DBIO::PostgreSQL::Diff::Function ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 103 ▶use DBIO::PostgreSQL::Diff::Trigger ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 103 ▶use DBIO::PostgreSQL::Diff::Policy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 103 ▶use DBIO::PostgreSQL::Diff::Extension ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 103 ▶use DBIO::PostgreSQL::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 103 my %_REGISTRY = (
28 extensions => 'DBIO::PostgreSQL::Diff::Extension',
29 schemas => 'DBIO::PostgreSQL::Diff::Schema',
30 types => 'DBIO::PostgreSQL::Diff::Type',
31 sequences => 'DBIO::PostgreSQL::Diff::Sequence',
32 functions => 'DBIO::PostgreSQL::Diff::Function',
33 tables => 'DBIO::PostgreSQL::Diff::Table',
34 columns => 'DBIO::PostgreSQL::Diff::Column',
35 sequence_owners => 'DBIO::PostgreSQL::Diff::SequenceOwner',
36 views => 'DBIO::PostgreSQL::Diff::View',
37 indexes => 'DBIO::PostgreSQL::Diff::Index',
38 foreign_keys => 'DBIO::PostgreSQL::Diff::ForeignKey',
39 triggers => 'DBIO::PostgreSQL::Diff::Trigger',
40 policies => 'DBIO::PostgreSQL::Diff::Policy',
41 );
42  
43 # The order of dependency: each section after the ones it can depend on.
44 # Creates and changes run through it forwards, removals backwards -- see
45 # ORDER OF OPERATIONS. Schemas stand ahead of extensions because an extension
46 # depends on the schema it is installed into (pg_depend, deptype n), not the
47 # other way round (karr k106). Views stand behind the columns because a view
48 # reads columns, also ones that are added to a table that is there, and ahead
49 # of the indexes and triggers because a materialized view has indexes and a
50 # view has triggers (karr k100). Sequences stand ahead of the functions and
51 # the tables because a column default and the body of a function call a
52 # sequence by name, and what a sequence belongs to stands behind the columns
53 # because it can be given to a column only when that is there (karr k121).
54 103 my @_ORDER = qw(
55 schemas extensions types sequences functions tables columns sequence_owners
56 views indexes foreign_keys triggers policies
57 );
58  
59 # The section of the model a class reads when it is not the one of its own
60 # name: the model has no "views", it files a view among the tables, by kind,
61 # and what a sequence belongs to is in its entry among the sequences.
62 103 my %_MODEL_SECTION = (
63 views => 'tables',
64 sequence_owners => 'sequences',
65 );
66  
67 # Trailing context each table-aware diff class needs after its own
68 # (source, target) section pair. Diff::Table builds full CREATE TABLE
69 # statements and therefore needs the *column* data; Diff::Column and
70 # Diff::Policy need the *table* list for schema/table context; Diff::Index
71 # needs the *table* list to detect which tables are being dropped in the
72 # same pass, so it can suppress standalone DROP INDEX ops that DROP TABLE
73 # already covers (karr k32). Diff::Schema needs the *extension*
74 # list to see an extension it would drop along with a schema, and the
75 # *outside_dependents* for what stands in it (karr k152), as do Diff::Table
76 # and Diff::Type for what stands on a table or a type. Diff::Sequence reads
77 # the *schemas*, for the sequences of a schema that is dropped. Diff::Trigger
78 # needs the *table* list to see which views are dropped and created again,
79 # their triggers with them. Diff::Column also reads the *types*, to tell
80 # whether two composite types have the same attributes (karr k121).
81 # Diff::Table reads the *foreign_keys*, for the order of SET LOGGED / SET
82 # UNLOGGED (karr k121). Diff::Column reads the *policies*, *triggers* and
83 # *outside_readers* for what stops a column from changing its type
84 # (karr k121). A value
85 # that is a list names several sections, each handed over as its own
86 # (source, target) pair.
87 103 my %_AUX_SECTION = (
88 schemas => [qw( extensions outside_dependents )],
89 types => 'outside_dependents',
90 sequences => 'schemas',
91 tables => [qw( columns foreign_keys outside_dependents )],
92 columns => [qw( tables types policies triggers outside_readers )],
93 views => 'columns',
94 indexes => 'tables',
95 foreign_keys => 'tables',
96 triggers => [qw( tables columns )],
97 policies => 'tables',
98 );
99  
100 2 ▶sub _diff_registry { %_REGISTRY }
Subroutine _diff_registry: called CC=1 SCAR=0.0
101 1 ▶sub _diff_order { @_ORDER }
Subroutine _diff_order: called CC=1 SCAR=0.0
102  
103  
104 6 ▶sub diff_options { qw(foreign_keys_not_valid) }
Subroutine diff_options: called CC=1 SCAR=0.0
Pod documented
105  
106  
107 3 ▶sub register_diff_class {
Subroutine register_diff_class: called CC=8 SCAR=25.2
Pod documented
108 3 my ($class, %args) = @_;
109 3 ▶ my $key = $args{model_key} or die 'model_key required';
Branch: 1 branch
BranchTrueFalseTotal
my $key = $args{"model_key"} or die "model_key required" 3 0 3
110 3 ▶ my $diff_cls = $args{class} or die 'class required';
Branch: 1 branch
BranchTrueFalseTotal
my $diff_cls = $args{"class"} or die "class required" 3 0 3
111 3 my $position = $args{position};
112  
113 3 $_REGISTRY{$key} = $diff_cls;
114 3 @_ORDER = grep { $_ ne $key } @_ORDER;
115  
116 3 ▶ if (!defined $position) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $position) { } 1 2 3
elsif ($position =~ /\Aafter:(.+)\z/) { } 2 0 2
elsif ($position =~ /\Abefore:(.+)\z/) { } 0 0 0
117 1 push @_ORDER, $key;
118 } elsif ($position =~ /\Aafter:(.+)\z/) {
119 2 my $anchor = $1;
120 2 my ($i) = grep { $_ORDER[$_] eq $anchor } 0..$#_ORDER;
121 2 ▶ die "position anchor '$anchor' not found in diff order" unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 2 2
122 2 splice @_ORDER, $i + 1, 0, $key;
123 } elsif ($position =~ /\Abefore:(.+)\z/) {
124 0 my $anchor = $1;
125 0 my ($i) = grep { $_ORDER[$_] eq $anchor } 0..$#_ORDER;
126 0 ▶ die "position anchor '$anchor' not found in diff order" unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 0 0
127 0 splice @_ORDER, $i, 0, $key;
128 } else {
129 0 die "invalid position '$position' — use 'after:KEY' or 'before:KEY'";
130 }
131 }
132  
133 # True for an operation that takes an object away for good: a drop that is
134 # not the first half of a drop-and-recreate.
135 7642 ▶sub _is_removal {
Subroutine _is_removal: called CC=6 SCAR=18.0
136 7642 my ($op) = @_;
137 7642 ▶ return 0 unless $op->can('action') && ($op->action // '') eq 'drop';
Branch: 1 branch
BranchTrueFalseTotal
unless $op->can("action") and ($op->action // "") eq "drop" 7358 284 7642
Condition60%3/5
$op->can("action") and ($op->action // "") eq "drop"
!ll&&!rl&&r
07358284
$op->action // ""
l!l
76420
MC/DC0%0/2
$op->can("action") and ($op->action // "") eq "drop"
$op->can("action") $op->action
Truth table33%1/3
A and B
A= $op->can("action")B= $op->action
ABresult
0X0
100
111
138 284 ▶ return 0 if $op->can('replaced') && $op->replaced;
Branch: 1 branch
BranchTrueFalseTotal
if $op->can("replaced") and $op->replaced 46 238 284
Condition100%3/3
$op->can("replaced") and $op->replaced
!ll&&!rl&&r
1647446
MC/DC100%2/2
$op->can("replaced") and $op->replaced
$op->can("replaced") $op->replaced
Truth table100%3/3
A and B
A= $op->can("replaced")B= $op->replaced
ABresult
0X0
100
111
139 238 return 1;
140 }
141  
142 3670 ▶sub _build_operations {
Subroutine _build_operations: called CC=25 SCAR=32.3
143 3670 my ($self) = @_;
144 3670 my (@build, @remove, @rebuild_drops);
145 3670 my $tables_at;
146  
147 3670 for my $key (@_ORDER) {
148 # Where the changes to tables and columns begin: the views that are
149 # dropped and created again have to be gone by then.
150 47305 ▶ $tables_at = @build if $key eq 'tables';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "tables" 3661 43644 47305
151  
152 47305 ▶ my $diff_cls = $_REGISTRY{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $diff_cls = $_REGISTRY{$key} or next 47305 0 47305
153 47305 ▶ my $section = $_MODEL_SECTION{$key} // $key;
Condition66%2/3
$_MODEL_SECTION{$key} // $key
l!l&&r!l&&!r
7232400730
MC/DC0%0/2
$_MODEL_SECTION{$key} // $key
$_MODEL_SECTION{$key} $key
Truth table66%2/3
A // B
A= $_MODEL_SECTION{$key}B= $key
ABresult
1X1
011
000
154 47305 ▶ next unless exists $self->source->{$section} || exists $self->target->{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->source->{$section} or exists $self->target->{$section} 36242 11063 47305
Condition100%3/3
exists $self->source->{$section} or exists $self->target->{$section}
l!l&&r!l&&!r
9731009036242
MC/DC100%2/2
exists $self->source->{$section} or exists $self->target->{$section}
exists $self->source->{$section} exists $self->target->{$section}
Truth table100%3/3
A or B
A= exists $self->source->{$section}B= exists $self->target->{$section}
ABresult
1X1
011
000
155  
156 # A target without the section says nothing of foreign keys; it does not
157 # declare there are none (a model built by hand, target_from_compiled).
158 11063 ▶ next if $key eq 'foreign_keys' && !exists $self->target->{foreign_keys};
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "foreign_keys" and not exists $self->target->{"foreign_keys"} 2 11061 11063
Condition100%3/3
$key eq "foreign_keys" and not exists $self->target->{"foreign_keys"}
!ll&&!rl&&r
10992692
MC/DC100%2/2
$key eq "foreign_keys" and not exists $self->target->{"foreign_keys"}
$key eq "foreign_keys" not exists $self->target->{"foreign_keys"}
Truth table100%3/3
A and not(B)
A= $key eq "foreign_keys"B= not exists $self->target->{"foreign_keys"}
ABresult
0X0
100
111
159  
160 11061 my @extra;
161 11061 ▶ if (my $aux = $_AUX_SECTION{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $aux = $_AUX_SECTION{$key}) 10975 86 11061
162 10975 ▶ my @aux = ref $aux ? @$aux : ($aux);
Branch: 1 branch
BranchTrueFalseTotal
ref $aux ? : 7243 3732 10975
163 10975 ▶ @extra = map { ($self->source->{$_} // {}, $self->target->{$_} // {}) } @aux;
Condition100%4/4
$self->source->{$_} // {}
l!l
77731650
$self->target->{$_} // {}
l!l
1084721580
MC/DC100%1/1
$self->source->{$_} // {}
$self->source->{$_}
MC/DC100%1/1
$self->target->{$_} // {}
$self->target->{$_}
Truth table100%2/2
A
A= $self->source->{$_}
Aresult
11
00
Truth table100%2/2
A
A= $self->target->{$_}
Aresult
11
00
164 }
165  
166 # The extension protocol (ADR 0066) is asked about the change of a
167 # column's type: the columns are the one class that takes it.
168 11061 ▶ push @extra, $self->{extensions} if $key eq 'columns';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "columns" 3525 7536 11061
169  
170 # The indexes are asked whether a foreign key that points at a constraint
171 # to drop goes first. A target without the section declares no keys, which
172 # is not the same as none left, so the sections go over as they are.
173 11061 ▶ push @extra, $self->source->{foreign_keys}, $self->target->{foreign_keys} if $key eq 'indexes';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "indexes" 20 11041 11061
174  
175 11061 ▶ my ($from, $to) = ($self->source->{$section} // {}, $self->target->{$section} // {});
Condition100%4/4
$self->source->{$section} // {}
l!l
97110090
$self->target->{$section} // {}
l!l
110601
MC/DC100%1/1
$self->source->{$section} // {}
$self->source->{$section}
MC/DC100%1/1
$self->target->{$section} // {}
$self->target->{$section}
Truth table100%2/2
A
A= $self->source->{$section}
Aresult
11
00
Truth table100%2/2
A
A= $self->target->{$section}
Aresult
11
00
176  
177 # A key into a table in neither model is not the deploy's: not compared,
178 # not dropped, named in the summary.
179 11061 ▶ if ($key eq 'foreign_keys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "foreign_keys") 69 10992 11061
180 69 my @kept;
181 69 ($from, $to, @kept) = $diff_cls->split_outside($from, $to, @extra);
182 69 $self->{kept_foreign_keys} = \@kept;
183 }
184  
185 11061 my @ops = $diff_cls->diff($from, $to, @extra);
186  
187 @ops = $diff_cls->with_validation(@ops)
188 11001 ▶ if $key eq 'foreign_keys' && $self->{foreign_keys_not_valid};
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "foreign_keys" and $self->{"foreign_keys_not_valid"} 7 10994 11001
Condition100%3/3
$key eq "foreign_keys" and $self->{"foreign_keys_not_valid"}
!ll&&!rl&&r
10932627
MC/DC100%2/2
$key eq "foreign_keys" and $self->{"foreign_keys_not_valid"}
$key eq "foreign_keys" $self->{"foreign_keys_not_valid"}
Truth table100%3/3
A and B
A= $key eq "foreign_keys"B= $self->{"foreign_keys_not_valid"}
ABresult
0X0
100
111
189  
190 # Creates and changes in the order of dependency; removals in the
191 # reverse -- each section's ahead of those of the sections before it --
192 # and after everything that is created or changed.
193 11001 ▶ if ($key eq 'views' && defined $tables_at) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "views" and defined $tables_at) 3586 7415 11001
Condition66%2/3
$key eq "views" and defined $tables_at
!ll&&!rl&&r
741503586
MC/DC50%1/2
$key eq "views" and defined $tables_at
$key eq "views" defined $tables_at
Truth table66%2/3
A and B
A= $key eq "views"B= defined $tables_at
ABresult
0X0
100
111
194 # The drops of the views that are created again stand ahead of the
195 # tables and columns, not behind them: PostgreSQL does not alter the
196 # type of a column while a view reads it (karr k120).
197 3586 ▶ push @rebuild_drops, grep { $_->can('replaced') && $_->replaced } @ops;
Condition66%2/3
$_->can('replaced') && $_->replaced
!ll&&!rl&&r
0340931
MC/DC50%1/2
$_->can('replaced') && $_->replaced
$_->can('replaced') $_->replaced
Truth table66%2/3
A && B
A= $_->can('replaced')B= $_->replaced
ABresult
0X0
100
111
198 3586 ▶ @ops = grep { !($_->can('replaced') && $_->replaced) } @ops;
Condition66%2/3
$_->can('replaced') && $_->replaced
!ll&&!rl&&r
0340931
MC/DC50%1/2
$_->can('replaced') && $_->replaced
$_->can('replaced') $_->replaced
Truth table66%2/3
A && B
A= $_->can('replaced')B= $_->replaced
ABresult
0X0
100
111
199 }
200 11001 push @build, grep { !_is_removal($_) } @ops;
201 11001 unshift @remove, grep { _is_removal($_) } @ops;
202 }
203  
204 3610 ▶ splice @build, $tables_at // 0, 0, @rebuild_drops;
Condition50%1/2
$tables_at // 0
l!l
36100
MC/DC0%0/1
$tables_at // 0
$tables_at
Truth table50%1/2
A
A= $tables_at
Aresult
11
00
205  
206 3610 return [ @build, @remove ];
207 }
208  
209  
210 25 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
211 25 my ($self) = @_;
212 25 my $text = $self->SUPER::summary;
213 25 $self->operations;
214 25 ▶ my @kept = map { $_->summary } @{ $self->{kept_foreign_keys} // [] };
Condition100%2/2
$self->{"kept_foreign_keys"} // []
l!l
1114
MC/DC100%1/1
$self->{"kept_foreign_keys"} // []
$self->{"kept_foreign_keys"}
Truth table100%2/2
A
A= $self->{"kept_foreign_keys"}
Aresult
11
00
215 25 return join "\n", grep { length } $text, @kept;
216 }
217  
218  
219 # The adapter's native_type the way pg_catalog.format_type reports it back,
220 # where the two differ: timestamptz is "timestamp with time zone" there, with
221 # its precision behind the first word, and a numeric with a precision alone
222 # has the scale 0.
223 12 ▶sub _format_type_of_native {
Subroutine _format_type_of_native: called CC=3 SCAR=11.0
224 12 my ($native) = @_;
225 12 ▶ return DBIO::PostgreSQL::Util::type_with_modifier('timestamp with time zone', $1)
Branch: 1 branch
BranchTrueFalseTotal
if $native =~ /\Atimestamptz(?:\((\d+)\))?\z/ 3 9 12
226 if $native =~ /\Atimestamptz(?:\((\d+)\))?\z/;
227 9 ▶ return "numeric($1,0)" if $native =~ /\Anumeric\((\d+)\)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $native =~ /\Anumeric\((\d+)\)\z/ 1 8 9
228 8 return $native;
229 }
230  
231 # Render a default value in the same string form that pg_get_expr reports
232 # back from pg_attrdef, so the synthesized target model round-trips against
233 # the live (introspected) source. Returns undef when there is no default.
234 #
235 # $c->{default} shape (from DBIO::Schema::Type::canonical_column):
236 # undef -- no default
237 # \"now()" -- SCALAR ref holding a raw PG expression
238 # \"5" -- SCALAR ref holding a numeric literal
239 # "draft" -- plain string (a text/varchar literal in single quotes)
240 #
241 # pg_get_expr output for common cases:
242 # text 'draft' -> "'draft'::text" (string + ::textype)
243 # numeric 42 -> "42" (number, no cast)
244 # now() -> "now()" (expression, unchanged)
245 # nextval seq -> "nextval('schema.seq'::regclass)"
246 21 ▶sub _format_pg_default {
Subroutine _format_pg_default: called CC=21 SCAR=42.3
247 21 my ($default, $data_type) = @_;
248 21 ▶ return undef unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 11 10 21
249  
250 # Only a scalar ref is an expression, written as given. A plain string is a
251 # value: whatever it holds, it is written as a literal of the column's type.
252 10 my $is_expr = ref $default eq 'SCALAR';
253 10 ▶ my $expr = $is_expr ? $$default : $default;
Branch: 1 branch
BranchTrueFalseTotal
$is_expr ? : 2 8 10
254 10 ▶ return undef unless defined $expr && length $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr and length $expr 0 10 10
Condition33%1/3
defined $expr and length $expr
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $expr and length $expr
defined $expr length $expr
Truth table33%1/3
A and B
A= defined $exprB= length $expr
ABresult
0X0
100
111
255 10 $expr =~ s/^\s+|\s+$//g;
256  
257 10 ▶ if (!$is_expr) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_expr) 8 2 10
258 # a boolean column's true/false is what pg_get_expr prints, without a cast
259 8 ▶ return lc $expr
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /^(?:true|false)\z/i and ($data_type // "") =~ /^bool/i 1 7 8
Condition60%3/5
$expr =~ /^(?:true|false)\z/i and ($data_type // "") =~ /^bool/i
!ll&&!rl&&r
701
$data_type // ""
l!l
10
MC/DC50%1/2
$expr =~ /^(?:true|false)\z/i and ($data_type // "") =~ /^bool/i
$expr =~ /^(?:true|false)\z/i $data_type
Truth table66%2/3
A and B
A= $expr =~ /^(?:true|false)\z/iB= $data_type
ABresult
0X0
100
111
260 if $expr =~ /^(?:true|false)\z/i && ($data_type // '') =~ /^bool/i;
261 7 ▶ my $cast = $data_type // 'text';
Condition50%1/2
$data_type // "text"
l!l
70
MC/DC0%0/1
$data_type // "text"
$data_type
Truth table50%1/2
A
A= $data_type
Aresult
11
00
262 7 ▶ $cast = "timestamp with time zone" if $cast eq 'timestamptz';
Branch: 1 branch
BranchTrueFalseTotal
if $cast eq "timestamptz" 0 7 7
263 7 return sprintf('%s::%s', DBIO::PostgreSQL::Util::quote_literal($expr), $cast);
264 }
265  
266 # nextval() -- left untouched (matches pg_get_expr for IDENTITY-adjacent cols,
267 # though those are handled via $identity above and not via default).
268 2 ▶ return $expr if $expr =~ /\bnextval\s*\(/i;
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /\bnextval\s*\(/i 0 2 2
269  
270 # Already a quoted string (e.g. DDL-emitted literal). Pass through as-is so
271 # any embedded ::cast is preserved.
272 2 ▶ return $expr if $expr =~ /^['"]/;
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /^['"]/ 1 1 2
273  
274 # Already a quoted expression like 'now()' as a SCALAR ref string content
275 # of a non-string column -- common case: default_value => \'now()'.
276 # If it has parens or ::cast, leave alone.
277 1 ▶ return $expr if $expr =~ /[()]/ || $expr =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /[()]/ or $expr =~ /::/ 1 0 1
Condition33%1/3
$expr =~ /[()]/ or $expr =~ /::/
l!l&&r!l&&!r
100
MC/DC0%0/2
$expr =~ /[()]/ or $expr =~ /::/
$expr =~ /[()]/ $expr =~ /::/
Truth table33%1/3
A or B
A= $expr =~ /[()]/B= $expr =~ /::/
ABresult
1X1
011
000
278  
279 # Pure number or numeric expression -- PG renders numeric defaults
280 # as a quoted literal with an explicit cast (e.g. '0'::bigint), not as
281 # a bare number. The column type is *not* implied by the value.
282 0 ▶ if ($expr =~ /^-?\d+(?:\.\d+)?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^-?\d+(?:\.\d+)?\z/) 0 0 0
283 0 ▶ my $cast = $data_type // 'text';
Condition0%0/2
$data_type // "text"
l!l
00
MC/DC0%0/1
$data_type // "text"
$data_type
Truth table0%0/2
A
A= $data_type
Aresult
11
00
284 0 ▶ $cast = "timestamp with time zone" if $cast eq 'timestamptz';
Branch: 1 branch
BranchTrueFalseTotal
if $cast eq "timestamptz" 0 0 0
285 0 return sprintf('%s::%s', DBIO::PostgreSQL::Util::quote_literal($expr), $cast);
286 }
287  
288 # Boolean literals -- PG renders as 'true' / 'false' / 'null' (no cast).
289 0 ▶ return lc $expr if $expr =~ /^(?:true|false|null)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /^(?:true|false|null)\z/i 0 0 0
290  
291 # Fallback: treat as a string literal, quoting and casting to the column
292 # type. pg_get_expr appends '::textype' (the column's data_type string as
293 # format_type renders it). Use the already-normalized $data_type. The
294 # literal is the one install_ddl writes and pg_get_expr reports back: an
295 # embedded apostrophe doubled.
296 0 ▶ my $cast = $data_type // 'text';
Condition0%0/2
$data_type // "text"
l!l
00
MC/DC0%0/1
$data_type // "text"
$data_type
Truth table0%0/2
A
A= $data_type
Aresult
11
00
297 0 ▶ $cast = "timestamp with time zone" if $cast eq 'timestamptz';
Branch: 1 branch
BranchTrueFalseTotal
if $cast eq "timestamptz" 0 0 0
298 0 return sprintf('%s::%s', DBIO::PostgreSQL::Util::quote_literal($expr), $cast);
299 }
300  
301 2 ▶sub target_from_compiled {
Subroutine target_from_compiled: called CC=11 SCAR=25.0
Pod documented
302 2 my ($class, $compiled) = @_;
303 2 my (%tables, %columns, %indexes);
304  
305 # The public schema always exists in PostgreSQL; include it so the schema
306 # diff does not emit a spurious DROP SCHEMA public.
307 2 my %schemas = ( public => {} );
308  
309 2 ▶ for my $tname (keys %{ $compiled->{tables} // {} }) {
Condition50%1/2
$compiled->{"tables"} // {}
l!l
20
MC/DC0%0/1
$compiled->{"tables"} // {}
$compiled->{"tables"}
Truth table50%1/2
A
A= $compiled->{"tables"}
Aresult
11
00
310 4 my $t = $compiled->{tables}{$tname};
311 4 my $key = "public.$tname";
312  
313 4 $tables{$key} = {
314 schema_name => 'public',
315 table_name => $tname,
316 kind => 'r',
317 };
318  
319 4 my @cols;
320 my @pk_cols;
321 4 ▶ for my $c (@{ $t->{columns} // [] }) {
Condition50%1/2
$t->{"columns"} // []
l!l
40
MC/DC0%0/1
$t->{"columns"} // []
$t->{"columns"}
Truth table50%1/2
A
A= $t->{"columns"}
Aresult
11
00
322 12 my $native = $c->{native_type};
323 12 my $data_type = _format_type_of_native($native);
324  
325 12 my $identity = undef;
326 12 my $default_value = _format_pg_default($c->{default}, $data_type);
327  
328 12 ▶ if ($c->{auto_increment} && $c->{is_pk}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->{'auto_increment'} and $c->{'is_pk'}) { } 0 12 12
elsif ($c->{'auto_increment'}) { } 0 12 12
Condition33%1/3
$c->{"auto_increment"} and $c->{"is_pk"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$c->{"auto_increment"} and $c->{"is_pk"}
$c->{"auto_increment"} $c->{"is_pk"}
Truth table33%1/3
A and B
A= $c->{"auto_increment"}B= $c->{"is_pk"}
ABresult
0X0
100
111
329 # Deploy as GENERATED ALWAYS AS IDENTITY; PG reports identity='a' and no default.
330 0 $identity = 'a';
331 0 $default_value = undef;
332 }
333 elsif ($c->{auto_increment}) {
334 # Non-PK auto-increment: DDL.pm emits GENERATED BY DEFAULT AS IDENTITY,
335 # and PG reports attidentity='d' with no default expression.
336 0 $identity = 'd';
337 0 $default_value = undef;
338 }
339  
340 push @cols, {
341 column_name => $c->{column_name},
342 data_type => $data_type,
343 12 ▶ not_null => ($c->{not_null} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$c->{'not_null'} ? : 3 9 12
344 default_value => $default_value,
345 identity => $identity,
346 generated => undef,
347 };
348  
349 12 ▶ push @pk_cols, $c->{column_name} if $c->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"is_pk"} 3 9 12
350 }
351 4 $columns{$key} = \@cols;
352  
353 # PostgreSQL automatically creates a primary key index named <table>_pkey.
354 # Include it in the target so the index diff does not flag it as dropped.
355 # The definition string matches what pg_get_indexdef returns, enabling
356 # the definition-based comparison in Diff::Index. pg_get_indexdef quotes
357 # the index, the table and the columns by the rule of quote_ident(), so
358 # the same rule writes them here: a reserved or Mixed-Case name would
359 # otherwise differ from the server's text in nothing but its quotes, and
360 # the index would be dropped and recreated on every diff.
361 4 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 3 1 4
362 3 my $pkey_name = "${tname}_pkey";
363 my $definition = sprintf 'CREATE UNIQUE INDEX %s ON %s USING btree (%s)',
364 DBIO::PostgreSQL::Util::quote_ident($pkey_name),
365 DBIO::PostgreSQL::Util::quote_qualified('public', $tname),
366 3 join(', ', map { DBIO::PostgreSQL::Util::quote_ident($_) } @pk_cols);
367 3 $indexes{$key}{$pkey_name} = {
368 schema_name => 'public',
369 table_name => $tname,
370 index_name => $pkey_name,
371 access_method => 'btree',
372 is_unique => 1,
373 is_primary => 1,
374 is_valid => 1,
375 definition => $definition,
376 predicate => undef,
377 expressions => undef,
378 columns => \@pk_cols,
379 include_columns => [],
380 storage_params => {},
381 opclasses => {},
382 sort_options => {},
383 constraint_name => $pkey_name,
384 constraint_type => 'p',
385 referenced_by => [],
386 };
387 }
388 }
389  
390 return {
391 2 tables => \%tables,
392 columns => \%columns,
393 schemas => \%schemas,
394 extensions => {},
395 types => {},
396 functions => {},
397 indexes => \%indexes,
398 triggers => {},
399 policies => {},
400 sequences => {},
401 check_constraints => {},
402 };
403 }
404  
405  
406 103 1;
407  
408 __END__
« blib/lib/DBIO/PostgreSQL/Introspect.pm ↑ Summary blib/lib/DBIO/PostgreSQL/SQLMaker.pm »