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

filter: Statementstmt 99.5% 203 / 204 Branchbran 96.1% 75 / 78 Conditioncond 85.2% 58 / 68 MC/DCmcdc 74.2% 26 / 35 Subroutinesub 96.8% 31 / 32 Podpod 100% 4 / 4 totaltotal 94.3% 397 / 421 CC 82 SCAR 25.1
CC
82
Cov
96%
diff
29.0
_persistence_order
16.0
as_sql
8.0
CRAP 12.3
« blib/lib/DBIO/PostgreSQL/Introspect/Triggers.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/Schemas.pm »
1 package DBIO::PostgreSQL::Diff::Table;
2 # ABSTRACT: Diff operations for PostgreSQL tables
3  
4 103 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 103 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 103 our $VERSION = '0.900007';
8  
9 103 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 103 ▶use DBIO::PostgreSQL::Diff::CaseOnly ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 103 ▶use DBIO::PostgreSQL::Diff::Dependents ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 103 ▶use DBIO::PostgreSQL::Diff::ForeignKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 103 ▶use DBIO::PostgreSQL::Diff::NoStatement ();
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::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 103 ▶use DBIO::PostgreSQL::Util qw(quote_ident quote_qualified);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 103 __PACKAGE__->mk_diff_accessors(qw(schema_name table_name table_info columns));
20  
21  
22  
23  
24  
25  
26  
27 103 my %KIND_LABEL = (
28 r => 'table',
29 v => 'view',
30 m => 'materialized view',
31 f => 'foreign table',
32 p => 'partitioned table',
33 );
34  
35 # An entry without a kind -- a model built by hand -- is a table.
36 6386 ▶sub _kind {
Subroutine _kind: called CC=2 SCAR=6.9
37 6386 my ($info) = @_;
38 6386 ▶ return $info->{kind} // 'r';
Condition100%2/2
$info->{"kind"} // "r"
l!l
635531
MC/DC100%1/1
$info->{"kind"} // "r"
$info->{"kind"}
Truth table100%2/2
A
A= $info->{"kind"}
Aresult
11
00
39 }
40  
41 55 ▶sub _kind_label {
Subroutine _kind_label: called CC=2 SCAR=7.2
42 55 my ($info) = @_;
43 55 ▶ return $KIND_LABEL{ _kind($info) } // "relation of kind '" . _kind($info) . "'";
Condition50%1/2
$KIND_LABEL{_kind($info)} // q[relation of kind '] . _kind($info) . q[']
l!l
550
MC/DC0%0/1
$KIND_LABEL{_kind($info)} // q[relation of kind '] . _kind($info) . q[']
$KIND_LABEL{_kind($info)}
Truth table50%1/2
A
A= $KIND_LABEL{_kind($info)}
Aresult
11
00
44 }
45  
46 4 ▶sub _refuse_kind_change {
Subroutine _refuse_kind_change: called CC=1 SCAR=0.0
47 4 my ($key, $old, $new) = @_;
48 4 die join "\n",
49 sprintf(
50 'refusing to diff: %s is a %s in the database and a %s in the target',
51 $key, _kind_label($old), _kind_label($new),
52 ),
53 '',
54 'To make the one into the other it has to be dropped and created anew, and what',
55 'it holds -- the rows of a table, the data of a materialized view -- is dropped',
56 'with it. No statement was generated.',
57 '',
58 'Drop or rename it yourself and diff again: the diff then creates what the',
59 'target has under that name.',
60 '';
61 }
62  
63 # What the model does not hold of a relation it cannot create, by kind.
64 103 my %NOT_IN_MODEL = (
65 f => 'its foreign server and its options',
66 p => 'how it is partitioned',
67 );
68  
69 3 ▶sub _refuse_create {
Subroutine _refuse_create: called CC=2 SCAR=6.9
70 3 my ($key, $info, $missing, $columns) = @_;
71 3 my $would_be = _create_table_sql($info, $columns);
72 my $what = defined $info->{partition_of}
73 3 ▶ ? "table $key is a partition of $info->{partition_of} in the target"
Branch: 1 branch
BranchTrueFalseTotal
defined $info->{'partition_of'} ? : 1 2 3
74 : _kind_label($info) . " $key is in the target";
75 3 die join "\n",
76 "refusing to diff: $what and not in the database, and the diff cannot create it",
77 '',
78 "The model holds its columns but not $missing, so all the diff",
79 'could write is',
80 " $would_be",
81 'which makes an ordinary table of that name. No statement was generated.',
82 '',
83 'Create it yourself, with the statement that declares it, and diff again.',
84 '';
85 }
86  
87 # How a relation is given another name, for the kinds whose drop takes what
88 # they hold. A view holds nothing: it is dropped and created (Diff::View).
89 103 my %RENAME = (
90 r => 'ALTER TABLE',
91 p => 'ALTER TABLE',
92 f => 'ALTER FOREIGN TABLE',
93 m => 'ALTER MATERIALIZED VIEW',
94 );
95  
96 # The relations of those kinds, as DBIO::PostgreSQL::Diff::CaseOnly compares
97 # them: a name within its schema, among the relations of its kind. A table
98 # and a materialized view are no pair -- no rename makes the one the other.
99 7286 ▶sub _renamable {
Subroutine _renamable: called CC=4 SCAR=13.9
100 7286 my ($tables) = @_;
101 7286 my @entries;
102 7286 for my $key (keys %$tables) {
103 4401 my $info = $tables->{$key};
104 4401 ▶ next unless $RENAME{ _kind($info) };
Branch: 1 branch
BranchTrueFalseTotal
unless $RENAME{_kind($info)} 3496 905 4401
105 905 my ($schema, $name) = DBIO::PostgreSQL::Introspect->key_names($key, table_name => $info);
106 905 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 905 905
107 905 push @entries, {
108 in => [ $schema, _kind($info) ], name => $name, schema => $schema, info => $info,
109 };
110 }
111 7286 return \@entries;
112 }
113  
114 3643 ▶sub _refuse_case_only {
Subroutine _refuse_case_only: called CC=1 SCAR=0.0
115 3643 my ($source, $target) = @_;
116 DBIO::PostgreSQL::Diff::CaseOnly->refuse(
117 "The drop takes what it holds: the rows of a table, the data of a\n"
118 . 'materialized view and the indexes on it.',
119 map {
120 3643 my ($old, $new) = @$_;
121 7 my $schema = $old->{schema};
122 {
123 kind => _kind_label($old->{info}),
124 database => "$schema.$old->{name}",
125 target => "$schema.$new->{name}",
126 rename => sprintf('%s %s RENAME TO %s;', $RENAME{ _kind($old->{info}) },
127 7 quote_qualified($schema, $old->{name}), quote_ident($new->{name})),
128 }
129 } DBIO::PostgreSQL::Diff::CaseOnly->pairs(_renamable($source), _renamable($target)),
130 );
131 }
132  
133 # What a table is among the others: a partition, a table that inherits, or
134 # neither. The parents are named in sorted order -- their order is that of
135 # the columns and nothing a statement changes.
136 358 ▶sub _belongs_to {
Subroutine _belongs_to: called CC=4 SCAR=13.9
137 358 my ($info) = @_;
138 358 ▶ return "a partition of $info->{partition_of}" if defined $info->{partition_of};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"partition_of"} 128 230 358
139 230 ▶ my @parents = sort @{ $info->{inherits} // [] };
Condition100%2/2
$info->{"inherits"} // []
l!l
76154
MC/DC100%1/1
$info->{"inherits"} // []
$info->{"inherits"}
Truth table100%2/2
A
A= $info->{"inherits"}
Aresult
11
00
140 230 ▶ return 'a table that inherits from ' . join(', ', @parents) if @parents;
Branch: 1 branch
BranchTrueFalseTotal
if @parents 30 200 230
141 200 return 'a table of its own';
142 }
143  
144 # A model read from the catalog says of every table what it belongs to. One
145 # built by hand that has neither key says nothing, and is not asked.
146 647 ▶sub _says_what_it_belongs_to {
Subroutine _says_what_it_belongs_to: called CC=2 SCAR=6.9
147 647 my ($info) = @_;
148 647 ▶ return exists $info->{partition_of} || exists $info->{inherits};
Condition100%3/3
exists $info->{'partition_of'} || exists $info->{'inherits'}
l!l&&r!l&&!r
32349275
MC/DC100%2/2
exists $info->{'partition_of'} || exists $info->{'inherits'}
exists $info->{'partition_of'} exists $info->{'inherits'}
Truth table100%3/3
A || B
A= exists $info->{'partition_of'}B= exists $info->{'inherits'}
ABresult
1X1
011
000
149 }
150  
151 3 ▶sub _refuse_parent_change {
Subroutine _refuse_parent_change: called CC=1 SCAR=0.0
152 3 my ($key, $old, $new) = @_;
153 3 die join "\n",
154 sprintf('refusing to diff: table %s is %s in the database and %s in the target',
155 $key, _belongs_to($old), _belongs_to($new)),
156 '',
157 'The diff has no statement that makes the one into the other: a partition is',
158 'attached with its bounds, which the model does not hold, and a table that is',
159 'made to inherit needs the columns of the other first.',
160 'No statement was generated.',
161 '',
162 'Do it yourself -- ALTER TABLE ... ATTACH PARTITION or DETACH PARTITION, ALTER',
163 'TABLE ... INHERIT or NO INHERIT -- and diff again.',
164 '';
165 }
166  
167 1 ▶sub _refuse_create_inheriting {
Subroutine _refuse_create_inheriting: called CC=1 SCAR=0.0
168 1 my ($key, $info, $columns) = @_;
169 1 my $parents = join ', ', @{ $info->{inherits} };
170 1 die join "\n",
171 "refusing to diff: table $key inherits from $parents in the target and is not in the database, and the diff cannot create it",
172 '',
173 'The diff has no statement for a table that inherits. All it could write is',
174 ' ' . _create_table_sql($info, $columns),
175 'which makes an ordinary table of that name, with the columns it has from',
176 "$parents as its own. No statement was generated.",
177 '',
178 'Create it yourself, with the statement that declares it -- CREATE TABLE ...',
179 'INHERITS (...) -- and diff again.',
180 '';
181 }
182  
183 # How a table is kept (pg_class.relpersistence), as a reader calls it.
184 103 my %PERSISTENCE = (
185 p => 'logged',
186 u => 'unlogged',
187 t => 'temporary',
188 );
189  
190 4 ▶sub _persistence_label {
Subroutine _persistence_label: called CC=2 SCAR=7.2
191 4 my ($persistence) = @_;
192 4 ▶ return $PERSISTENCE{$persistence} // "of the persistence '$persistence'";
Condition50%1/2
$PERSISTENCE{$persistence} // "of the persistence '${persistence}'"
l!l
40
MC/DC0%0/1
$PERSISTENCE{$persistence} // "of the persistence '${persistence}'"
$PERSISTENCE{$persistence}
Truth table50%1/2
A
A= $PERSISTENCE{$persistence}
Aresult
11
00
193 }
194  
195 # The persistences the diff writes, and the word ALTER TABLE has for each.
196 103 my %SET_PERSISTENCE = (
197 p => 'LOGGED',
198 u => 'UNLOGGED',
199 );
200  
201 1 ▶sub _refuse_persistence_create {
Subroutine _refuse_persistence_create: called CC=1 SCAR=0.0
202 1 my ($key, $info) = @_;
203 die join "\n",
204 sprintf('refusing to diff: %s %s is %s in the target',
205 1 _kind_label($info), $key, _persistence_label($info->{persistence})),
206 '',
207 'The diff creates logged and unlogged tables. A temporary table belongs to the',
208 'session that made it, and no model read from the catalog has one.',
209 'No statement was generated.',
210 '',
211 'Take it out of the target model and diff again.',
212 '';
213 }
214  
215 1 ▶sub _refuse_persistence_change {
Subroutine _refuse_persistence_change: called CC=1 SCAR=0.0
216 1 my ($key, $old, $new) = @_;
217 die join "\n",
218 sprintf('refusing to diff: %s %s is %s in the database and %s in the target',
219 1 _kind_label($new), $key, map { _persistence_label($_->{persistence}) } $old, $new),
220 '',
221 'PostgreSQL has ALTER TABLE ... SET LOGGED and SET UNLOGGED for an ordinary',
222 'table and for a partition, between those two, and for nothing else: a',
223 'partitioned table and a materialized view are not unlogged.',
224 'No statement was generated.',
225 '',
226 'Nothing the diff can write changes that. Where the server lets you, change',
227 'it yourself and diff again.',
228 '';
229 }
230  
231 # The tables a change of persistence is written for, in the order the server
232 # takes: going to UNLOGGED the table that holds a foreign key goes ahead of
233 # the one it references, going to LOGGED the referenced one goes ahead. Each
234 # step takes the first table by name that has nothing left ahead of it, so
235 # without a key the order is that of the names. A key between tables that go
236 # the other ways, or to a table that does not change, asks for nothing.
237 3625 ▶sub _persistence_order {
Subroutine _persistence_order: called CC=16 SCAR=27.7
238 3625 my ($source, $target, $foreign_keys) = @_;
239  
240 3625 my %to;
241 3625 for my $key (keys %$target) {
242 3846 ▶ next unless exists $source->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source->{$key} 3407 439 3846
243 439 my ($old, $new) = ($source->{$key}, $target->{$key});
244 next unless defined $old->{persistence} && defined $new->{persistence}
245 439 ▶ && $old->{persistence} ne $new->{persistence};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old->{"persistence"} and defined $new->{"persistence"} and $old->{"persistence"} ne $new->{"persistence"} 416 23 439
Condition100%6/6
defined $old->{"persistence"} and defined $new->{"persistence"} and $old->{"persistence"} ne $new->{"persistence"}
!ll&&!rl&&r
414223
defined $old->{"persistence"} and defined $new->{"persistence"}
!ll&&!rl&&r
413125
MC/DC100%3/3
defined $old->{"persistence"} and defined $new->{"persistence"} and $old->{"persistence"} ne $new->{"persistence"}
defined $old->{"persistence"} defined $new->{"persistence"} $old->{"persistence"} ne $new->{"persistence"}
Truth table100%4/4
A and B and C
A= defined $old->{"persistence"}B= defined $new->{"persistence"}C= $old->{"persistence"} ne $new->{"persistence"}
ABCresult
0XX0
10X0
1100
1111
246 23 $to{$key} = $new->{persistence};
247 }
248  
249 # %ahead: what has to be changed before the table, by key.
250 3625 my %ahead = map { $_ => {} } keys %to;
251 3625 for my $holder (keys %to) {
252 23 ▶ for my $fk (@{ $foreign_keys->{$holder} // [] }) {
Condition100%2/2
$foreign_keys->{$holder} // []
l!l
1310
MC/DC100%1/1
$foreign_keys->{$holder} // []
$foreign_keys->{$holder}
Truth table100%2/2
A
A= $foreign_keys->{$holder}
Aresult
11
00
253 my $referenced = DBIO::PostgreSQL::Introspect->qualified_key(
254 13 $fk->{remote_schema}, $fk->{remote_table});
255 13 ▶ next if $referenced eq $holder || !exists $to{$referenced};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced eq $holder or not exists $to{$referenced} 2 11 13
Condition100%3/3
$referenced eq $holder or not exists $to{$referenced}
l!l&&r!l&&!r
1111
MC/DC100%2/2
$referenced eq $holder or not exists $to{$referenced}
$referenced eq $holder not exists $to{$referenced}
Truth table100%3/3
A or not(B)
A= $referenced eq $holderB= not exists $to{$referenced}
ABresult
1X1
011
000
256 11 ▶ next if $to{$referenced} ne $to{$holder};
Branch: 1 branch
BranchTrueFalseTotal
if $to{$referenced} ne $to{$holder} 0 11 11
257 11 ▶ if ($to{$holder} eq 'u') { $ahead{$referenced}{$holder} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($to{$holder} eq 'u') { } 8 3 11
258 3 else { $ahead{$holder}{$referenced} = 1 }
259 }
260 }
261  
262 3625 my @order;
263 3625 my %left = %ahead;
264 3625 ▶ while (%left) {
Branch: 1 branch
BranchTrueFalseTotal
if (%left) 21 3623 3644
265 21 my ($next) = sort grep { !%{ $left{$_} } } keys %left;
266 21 ▶ unless (defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next) 2 19 21
267 2 my @cycle = sort keys %left;
268 die join "\n",
269 sprintf(
270 'refusing to diff: %s reference each other and are to be %s',
271 2 join(', ', @cycle), $SET_PERSISTENCE{ $to{ $cycle[0] } }),
272 '',
273 'PostgreSQL does not change a table to unlogged while it references a logged',
274 'one, nor to logged while it references an unlogged one, so with keys in',
275 'both directions no order of ALTER TABLE ... SET LOGGED / SET UNLOGGED is',
276 'taken. No statement was generated.',
277 '',
278 'Drop one of the foreign keys, diff again, and add it back afterwards.',
279 '';
280 }
281 19 push @order, $next;
282 19 delete $left{$next};
283 19 delete $_->{$next} for values %left;
284 }
285 3623 return @order;
286 }
287  
288 # The order tables are dropped in: one that inherits from another goes ahead of
289 # it, and so does one whose foreign key points at another, since the server
290 # does not drop a table while something stands on it. A key in a circle of
291 # dropped tables is dropped by the foreign key diff and sets no order.
292 # Otherwise by name.
293 3620 ▶sub _drop_order {
Subroutine _drop_order: called CC=8 SCAR=20.8
294 3620 my ($source, $keys_between, @keys) = @_;
295 3620 my %left = map { $_ => 1 } @keys;
296 3620 my %children;
297 3620 for my $key (@keys) {
298 33 ▶ $children{$_}{$key} = 1 for grep { $left{$_} } @{ $source->{$key}{inherits} // [] };
Condition100%2/2
$source->{$key}{"inherits"} // []
l!l
132
MC/DC100%1/1
$source->{$key}{"inherits"} // []
$source->{$key}{"inherits"}
Truth table100%2/2
A
A= $source->{$key}{"inherits"}
Aresult
11
00
299 }
300 3620 $children{ $_->{referenced} }{ $_->{holder} } = 1 for grep { !$_->{cyclic} } @$keys_between;
301 3620 my @order;
302 3620 ▶ while (%left) {
Branch: 1 branch
BranchTrueFalseTotal
if (%left) 33 3620 3653
303 33 ▶ my ($next) = sort grep { !grep { $left{$_} } keys %{ $children{$_} // {} } } keys %left;
Condition100%2/2
$children{$_} // {}
l!l
838
MC/DC100%1/1
$children{$_} // {}
$children{$_}
Truth table100%2/2
A
A= $children{$_}
Aresult
11
00
304 33 ▶ ($next) = sort keys %left unless defined $next;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 0 33 33
305 33 push @order, $next;
306 33 delete $left{$next};
307 }
308 3620 return @order;
309 }
310  
311 3643 ▶sub diff {
Subroutine diff: called CC=29 SCAR=33.7
Pod documented
312 3643 my ($class, $source, $target, $source_cols, $target_cols, $source_fks, $target_fks, $source_dependents) = @_;
313 3643 my @ops;
314  
315 3643 _refuse_case_only($source, $target);
316  
317 3637 for my $key (sort keys %$target) {
318 3867 ▶ next unless exists $source->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source->{$key} 3412 455 3867
319 455 my ($old, $new) = ($source->{$key}, $target->{$key});
320 455 ▶ _refuse_kind_change($key, $old, $new) if _kind($old) ne _kind($new);
Branch: 1 branch
BranchTrueFalseTotal
if _kind($old) ne _kind($new) 4 451 455
321 451 ▶ _refuse_parent_change($key, $old, $new)
Branch: 1 branch
BranchTrueFalseTotal
if _says_what_it_belongs_to($old) and _says_what_it_belongs_to($new) and _belongs_to($old) ne _belongs_to($new) 3 448 451
Condition100%6/6
_says_what_it_belongs_to($old) and _says_what_it_belongs_to($new) and _belongs_to($old) ne _belongs_to($new)
!ll&&!rl&&r
2751733
_says_what_it_belongs_to($old) and _says_what_it_belongs_to($new)
!ll&&!rl&&r
25520176
MC/DC100%3/3
_says_what_it_belongs_to($old) and _says_what_it_belongs_to($new) and _belongs_to($old) ne _belongs_to($new)
_says_what_it_belongs_to($old) _says_what_it_belongs_to($new) _belongs_to($old) ne _belongs_to($new)
Truth table100%4/4
A and B and C
A= _says_what_it_belongs_to($old)B= _says_what_it_belongs_to($new)C= _belongs_to($old) ne _belongs_to($new)
ABCresult
0XX0
10X0
1100
1111
322 if _says_what_it_belongs_to($old) && _says_what_it_belongs_to($new)
323 && _belongs_to($old) ne _belongs_to($new);
324 }
325  
326 3630 for my $key (sort keys %$target) {
327 3853 ▶ next if exists $source->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $source->{$key} 441 3412 3853
328 3412 my $t = $target->{$key};
329 3412 ▶ next if DBIO::PostgreSQL::Diff::View->is_view($t);
Branch: 1 branch
BranchTrueFalseTotal
if "DBIO::PostgreSQL::Diff::View"->is_view($t) 3375 37 3412
330  
331 my $missing = defined $t->{partition_of} ? 'the bounds of the partition'
332 37 ▶ : $NOT_IN_MODEL{ _kind($t) };
Branch: 1 branch
BranchTrueFalseTotal
defined $t->{'partition_of'} ? : 1 36 37
333 37 ▶ _refuse_create($key, $t, $missing, $target_cols->{$key}) if $missing;
Branch: 1 branch
BranchTrueFalseTotal
if $missing 3 34 37
334 34 ▶ _refuse_create_inheriting($key, $t, $target_cols->{$key}) if @{ $t->{inherits} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$t->{"inherits"} // [];} 1 33 34
Condition100%2/2
$t->{"inherits"} // []
l!l
133
MC/DC100%1/1
$t->{"inherits"} // []
$t->{"inherits"}
Truth table100%2/2
A
A= $t->{"inherits"}
Aresult
11
00
335 _refuse_persistence_create($key, $t)
336 33 ▶ unless $SET_PERSISTENCE{ $t->{persistence} // 'p' };
Branch: 1 branch
BranchTrueFalseTotal
unless $SET_PERSISTENCE{$t->{"persistence"} // "p"} 1 32 33
Condition100%2/2
$t->{"persistence"} // "p"
l!l
330
MC/DC100%1/1
$t->{"persistence"} // "p"
$t->{"persistence"}
Truth table100%2/2
A
A= $t->{"persistence"}
Aresult
11
00
337  
338 push @ops, $class->new(
339 action => 'create',
340 schema_name => $t->{schema_name},
341 table_name => $t->{table_name},
342 table_info => $t,
343 32 ▶ columns => $target_cols->{$key} // [],
Condition100%2/2
$target_cols->{$key} // []
l!l
2111
MC/DC100%1/1
$target_cols->{$key} // []
$target_cols->{$key}
Truth table100%2/2
A
A= $target_cols->{$key}
Aresult
11
00
344 );
345 }
346  
347 # How a table both have is kept. A target that does not say -- a model
348 # built by hand -- asks for nothing. The order is that of the foreign keys
349 # the database has (_persistence_order).
350 3625 ▶ for my $key (_persistence_order($source, $target, $source_fks // {})) {
Condition100%2/2
$source_fks // {}
l!l
36169
MC/DC100%1/1
$source_fks // {}
$source_fks
Truth table100%2/2
A
A= $source_fks
Aresult
11
00
351 19 my ($old, $new) = ($source->{$key}, $target->{$key});
352  
353 _refuse_persistence_change($key, $old, $new)
354 19 ▶ unless _kind($new) eq 'r' && $SET_PERSISTENCE{ $new->{persistence} };
Branch: 1 branch
BranchTrueFalseTotal
unless _kind($new) eq "r" and $SET_PERSISTENCE{$new->{"persistence"}} 1 18 19
Condition66%2/3
_kind($new) eq "r" and $SET_PERSISTENCE{$new->{"persistence"}}
!ll&&!rl&&r
1018
MC/DC50%1/2
_kind($new) eq "r" and $SET_PERSISTENCE{$new->{"persistence"}}
_kind($new) eq "r" $SET_PERSISTENCE{$new->{"persistence"}}
Truth table66%2/3
A and B
A= _kind($new) eq "r"B= $SET_PERSISTENCE{$new->{"persistence"}}
ABresult
0X0
100
111
355  
356 push @ops, $class->new(
357 action => 'set_persistence',
358 schema_name => $new->{schema_name},
359 table_name => $new->{table_name},
360 18 table_info => $new,
361 );
362 }
363  
364 3622 my @dropped;
365 3622 for my $key (sort keys %$source) {
366 485 ▶ next if exists $target->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$key} 434 51 485
367 51 my $t = $source->{$key};
368 51 ▶ next if DBIO::PostgreSQL::Diff::View->is_view($t);
Branch: 1 branch
BranchTrueFalseTotal
if "DBIO::PostgreSQL::Diff::View"->is_view($t) 13 38 51
369  
370 # A partition goes with the partitioned table it belongs to.
371 38 my $parent = $t->{partition_of};
372 38 ▶ next if defined $parent && exists $source->{$parent} && !exists $target->{$parent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $parent and exists $source->{$parent} and not exists $target->{$parent} 2 36 38
Condition83%5/6
defined $parent and exists $source->{$parent} and not exists $target->{$parent}
!ll&&!rl&&r
3512
defined $parent and exists $source->{$parent}
!ll&&!rl&&r
3503
MC/DC66%2/3
defined $parent and exists $source->{$parent} and not exists $target->{$parent}
defined $parent exists $source->{$parent} not exists $target->{$parent}
Truth table75%3/4
A and B and not(C)
A= defined $parentB= exists $source->{$parent}C= not exists $target->{$parent}
ABCresult
0XX0
10X0
1100
1111
373  
374 36 push @dropped, $key;
375 }
376  
377 # What stands on a table outside the model is named, not left to the server.
378 3622 DBIO::PostgreSQL::Diff::Dependents->refuse(map { {
379 object => _kind_label($source->{$_}) . " $_",
380 36 ▶ dependents => ($source_dependents // {})->{tables}{$_},
Condition100%2/2
$source_dependents // {}
l!l
324
MC/DC100%1/1
$source_dependents // {}
$source_dependents
Truth table100%2/2
A
A= $source_dependents
Aresult
11
00
381 } } @dropped);
382  
383 3620 my @between = DBIO::PostgreSQL::Diff::ForeignKey->dropped_together($source_fks, $source, $target);
384 3620 for my $key (_drop_order($source, \@between, @dropped)) {
385 33 my $t = $source->{$key};
386 push @ops, $class->new(
387 action => 'drop',
388 schema_name => $t->{schema_name},
389 table_name => $t->{table_name},
390 33 table_info => $t,
391 );
392 }
393  
394 3620 return @ops;
395 }
396  
397  
398 25 ▶sub qualified_name {
Subroutine qualified_name: called CC=1 SCAR=0.0
Pod documented
399 25 my ($self) = @_;
400 25 return $self->schema_name . '.' . $self->table_name;
401 }
402  
403  
404 0 ▶sub _identity_kind {
Subroutine _identity_kind: not called CC=1 SCAR=6.9
405 0 return DBIO::PostgreSQL::Introspect->identity_kind(@_);
406 }
407  
408 57 ▶sub _column_def {
Subroutine _column_def: called CC=7 SCAR=19.5
409 57 my ($col) = @_;
410 57 ▶ my $def = quote_ident($col->{column_name}) . ' ' . ($col->{data_type} // 'text');
Condition50%1/2
$col->{"data_type"} // "text"
l!l
570
MC/DC0%0/1
$col->{"data_type"} // "text"
$col->{"data_type"}
Truth table50%1/2
A
A= $col->{"data_type"}
Aresult
11
00
411  
412 # NOT NULL
413 57 ▶ $def .= ' NOT NULL' if $col->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"not_null"} 10 47 57
414  
415 # Generated columns: default_value is the expression they are computed
416 # from, not a default
417 57 ▶ if (my $generated = DBIO::PostgreSQL::Introspect->generated_kind($col->{generated})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $generated = "DBIO::PostgreSQL::Introspect"->generated_kind($col->{"generated"})) 8 49 57
418 8 return $def . sprintf ' GENERATED ALWAYS AS (%s) %s', $col->{default_value}, $generated;
419 }
420  
421 # Default value
422 49 ▶ if (defined $col->{default_value} && $col->{default_value} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"default_value"} and $col->{"default_value"} ne "") 5 44 49
Condition66%2/3
defined $col->{"default_value"} and $col->{"default_value"} ne ""
!ll&&!rl&&r
4405
MC/DC50%1/2
defined $col->{"default_value"} and $col->{"default_value"} ne ""
defined $col->{"default_value"} $col->{"default_value"} ne ""
Truth table66%2/3
A and B
A= defined $col->{"default_value"}B= $col->{"default_value"} ne ""
ABresult
0X0
100
111
423 5 $def .= sprintf ' DEFAULT %s', $col->{default_value};
424 }
425  
426 # Identity
427 49 my $kind = DBIO::PostgreSQL::Introspect->identity_kind($col->{identity});
428 49 ▶ $def .= " GENERATED $kind AS IDENTITY" if $kind;
Branch: 1 branch
BranchTrueFalseTotal
if $kind 3 46 49
429  
430 49 return $def;
431 }
432  
433 # CREATE TABLE for a table entry of the model and its columns.
434 50 ▶sub _create_table_sql {
Subroutine _create_table_sql: called CC=4 SCAR=13.9
435 50 my ($info, $columns) = @_;
436 return sprintf 'CREATE %sTABLE %s (%s);',
437 ( ($info->{persistence} // '') eq 'u' ? 'UNLOGGED ' : '' ),
438 50 quote_qualified(@{$info}{qw( schema_name table_name )}),
439 50 ▶ join ', ', map { _column_def($_) } @{ $columns // [] };
Branch: 1 branch
BranchTrueFalseTotal
($info->{'persistence'} // '') eq 'u' ? : 1 49 50
Condition100%4/4
$info->{"persistence"} // ""
l!l
248
$columns // []
l!l
491
MC/DC100%1/1
$info->{"persistence"} // ""
$info->{"persistence"}
MC/DC100%1/1
$columns // []
$columns
Truth table100%2/2
A
A= $info->{"persistence"}
Aresult
11
00
Truth table100%2/2
A
A= $columns
Aresult
11
00
440 }
441  
442 119 ▶sub as_sql {
Subroutine as_sql: called CC=8 SCAR=20.8
Pod documented
443 119 my ($self) = @_;
444 119 ▶ my $info = $self->table_info // {};
Condition50%1/2
$self->table_info // {}
l!l
1190
MC/DC0%0/1
$self->table_info // {}
$self->table_info
Truth table50%1/2
A
A= $self->table_info
Aresult
11
00
445 119 my $object = 'table ' . join('.', grep { defined } $self->schema_name, $self->table_name);
446  
447 119 ▶ if ($self->action eq 'create') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->action eq 'create') { } 46 73 119
elsif ($self->action eq 'set_persistence') { } 19 54 73
elsif ($self->action eq 'drop') { } 53 1 54
448 46 return _create_table_sql(
449 { %$info, schema_name => $self->schema_name, table_name => $self->table_name },
450 $self->columns,
451 );
452 }
453 elsif ($self->action eq 'set_persistence') {
454 19 ▶ my $to = $SET_PERSISTENCE{ $info->{persistence} // '' }
Branch: 1 branch
BranchTrueFalseTotal
my $to = $SET_PERSISTENCE{$info->{"persistence"} // ""} or "DBIO::PostgreSQL::Diff::NoStatement"->throw($self, $object, "its table_info does not say whether the table is to be logged ('p') or unlogged ('u')") 18 1 19
Condition50%1/2
$info->{"persistence"} // ""
l!l
190
MC/DC0%0/1
$info->{"persistence"} // ""
$info->{"persistence"}
Truth table50%1/2
A
A= $info->{"persistence"}
Aresult
11
00
455 or DBIO::PostgreSQL::Diff::NoStatement->throw(
456 $self, $object, q{its table_info does not say whether the table is to be logged ('p') or unlogged ('u')},
457 );
458 18 return sprintf 'ALTER TABLE %s SET %s;',
459 quote_qualified($self->schema_name, $self->table_name), $to;
460 }
461 elsif ($self->action eq 'drop') {
462 53 my $table = quote_qualified($self->schema_name, $self->table_name);
463 53 ▶ return sprintf 'DROP FOREIGN TABLE %s;', $table
Branch: 1 branch
BranchTrueFalseTotal
if _kind($info) eq "f" 1 52 53
464 if _kind($info) eq 'f';
465 52 return sprintf 'DROP TABLE %s;', $table;
466 }
467 1 DBIO::PostgreSQL::Diff::NoStatement->throw($self, $object);
468 }
469  
470  
471 20 ▶sub summary {
Subroutine summary: called CC=5 SCAR=16.4
Pod documented
472 20 my ($self) = @_;
473 return sprintf ' ~table: %s (%s)', $self->qualified_name,
474 20 ▶ _persistence_label(($self->table_info // {})->{persistence} // '')
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "set_persistence" 1 19 20
Condition50%2/4
+($self->table_info // {})->{"persistence"} // ""
l!l
10
$self->table_info // {}
l!l
10
MC/DC0%0/1
+($self->table_info // {})->{"persistence"} // ""
$self->table_info
Truth table50%1/2
A
A= $self->table_info
Aresult
11
00
475 if $self->action eq 'set_persistence';
476 19 ▶ return sprintf ' %stable: %s',
Branch: 1 branch
BranchTrueFalseTotal
$self->action eq 'create' ? : 5 14 19
477 ($self->action eq 'create' ? '+' : '-'), $self->qualified_name;
478 }
479  
480 103 1;
481  
482 __END__
« blib/lib/DBIO/PostgreSQL/Introspect/Triggers.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/Schemas.pm »