blib/lib/DBIO/DuckDB/Diff.pm

filter: Statementstmt 58.0% 137 / 236 Branchbran 16.6% 6 / 36 Conditioncond 20.3% 27 / 133 MC/DCmcdc 0.0% 0 / 58 Subroutinesub 89.4% 17 / 19 Podpod 100% 2 / 2 totaltotal 39.0% 189 / 484 CC 97 SCAR 54.3
CC
97
Cov
42%
rebuild_reasons
628.1
_rebuild_ops
47.6
_unique_constraint_ops
31.1
CRAP 227.9
« blib/lib/DBIO/DuckDB/Diff/Column.pm ↑ Summary blib/lib/DBIO/DuckDB/Deploy.pm »
1 package DBIO::DuckDB::Diff;
2 # ABSTRACT: Compare two introspected DuckDB models
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.900007';
8  
9 8 ▶use base 'DBIO::Diff::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use DBIO::DuckDB::Diff::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use DBIO::DuckDB::Diff::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use DBIO::DuckDB::Diff::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use DBIO::DuckDB::Diff::View;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use DBIO::DuckDB::Diff::Rebuild;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 ▶sub _build_operations {
Subroutine _build_operations: called CC=6 SCAR=20.0
19 2 my ($self) = @_;
20 2 my @ops;
21  
22 # A view is no table: views are split off here, so no table, column or
23 # index op is ever written for one (DBIO::DuckDB::Diff::View).
24 4 ▶ my $split = sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
25 4 my ($tables) = @_;
26 4 my (%t, %v);
27 4 ▶ for my $name (keys %{ $tables || {} }) {
Condition50%1/2
$tables || {}
l!l
40
MC/DC0%0/1
$tables || {}
$tables
Truth table50%1/2
A
A= $tables
Aresult
11
00
28 3 ▶ my $into = ($tables->{$name}{kind} // '') eq 'view' ? \%v : \%t;
Branch: 1 branch
BranchTrueFalseTotal
($tables->{$name}{'kind'} // '') eq 'view' ? : 0 3 3
Condition50%1/2
$tables->{$name}{"kind"} // ""
l!l
30
MC/DC0%0/1
$tables->{$name}{"kind"} // ""
$tables->{$name}{"kind"}
Truth table50%1/2
A
A= $tables->{$name}{"kind"}
Aresult
11
00
29 3 $into->{$name} = $tables->{$name};
30 }
31 4 return (\%t, \%v);
32 2 };
33 2 my ($src_tables, $src_views) = $split->($self->source->{tables});
34 2 my ($tgt_tables, $tgt_views) = $split->($self->target->{tables});
35  
36 2 my @view_ops = DBIO::DuckDB::Diff::View->diff($src_views, $tgt_views);
37 2 my $rebuild = $self->rebuild_reasons;
38  
39 # views that go, and the old half of a replaced one, first: DuckDB refuses
40 # to drop what a view depends on
41 0 push @ops, grep { $_->action eq 'drop' } @view_ops;
42  
43 my @table_ops = DBIO::DuckDB::Diff::Table->diff(
44 $src_tables, $tgt_tables,
45 $self->target->{columns}, $self->target->{foreign_keys},
46 $self->source->{foreign_keys}, $self->target->{unique_constraints},
47 2 );
48 # the creates are written together with the rebuilds below, in the order of
49 # the foreign keys; the drops stay where they are
50 2 my @creates = grep { $_->action eq 'create' } @table_ops;
51 2 push @ops, grep { $_->action ne 'create' } @table_ops;
52 my @column_ops = DBIO::DuckDB::Diff::Column->diff(
53 $self->source->{columns}, $self->target->{columns},
54 2 $src_tables, $tgt_tables,
55 );
56 2 my %column_ops_of;
57 2 for my $op (@column_ops) {
58 0 ▶ push @{ $column_ops_of{ $op->table_name } }, $op if $rebuild->{ $op->table_name };
Branch: 1 branch
BranchTrueFalseTotal
if $$rebuild{$op->table_name} 0 0 0
59 }
60 0 push @ops, grep { !$rebuild->{ $_->table_name } } @column_ops;
61 2 push @ops, $self->_order_by_keys(
62 [ @creates, $self->_rebuild_ops($rebuild, \%column_ops_of) ],
63 );
64  
65 # A UNIQUE constraint of a table that exists is added as a unique index
66 # (DuckDB has no ALTER TABLE for it) and, once there, satisfies the
67 # constraint: the unique index is not an extra index of the source then.
68 2 my ($src_indexes, @unique_ops) = $self->_unique_constraint_ops($src_tables, $tgt_tables);
69 # a rebuilt table loses its indexes with the old table and gets the ones of
70 # the target again; its UNIQUE constraints are in its CREATE TABLE
71 1 push @ops, grep { !$rebuild->{ $_->table_name } } DBIO::DuckDB::Diff::Index->diff(
72 $src_indexes, $self->target->{indexes},
73 2 $src_tables, $tgt_tables,
74 );
75 0 push @ops, grep { !$rebuild->{ $_->table_name } } @unique_ops;
76 2 for my $table (sort keys %$rebuild) {
77 0 ▶ my $indexes = ($self->target->{indexes} || {})->{$table} || {};
Condition0%0/4
+($self->target->{'indexes'} || {})->{$table} || {}
l!l
00
$self->target->{'indexes'} || {}
l!l
00
MC/DC0%0/1
+($self->target->{'indexes'} || {})->{$table} || {}
$self->target->{'indexes'}
Truth table0%0/2
A
A= $self->target->{'indexes'}
Aresult
11
00
78 push @ops, map {
79 0 DBIO::DuckDB::Diff::Index->new(
80 action => 'create', table_name => $table,
81 0 index_name => $_, index_info => $indexes->{$_},
82 );
83 } sort keys %$indexes;
84 }
85  
86 # new views behind the tables and columns they select from
87 0 push @ops, grep { $_->action eq 'create' } @view_ops;
88  
89 2 return \@ops;
90 }
91  
92  
93 2 ▶sub rebuild_reasons {
Subroutine rebuild_reasons: called CC=39 SCAR=64.4
Pod documented
94 2 my ($self) = @_;
95 2 my ($src, $tgt) = ($self->source, $self->target);
96 2 my %reasons;
97  
98 2 ▶ my $plain = sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
99 2 my ($model, $table) = @_;
100 2 ▶ return exists $model->{tables}{$table} && ($model->{tables}{$table}{kind} // '') ne 'view';
Condition40%2/5
exists $model->{'tables'}{$table} && ($model->{'tables'}{$table}{'kind'} // '') ne 'view'
!ll&&!rl&&r
002
$model->{"tables"}{$table}{"kind"} // ""
l!l
20
MC/DC0%0/2
exists $model->{'tables'}{$table} && ($model->{'tables'}{$table}{'kind'} // '') ne 'view'
exists $model->{'tables'}{$table} $model->{"tables"}{$table}{"kind"}
Truth table33%1/3
A && B
A= exists $model->{'tables'}{$table}B= $model->{"tables"}{$table}{"kind"}
ABresult
0X0
100
111
101 2 };
102 2 ▶ my @both = grep { $plain->($src, $_) && $plain->($tgt, $_) } sort keys %{ $tgt->{tables} || {} };
Condition40%2/5
&$plain($src, $_) && &$plain($tgt, $_)
!ll&&!rl&&r
001
$tgt->{'tables'} || {}
l!l
20
MC/DC0%0/2
&$plain($src, $_) && &$plain($tgt, $_)
&$plain($src, $_) &$plain($tgt, $_)
MC/DC0%0/1
$tgt->{'tables'} || {}
$tgt->{'tables'}
Truth table33%1/3
A && B
A= &$plain($src, $_)B= &$plain($tgt, $_)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $tgt->{'tables'}
Aresult
11
00
103  
104 # a column that goes under an index or a UNIQUE constraint
105 2 for my $table (@both) {
106 1 ▶ my @order = map { $_->{column_name} } @{ $src->{columns}{$table} || [] };
Condition50%1/2
$src->{'columns'}{$table} || []
l!l
10
MC/DC0%0/1
$src->{'columns'}{$table} || []
$src->{'columns'}{$table}
Truth table50%1/2
A
A= $src->{'columns'}{$table}
Aresult
11
00
107 1 ▶ my %want = map { $_->{column_name} => 1 } @{ $tgt->{columns}{$table} || [] };
Condition50%1/2
$tgt->{'columns'}{$table} || []
l!l
10
MC/DC0%0/1
$tgt->{'columns'}{$table} || []
$tgt->{'columns'}{$table}
Truth table50%1/2
A
A= $tgt->{'columns'}{$table}
Aresult
11
00
108 1 for my $at (grep { !$want{ $order[$_] } } 0 .. $#order) {
109 0 my $column = $order[$at];
110 0 my %later = map { $_ => 1 } @order[ $at + 1 .. $#order ];
111 0 my @why;
112 0 ▶ for my $idx (values %{ ($src->{indexes} || {})->{$table} || {} }) {
Condition0%0/4
+($src->{'indexes'} || {})->{$table} || {}
l!l
00
$src->{'indexes'} || {}
l!l
00
MC/DC0%0/1
+($src->{'indexes'} || {})->{$table} || {}
$src->{'indexes'}
Truth table0%0/2
A
A= $src->{'indexes'}
Aresult
11
00
113 0 ▶ push @why, "index '$idx->{index_name}'" if grep { $later{$_} } @{ $idx->{columns} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$later{$_};} @{$idx->{"columns"} or [];} 0 0 0
Condition0%0/2
$idx->{'columns'} || []
l!l
00
MC/DC0%0/1
$idx->{'columns'} || []
$idx->{'columns'}
Truth table0%0/2
A
A= $idx->{'columns'}
Aresult
11
00
114 }
115 0 ▶ for my $uq (values %{ ($src->{unique_constraints} || {})->{$table} || {} }) {
Condition0%0/4
+($src->{'unique_constraints'} || {})->{$table} || {}
l!l
00
$src->{'unique_constraints'} || {}
l!l
00
MC/DC0%0/1
+($src->{'unique_constraints'} || {})->{$table} || {}
$src->{'unique_constraints'}
Truth table0%0/2
A
A= $src->{'unique_constraints'}
Aresult
11
00
116 0 push @why, sprintf "UNIQUE (%s)", join ', ', @{ $uq->{columns} }
117 0 ▶ if grep { $later{$_} || $_ eq $column } @{ $uq->{columns} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$later{$_} or $_ eq $column;} @{$$uq{"columns"};} 0 0 0
Condition0%0/3
$later{$_} || $_ eq $column
l!l&&r!l&&!r
000
MC/DC0%0/2
$later{$_} || $_ eq $column
$later{$_} $_ eq $column
Truth table0%0/3
A || B
A= $later{$_}B= $_ eq $column
ABresult
1X1
011
000
118 }
119 0 ▶ push @{ $reasons{$table} }, sprintf
Branch: 1 branch
BranchTrueFalseTotal
if @why 0 0 0
120 "column '%s' cannot be dropped, %s stands on it or on a later column",
121 $column, join(', ', sort @why) if @why;
122 }
123 }
124  
125 # a UNIQUE constraint that goes
126 2 my (undef, $dropped) = $self->unique_constraint_changes;
127 0 push @{ $reasons{ $_->[0] } }, sprintf 'UNIQUE (%s) is dropped', join ', ', @{ $_->[2] }
128 2 for @$dropped;
129  
130 # a foreign key that comes or goes
131 0 ▶ my $key = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
132 0 my ($fk) = @_;
133 return sprintf 'FOREIGN KEY (%s) REFERENCES %s(%s)%s%s',
134 0 join(', ', @{ $fk->{from_columns} }), $fk->{to_table},
135 0 ▶ join(', ', @{ $fk->{to_columns} }),
Branch: 2 branches
BranchTrueFalseTotal
defined $fk->{'on_delete'} && $fk->{'on_delete'} ne 'NO ACTION' ? : 0 0 0
defined $fk->{'on_update'} && $fk->{'on_update'} ne 'NO ACTION' ? : 0 0 0
Condition0%0/6
defined $fk->{"on_delete"} and $fk->{"on_delete"} ne "NO ACTION"
!ll&&!rl&&r
000
defined $fk->{"on_update"} and $fk->{"on_update"} ne "NO ACTION"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fk->{"on_delete"} and $fk->{"on_delete"} ne "NO ACTION"
defined $fk->{"on_delete"} $fk->{"on_delete"} ne "NO ACTION"
MC/DC0%0/2
defined $fk->{"on_update"} and $fk->{"on_update"} ne "NO ACTION"
defined $fk->{"on_update"} $fk->{"on_update"} ne "NO ACTION"
Truth table0%0/3
A and B
A= defined $fk->{"on_delete"}B= $fk->{"on_delete"} ne "NO ACTION"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $fk->{"on_update"}B= $fk->{"on_update"} ne "NO ACTION"
ABresult
0X0
100
111
136 (defined $fk->{on_delete} && $fk->{on_delete} ne 'NO ACTION' ? " ON DELETE $fk->{on_delete}" : ''),
137 0 (defined $fk->{on_update} && $fk->{on_update} ne 'NO ACTION' ? " ON UPDATE $fk->{on_update}" : '');
138 2 };
139 2 for my $table (@both) {
140 0 ▶ my %have = map { $key->($_) => 1 } @{ ($src->{foreign_keys} || {})->{$table} || [] };
Condition50%2/4
+($src->{'foreign_keys'} || {})->{$table} || []
l!l
01
$src->{'foreign_keys'} || {}
l!l
01
MC/DC0%0/1
+($src->{'foreign_keys'} || {})->{$table} || []
$src->{'foreign_keys'}
Truth table50%1/2
A
A= $src->{'foreign_keys'}
Aresult
11
00
141 0 ▶ my %want = map { $key->($_) => 1 } @{ ($tgt->{foreign_keys} || {})->{$table} || [] };
Condition50%2/4
+($tgt->{'foreign_keys'} || {})->{$table} || []
l!l
01
$tgt->{'foreign_keys'} || {}
l!l
01
MC/DC0%0/1
+($tgt->{'foreign_keys'} || {})->{$table} || []
$tgt->{'foreign_keys'}
Truth table50%1/2
A
A= $tgt->{'foreign_keys'}
Aresult
11
00
142 0 my @changed = ( (map { "$_ is added" } grep { !$have{$_} } sort keys %want),
143 0 (map { "$_ is dropped" } grep { !$want{$_} } sort keys %have) );
144 0 ▶ push @{ $reasons{$table} }, @changed if @changed;
Branch: 1 branch
BranchTrueFalseTotal
if @changed 0 1 1
145 }
146  
147 # a UNIQUE constraint a foreign key needs
148 0 ▶ my $set = sub { join "\0", sort @{ $_[0] } };
Subroutine __ANON__: not called CC=1 SCAR=2.6
149 2 ▶ for my $from (sort keys %{ $tgt->{foreign_keys} || {} }) {
Condition50%1/2
$tgt->{'foreign_keys'} || {}
l!l
02
MC/DC0%0/1
$tgt->{'foreign_keys'} || {}
$tgt->{'foreign_keys'}
Truth table50%1/2
A
A= $tgt->{'foreign_keys'}
Aresult
11
00
150 0 for my $fk (@{ $tgt->{foreign_keys}{$from} }) {
151 0 my $table = $fk->{to_table};
152 0 ▶ next unless grep { $_ eq $table } @both;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $table;} @both 0 0 0
153 0 my $cols = $set->($fk->{to_columns});
154 0 next unless grep { $set->($_->{columns}) eq $cols }
155 0 ▶ values %{ ($tgt->{unique_constraints} || {})->{$table} || {} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {&$set($_->{"columns"}) eq $cols;} values %{+($tgt->{"unique_constraints"} || {})->{$table} or {};} 0 0 0
Condition0%0/4
+($tgt->{'unique_constraints'} || {})->{$table} || {}
l!l
00
$tgt->{'unique_constraints'} || {}
l!l
00
MC/DC0%0/1
+($tgt->{'unique_constraints'} || {})->{$table} || {}
$tgt->{'unique_constraints'}
Truth table0%0/2
A
A= $tgt->{'unique_constraints'}
Aresult
11
00
156 0 ▶ my @pk = map { $_->{column_name} } grep { $_->{is_pk} } @{ $src->{columns}{$table} || [] };
Condition0%0/2
$src->{'columns'}{$table} || []
l!l
00
MC/DC0%0/1
$src->{'columns'}{$table} || []
$src->{'columns'}{$table}
Truth table0%0/2
A
A= $src->{'columns'}{$table}
Aresult
11
00
157 0 ▶ next if @pk && $set->(\@pk) eq $cols;
Branch: 1 branch
BranchTrueFalseTotal
if @pk and &$set(\@pk) eq $cols 0 0 0
Condition0%0/3
@pk and &$set(\@pk) eq $cols
!ll&&!rl&&r
000
MC/DC0%0/2
@pk and &$set(\@pk) eq $cols
@pk &$set(\@pk) eq $cols
Truth table0%0/3
A and B
A= @pkB= &$set(\@pk) eq $cols
ABresult
0X0
100
111
158 0 next if grep { $set->($_->{columns}) eq $cols }
159 0 ▶ values %{ ($src->{unique_constraints} || {})->{$table} || {} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {&$set($_->{"columns"}) eq $cols;} values %{+($src->{"unique_constraints"} || {})->{$table} or {};} 0 0 0
Condition0%0/4
+($src->{'unique_constraints'} || {})->{$table} || {}
l!l
00
$src->{'unique_constraints'} || {}
l!l
00
MC/DC0%0/1
+($src->{'unique_constraints'} || {})->{$table} || {}
$src->{'unique_constraints'}
Truth table0%0/2
A
A= $src->{'unique_constraints'}
Aresult
11
00
160 0 push @{ $reasons{$table} }, sprintf
161 "the foreign key (%s) of table '%s' needs a UNIQUE constraint on (%s)",
162 0 join(', ', @{ $fk->{from_columns} }), $from, join(', ', @{ $fk->{to_columns} });
163 }
164 }
165  
166 2 for my $table (keys %reasons) {
167 0 my %seen;
168 0 $reasons{$table} = [ grep { !$seen{$_}++ } @{ $reasons{$table} } ];
169 }
170 2 return \%reasons;
171 }
172  
173 2 ▶sub _rebuild_ops {
Subroutine _rebuild_ops: called CC=8 SCAR=38.6
174 2 my ($self, $reasons, $column_ops_of) = @_;
175 2 my ($src, $tgt) = ($self->source, $self->target);
176  
177 2 my @order = sort keys %$reasons; # _order_by_keys puts them in key order
178  
179 return map {
180 0 my $table = $_;
181 0 ▶ my %have = map { $_->{column_name} => 1 } @{ $src->{columns}{$table} || [] };
Condition0%0/2
$src->{'columns'}{$table} || []
l!l
00
MC/DC0%0/1
$src->{'columns'}{$table} || []
$src->{'columns'}{$table}
Truth table0%0/2
A
A= $src->{'columns'}{$table}
Aresult
11
00
182 0 ▶ my @cols = @{ $tgt->{columns}{$table} || [] };
Condition0%0/2
$tgt->{'columns'}{$table} || []
l!l
00
MC/DC0%0/1
$tgt->{'columns'}{$table} || []
$tgt->{'columns'}{$table}
Truth table0%0/2
A
A= $tgt->{'columns'}{$table}
Aresult
11
00
183 DBIO::DuckDB::Diff::Rebuild->new(
184 action => 'rebuild',
185 table_name => $table,
186 columns => \@cols,
187 foreign_keys => ($tgt->{foreign_keys} || {})->{$table} || [],
188 unique_constraints => ($tgt->{unique_constraints} || {})->{$table} || {},
189 0 ▶ copy_columns => [ grep { $have{$_} } map { $_->{column_name} } @cols ],
Condition0%0/2
$column_ops_of->{$table} || []
l!l
00
MC/DC0%0/1
$column_ops_of->{$table} || []
$column_ops_of->{$table}
Truth table0%0/2
A
A= $column_ops_of->{$table}
Aresult
11
00
190 reasons => $reasons->{$table},
191 0 ▶ column_ops => $column_ops_of->{$table} || [],
Condition0%0/8
+($tgt->{'foreign_keys'} || {})->{$table} || []
l!l
00
$tgt->{'foreign_keys'} || {}
l!l
00
+($tgt->{'unique_constraints'} || {})->{$table} || {}
l!l
00
$tgt->{'unique_constraints'} || {}
l!l
00
MC/DC0%0/1
+($tgt->{'foreign_keys'} || {})->{$table} || []
$tgt->{'foreign_keys'}
MC/DC0%0/1
+($tgt->{'unique_constraints'} || {})->{$table} || {}
$tgt->{'unique_constraints'}
Truth table0%0/2
A
A= $tgt->{'foreign_keys'}
Aresult
11
00
Truth table0%0/2
A
A= $tgt->{'unique_constraints'}
Aresult
11
00
192 );
193 } @order;
194 }
195  
196 # Creates and rebuilds in one order, the table a key points to first, whichever
197 # of the two it is: a new table with a key to the new UNIQUE of a rebuilt one
198 # follows the rebuild, a rebuilt table with a key to a new table follows its
199 # create.
200 2 ▶sub _order_by_keys {
Subroutine _order_by_keys: called CC=7 SCAR=32.3
201 2 my ($self, $nodes) = @_;
202 2 ▶ my $fks = $self->target->{foreign_keys} || {};
Condition50%1/2
$self->target->{'foreign_keys'} || {}
l!l
02
MC/DC0%0/1
$self->target->{'foreign_keys'} || {}
$self->target->{'foreign_keys'}
Truth table50%1/2
A
A= $self->target->{'foreign_keys'}
Aresult
11
00
203 2 my @rest = @$nodes;
204 2 my (@order, %done);
205 0 my %pending = map { $_->table_name => 1 } @rest;
206 2 ▶ while (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 0 2 2
207 my ($at) = grep {
208 0 my $t = $rest[$_]->table_name;
209 0 ▶ !grep { $_ ne $t && $pending{$_} && !$done{$_} }
Condition0%0/6
$_ ne $t && $pending{$_} && !$done{$_}
!ll&&!rl&&r
000
$_ ne $t and $pending{$_}
!ll&&!rl&&r
000
MC/DC0%0/3
$_ ne $t && $pending{$_} && !$done{$_}
$_ ne $t $pending{$_} !$done{$_}
Truth table0%0/4
A and B && not(C)
A= $_ ne $tB= $pending{$_}C= !$done{$_}
ABCresult
0XX0
10X0
1100
1111
210 0 ▶ map { $_->{to_table} } @{ $fks->{$t} || [] };
Condition0%0/2
$fks->{$t} || []
l!l
00
MC/DC0%0/1
$fks->{$t} || []
$fks->{$t}
Truth table0%0/2
A
A= $fks->{$t}
Aresult
11
00
211 } 0 .. $#rest;
212 0 ▶ $at //= 0; # a circle: as it comes, what cannot run is refused by the deploy
Condition0%0/2
$at //= 0
l!l
00
MC/DC0%0/1
$at //= 0
$at
Truth table0%0/2
A
A= $at
Aresult
11
00
213 0 my ($n) = splice @rest, $at, 1;
214 0 $done{ $n->table_name } = 1;
215 0 push @order, $n;
216 }
217 2 return @order;
218 }
219  
220  
221 4 ▶sub unique_constraint_changes {
Subroutine unique_constraint_changes: called CC=12 SCAR=34.3
Pod documented
222 4 my ($self) = @_;
223 4 my ($src, $tgt) = ($self->source, $self->target);
224 4 my (@added, @dropped);
225  
226 4 ▶ my $tuples = sub {
Subroutine __ANON__: called CC=8 SCAR=28.8
227 4 my ($model, $table) = @_;
228 0 my %c = map { join("\0", @{ $_->{columns} }) => $_ }
229 4 ▶ values %{ ($model->{unique_constraints} || {})->{$table} || {} };
Condition50%2/4
+($model->{'unique_constraints'} || {})->{$table} || {}
l!l
04
$model->{'unique_constraints'} || {}
l!l
04
MC/DC0%0/1
+($model->{'unique_constraints'} || {})->{$table} || {}
$model->{'unique_constraints'}
Truth table50%1/2
A
A= $model->{'unique_constraints'}
Aresult
11
00
230 0 my %i = map { join("\0", @{ $_->{columns} }) => $_ }
231 0 ▶ grep { $_->{is_unique} && !$_->{partial} && @{ $_->{columns} || [] } }
Condition25%2/8
$_->{'is_unique'} && !$_->{'partial'} && @{$_->{'columns'} or [];}
!ll&&!rl&&r
200
$_->{"is_unique"} and not $_->{"partial"}
!ll&&!rl&&r
200
$_->{'columns'} || []
l!l
00
MC/DC0%0/3
$_->{'is_unique'} && !$_->{'partial'} && @{$_->{'columns'} or [];}
$_->{"is_unique"} not $_->{"partial"} @{$_->{'columns'} or [];}
MC/DC0%0/1
$_->{'columns'} || []
$_->{'columns'}
Truth table25%1/4
A and not(B) && C
A= $_->{"is_unique"}B= not $_->{"partial"}C= @{$_->{'columns'} or [];}
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $_->{'columns'}
Aresult
11
00
232 4 ▶ values %{ ($model->{indexes} || {})->{$table} || {} };
Condition50%2/4
+($model->{'indexes'} || {})->{$table} || {}
l!l
40
$model->{'indexes'} || {}
l!l
40
MC/DC0%0/1
+($model->{'indexes'} || {})->{$table} || {}
$model->{'indexes'}
Truth table50%1/2
A
A= $model->{'indexes'}
Aresult
11
00
233 4 return (\%c, \%i);
234 4 };
235  
236 4 ▶ for my $table (sort keys %{ $tgt->{tables} || {} }) {
Condition50%1/2
$tgt->{'tables'} || {}
l!l
40
MC/DC0%0/1
$tgt->{'tables'} || {}
$tgt->{'tables'}
Truth table50%1/2
A
A= $tgt->{'tables'}
Aresult
11
00
237 2 ▶ next unless exists $src->{tables}{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $src->{"tables"}{$table} 0 2 2
238 2 ▶ next if ($tgt->{tables}{$table}{kind} // '') eq 'view';
Branch: 1 branch
BranchTrueFalseTotal
if ($tgt->{"tables"}{$table}{"kind"} // "") eq "view" 0 2 2
Condition50%1/2
$tgt->{"tables"}{$table}{"kind"} // ""
l!l
20
MC/DC0%0/1
$tgt->{"tables"}{$table}{"kind"} // ""
$tgt->{"tables"}{$table}{"kind"}
Truth table50%1/2
A
A= $tgt->{"tables"}{$table}{"kind"}
Aresult
11
00
239 2 my ($sc, $si) = $tuples->($src, $table);
240 2 my ($tc, $ti) = $tuples->($tgt, $table);
241 2 for my $key (sort keys %$tc) {
242 push @added, [ $table, $tc->{$key}{constraint_name}, $tc->{$key}{columns} ]
243 0 ▶ unless $sc->{$key} || $si->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $sc->{$key} or $si->{$key} 0 0 0
Condition0%0/3
$sc->{$key} or $si->{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$sc->{$key} or $si->{$key}
$sc->{$key} $si->{$key}
Truth table0%0/3
A or B
A= $sc->{$key}B= $si->{$key}
ABresult
1X1
011
000
244 }
245 2 for my $key (sort keys %$sc) {
246 push @dropped, [ $table, $sc->{$key}{constraint_name}, $sc->{$key}{columns} ]
247 0 ▶ unless $tc->{$key} || $ti->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $tc->{$key} or $ti->{$key} 0 0 0
Condition0%0/3
$tc->{$key} or $ti->{$key}
l!l&&r!l&&!r
000
MC/DC0%0/2
$tc->{$key} or $ti->{$key}
$tc->{$key} $ti->{$key}
Truth table0%0/3
A or B
A= $tc->{$key}B= $ti->{$key}
ABresult
1X1
011
000
248 }
249 }
250  
251 4 return (\@added, \@dropped);
252 }
253  
254 2 ▶sub _unique_constraint_ops {
Subroutine _unique_constraint_ops: called CC=13 SCAR=34.4
255 2 my ($self, $src_tables, $tgt_tables) = @_;
256 2 my ($added) = $self->unique_constraint_changes;
257  
258 # unique indexes of the source that stand for a UNIQUE constraint of the
259 # target are not touched by the index diff
260 2 my %keep;
261 2 ▶ for my $table (keys %{ $self->target->{unique_constraints} || {} }) {
Condition50%1/2
$self->target->{'unique_constraints'} || {}
l!l
02
MC/DC0%0/1
$self->target->{'unique_constraints'} || {}
$self->target->{'unique_constraints'}
Truth table50%1/2
A
A= $self->target->{'unique_constraints'}
Aresult
11
00
262 0 $keep{$table}{ join "\0", @{ $_->{columns} } } = 1
263 0 for values %{ $self->target->{unique_constraints}{$table} };
264 }
265 2 my %indexes;
266 2 ▶ my $source_indexes = $self->source->{indexes} || {};
Condition50%1/2
$self->source->{'indexes'} || {}
l!l
20
MC/DC0%0/1
$self->source->{'indexes'} || {}
$self->source->{'indexes'}
Truth table50%1/2
A
A= $self->source->{'indexes'}
Aresult
11
00
267 2 for my $table (keys %$source_indexes) {
268 2 for my $name (keys %{ $source_indexes->{$table} }) {
269 2 my $idx = $source_indexes->{$table}{$name};
270 next if $idx->{is_unique} && !$idx->{partial}
271 0 ▶ && $keep{$table}{ join "\0", @{ $idx->{columns} || [] } }
Condition0%0/4
$idx->{'columns'} || []
l!l
00
$self->target->{'indexes'} || {}
l!l
00
MC/DC0%0/1
$idx->{'columns'} || []
$idx->{'columns'}
MC/DC0%0/1
$self->target->{'indexes'} || {}
$self->target->{'indexes'}
Truth table0%0/2
A
A= $idx->{'columns'}
Aresult
11
00
Truth table0%0/2
A
A= $self->target->{'indexes'}
Aresult
11
00
272 2 ▶ && !exists(($self->target->{indexes} || {})->{$table}{$name});
Branch: 1 branch
BranchTrueFalseTotal
if $idx->{"is_unique"} and not $idx->{"partial"} and $keep{$table}{join "\0", @{$idx->{"columns"} or [];}} and not exists +($self->target->{"indexes"} || {})->{$table}{$name} 0 2 2
Condition33%3/9
$idx->{"is_unique"} and not $idx->{"partial"} and $keep{$table}{join "\0", @{$idx->{"columns"} or [];}} and not exists +($self->target->{"indexes"} || {})->{$table}{$name}
!ll&&!rl&&r
200
$idx->{"is_unique"} and not $idx->{"partial"} and $keep{$table}{join "\0", @{$idx->{"columns"} or [];}}
!ll&&!rl&&r
200
$idx->{"is_unique"} and not $idx->{"partial"}
!ll&&!rl&&r
200
MC/DC0%0/4
$idx->{"is_unique"} and not $idx->{"partial"} and $keep{$table}{join "\0", @{$idx->{"columns"} or [];}} and not exists +($self->target->{"indexes"} || {})->{$table}{$name}
$idx->{"is_unique"} not $idx->{"partial"} $keep{$table}{join "\0", @{$idx->{"columns"} or [];}} not exists +($self->target->{"indexes"} || {})->{$table}{$name}
Truth table20%1/5
A and not(B) and C and not(D)
A= $idx->{"is_unique"}B= not $idx->{"partial"}C= $keep{$table}{join "\0", @{$idx->{"columns"} or [];}}D= not exists +($self->target->{"indexes"} || {})->{$table}{$name}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
273 2 $indexes{$table}{$name} = $idx;
274 }
275 }
276  
277 my @ops = map {
278 0 my ($table, $name, $columns) = @$_;
279 0 DBIO::DuckDB::Diff::Index->new(
280 action => 'create',
281 table_name => $table,
282 index_name => $name,
283 index_info => { index_name => $name, columns => $columns, is_unique => 1,
284 from_unique_constraint => 1 },
285 );
286 } @$added;
287  
288 2 return (\%indexes, @ops);
289 }
290  
291 8 1;
292  
293 __END__
« blib/lib/DBIO/DuckDB/Diff/Column.pm ↑ Summary blib/lib/DBIO/DuckDB/Deploy.pm »