blib/lib/DBIO/DuckDB/Deploy.pm

filter: Statementstmt 21.3% 38 / 178 Branchbran 1.6% 1 / 60 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 50.0% 13 / 26 Podpod 100% 3 / 3 totaltotal 16.3% 55 / 337 CC 59 SCAR 53.1
CC
59
Cov
14%
_refuse_duplicates_for_unique
506.0
_refuse_rebuilds
240.0
_refuse_unfillable
156.0
CRAP 201.6
« blib/lib/DBIO/DuckDB/Diff.pm ↑ Summary blib/lib/DBIO/DuckDB/DDL.pm »
1 package DBIO::DuckDB::Deploy;
2 # ABSTRACT: Deploy and upgrade DuckDB schemas via test-deploy-and-compare
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.900007';
8  
9 7 ▶use base 'DBIO::Deploy::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use DBIO::DuckDB::DDL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use DBIO::DuckDB::Introspect;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use DBIO::DuckDB::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use DBIO::DuckDB::Extension;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use DBIO::DuckDB::DDL::Emit ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 0 ▶sub catalog { $_[0]->{catalog} }
Subroutine catalog: not called CC=1 SCAR=6.9
Pod documented
21  
22 0 ▶sub _ddl_class { 'DBIO::DuckDB::DDL' }
Subroutine _ddl_class: not called CC=1 SCAR=6.9
23 0 ▶sub _introspect_class { 'DBIO::DuckDB::Introspect' }
Subroutine _introspect_class: not called CC=1 SCAR=6.9
24 0 ▶sub _diff_class { 'DBIO::DuckDB::Diff' }
Subroutine _diff_class: not called CC=1 SCAR=6.9
25  
26  
27  
28  
29 0 ▶sub _check_before_apply {
Subroutine _check_before_apply: not called CC=1 SCAR=6.9
30 0 my ($self, $diff) = @_;
31 0 $self->_refuse_rebuilds($diff);
32 0 $self->_refuse_duplicates_for_unique($diff);
33 0 $self->_refuse_unfillable($diff);
34 0 return $self->SUPER::_check_before_apply($diff);
35 }
36  
37  
38 0 ▶sub _execute_diff {
Subroutine _execute_diff: not called CC=5 SCAR=34.0
39 0 my ($self, $diff) = @_;
40 0 my @ops = @{ $diff->operations };
41 0 ▶ my @late = grep { length } map { $_->can('late_sql') ? $_->late_sql : '' } @ops;
Branch: 1 branch
BranchTrueFalseTotal
$_->can('late_sql') ? : 0 0 0
42 0 ▶ return $self->SUPER::_execute_diff($diff) unless @late;
Branch: 1 branch
BranchTrueFalseTotal
unless @late 0 0 0
43  
44 0 ▶ $self->{_extension_protocol}->assert_no_refusals if $self->{_extension_protocol};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_extension_protocol"} 0 0 0
45 my $early = join "\n",
46 0 ▶ map { $_->can('early_sql') ? $_->early_sql : $_->as_sql } @ops;
Branch: 1 branch
BranchTrueFalseTotal
$_->can('early_sql') ? : 0 0 0
47 0 $self->_execute_ddl($self->_dbh, $early);
48 0 $self->_execute_ddl($self->_dbh, join "\n", @late);
49 0 return 1;
50 }
51  
52  
53 1 ▶sub diff {
Subroutine diff: called CC=1 SCAR=1.2
Pod documented
54 1 my $self = shift;
55 1 my $diff = $self->SUPER::diff(@_);
56 0 $self->_refuse_rebuilds($diff);
57 0 return $diff;
58 }
59  
60 # DuckDB drops no table another one has a foreign key to ("Could not drop the
61 # table because this table is main key table of the table ...", measured
62 # libduckdb 1.5.3, also inside a transaction), and a table that points to
63 # itself keeps the temporary name in its key after the rename (the insert then
64 # fails with "Table with name ..__dbio_rebuild does not exist"). A rebuild in
65 # either case is refused before the first statement, naming the tables
66 # (karr k50, docs/adr/0010).
67 0 ▶sub _refuse_rebuilds {
Subroutine _refuse_rebuilds: not called CC=15 SCAR=54.8
68 0 my ($self, $diff) = @_;
69 0 ▶ my $src = $diff->source || {};
Condition0%0/2
$diff->source || {}
l!l
00
MC/DC0%0/1
$diff->source || {}
$diff->source
Truth table0%0/2
A
A= $diff->source
Aresult
11
00
70 0 ▶ my $tgt = $diff->target || {};
Condition0%0/2
$diff->target || {}
l!l
00
MC/DC0%0/1
$diff->target || {}
$diff->target
Truth table0%0/2
A
A= $diff->target
Aresult
11
00
71 0 ▶ my %gone = map { $_ => 1 } grep { !exists $tgt->{tables}{$_} } keys %{ $src->{tables} || {} };
Condition0%0/2
$src->{'tables'} || {}
l!l
00
MC/DC0%0/1
$src->{'tables'} || {}
$src->{'tables'}
Truth table0%0/2
A
A= $src->{'tables'}
Aresult
11
00
72 0 my @refused;
73  
74 0 for my $op (@{ $diff->operations }) {
75 0 ▶ next unless $op->isa('DBIO::DuckDB::Diff::Rebuild');
Branch: 1 branch
BranchTrueFalseTotal
unless $op->isa("DBIO::DuckDB::Diff::Rebuild") 0 0 0
76 0 my $table = $op->table_name;
77 0 my $why = join '; ', @{ $op->reasons };
78  
79 0 for my $fk (@{ $op->foreign_keys }) {
80 push @refused, sprintf "table '%s' points to itself with (%s), which a rebuild would leave pointing to the temporary table (%s)",
81 0 $table, join(', ', @{ $fk->{from_columns} }), $why
82 0 ▶ if $fk->{to_table} eq $table;
Branch: 1 branch
BranchTrueFalseTotal
if $fk->{"to_table"} eq $table 0 0 0
83 }
84 0 ▶ for my $from (sort keys %{ $src->{foreign_keys} || {} }) {
Condition0%0/2
$src->{'foreign_keys'} || {}
l!l
00
MC/DC0%0/1
$src->{'foreign_keys'} || {}
$src->{'foreign_keys'}
Truth table0%0/2
A
A= $src->{'foreign_keys'}
Aresult
11
00
85 0 ▶ next if $from eq $table || $gone{$from};
Branch: 1 branch
BranchTrueFalseTotal
if $from eq $table or $gone{$from} 0 0 0
Condition0%0/3
$from eq $table or $gone{$from}
l!l&&r!l&&!r
000
MC/DC0%0/2
$from eq $table or $gone{$from}
$from eq $table $gone{$from}
Truth table0%0/3
A or B
A= $from eq $tableB= $gone{$from}
ABresult
1X1
011
000
86 0 for my $fk (@{ $src->{foreign_keys}{$from} }) {
87 push @refused, sprintf "table '%s' has to be rebuilt (%s) but table '%s' has a foreign key (%s) to it, and DuckDB does not drop a table another one points to",
88 0 $table, $why, $from, join(', ', @{ $fk->{from_columns} })
89 0 ▶ if $fk->{to_table} eq $table;
Branch: 1 branch
BranchTrueFalseTotal
if $fk->{"to_table"} eq $table 0 0 0
90 }
91 }
92 }
93 0 ▶ return unless @refused;
Branch: 1 branch
BranchTrueFalseTotal
unless @refused 0 0 0
94  
95 0 DBIO::Exception->throw(
96 "The diff is refused: " . join('; ', @refused) . ". Nothing was changed.");
97 }
98  
99 # A unique index over values that are not unique fails half way through the
100 # diff ("Data contains duplicates on indexed column(s)"); look first. NULLs are
101 # no duplicates.
102 0 ▶sub _refuse_duplicates_for_unique {
Subroutine _refuse_duplicates_for_unique: not called CC=22 SCAR=62.3
103 0 my ($self, $diff) = @_;
104 0 my $dbh = $self->_dbh;
105 0 ▶ my $src = $diff->source || {};
Condition0%0/2
$diff->source || {}
l!l
00
MC/DC0%0/1
$diff->source || {}
$diff->source
Truth table0%0/2
A
A= $diff->source
Aresult
11
00
106 0 my @refused;
107  
108 # the unique indexes the diff creates, and the UNIQUE constraints a rebuilt
109 # table gets that it has not had (its copy would fail on a duplicate)
110 my @checks;
111 0 for my $op (@{ $diff->operations }) {
112 0 ▶ if ($op->isa('DBIO::DuckDB::Diff::Index') && $op->action eq 'create') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->isa('DBIO::DuckDB::Diff::Index') and $op->action eq 'create') { } 0 0 0
elsif ($op->isa('DBIO::DuckDB::Diff::Rebuild')) { } 0 0 0
Condition0%0/3
$op->isa("DBIO::DuckDB::Diff::Index") and $op->action eq "create"
!ll&&!rl&&r
000
MC/DC0%0/2
$op->isa("DBIO::DuckDB::Diff::Index") and $op->action eq "create"
$op->isa("DBIO::DuckDB::Diff::Index") $op->action eq "create"
Truth table0%0/3
A and B
A= $op->isa("DBIO::DuckDB::Diff::Index")B= $op->action eq "create"
ABresult
0X0
100
111
113 0 ▶ my $info = $op->index_info || {};
Condition0%0/2
$op->index_info || {}
l!l
00
MC/DC0%0/1
$op->index_info || {}
$op->index_info
Truth table0%0/2
A
A= $op->index_info
Aresult
11
00
114 0 ▶ push @checks, [ $op->table_name, $info->{columns} ] if $info->{is_unique} && !$info->{partial};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_unique"} and not $info->{"partial"} 0 0 0
Condition0%0/3
$info->{"is_unique"} and not $info->{"partial"}
!ll&&!rl&&r
000
MC/DC0%0/2
$info->{"is_unique"} and not $info->{"partial"}
$info->{"is_unique"} not $info->{"partial"}
Truth table0%0/3
A and not(B)
A= $info->{"is_unique"}B= not $info->{"partial"}
ABresult
0X0
100
111
115 }
116 elsif ($op->isa('DBIO::DuckDB::Diff::Rebuild')) {
117 0 my $table = $op->table_name;
118 0 my %had = map { join("\0", @{ $_->{columns} }) => 1 }
119 0 ▶ 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
120 0 push @checks, map { [ $table, $_->{columns} ] }
121 0 grep { !$had{ join "\0", @{ $_->{columns} } } }
122 0 map { $op->unique_constraints->{$_} } sort keys %{ $op->unique_constraints };
123 }
124 }
125 0 my %seen_check;
126 0 for my $check (@checks) {
127 0 my ($table, $columns) = @$check;
128 0 ▶ next if $seen_check{ join "\0", $table, @{ $columns || [] } }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_check{join "\0", $table, @{$columns or [];}}++ 0 0 0
Condition0%0/2
$columns || []
l!l
00
MC/DC0%0/1
$columns || []
$columns
Truth table0%0/2
A
A= $columns
Aresult
11
00
129 0 ▶ next unless exists $src->{tables}{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $src->{"tables"}{$table} 0 0 0
130 0 ▶ my @cols = @{ $columns || [] };
Condition0%0/2
$columns || []
l!l
00
MC/DC0%0/1
$columns || []
$columns
Truth table0%0/2
A
A= $columns
Aresult
11
00
131 0 ▶ next unless @cols && !grep { /^\(/ } @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols and not grep({/^\(/;} @cols) 0 0 0
Condition0%0/3
@cols and not grep({/^\(/;} @cols)
!ll&&!rl&&r
000
MC/DC0%0/2
@cols and not grep({/^\(/;} @cols)
@cols not grep({/^\(/;} @cols)
Truth table0%0/3
A and not(B)
A= @colsB= not grep({/^\(/;} @cols)
ABresult
0X0
100
111
132 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
133 0 ▶ next if grep { !$have{$_} } @cols; # a column the diff still adds
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $have{$_};} @cols 0 0 0
134  
135 0 my $list = join ', ', map { DBIO::DuckDB::DDL::Emit::quote_ident($_) } @cols;
136 my $found = $dbh->selectrow_array(sprintf
137 'SELECT 1 FROM %s WHERE %s GROUP BY %s HAVING count(*) > 1 LIMIT 1',
138 DBIO::DuckDB::DDL::Emit::quote_table($table),
139 0 join(' AND ', map { DBIO::DuckDB::DDL::Emit::quote_ident($_) . ' IS NOT NULL' } @cols),
140 $list);
141 0 ▶ push @refused, sprintf "UNIQUE (%s) of table '%s'", join(', ', @cols), $table if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
142 }
143 0 ▶ return unless @refused;
Branch: 1 branch
BranchTrueFalseTotal
unless @refused 0 0 0
144  
145 0 DBIO::Exception->throw(
146 "The diff is refused: " . join('; ', @refused)
147 . " cannot be created, the table holds duplicate values in it. Nothing was changed.");
148 }
149  
150 0 ▶sub _refuse_unfillable {
Subroutine _refuse_unfillable: not called CC=12 SCAR=50.5
151 0 my ($self, $diff) = @_;
152 0 my $dbh = $self->_dbh;
153  
154 0 ▶ my $any = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
155 0 my ($table, $where) = @_;
156 0 ▶ return scalar $dbh->selectrow_array(sprintf 'SELECT 1 FROM %s%s LIMIT 1',
Condition0%0/2
$where // ""
l!l
00
MC/DC0%0/1
$where // ""
$where
Truth table0%0/2
A
A= $where
Aresult
11
00
157 DBIO::DuckDB::DDL::Emit::quote_table($table), $where // '');
158 0 };
159  
160 # a rebuilt table copies its rows into the target's definition: the column
161 # operations it replaces still say what the rows would lack
162 0 ▶ my @ops = map { $_->isa('DBIO::DuckDB::Diff::Rebuild') ? @{ $_->column_ops } : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('DBIO::DuckDB::Diff::Rebuild') ? : 0 0 0
163 0 @{ $diff->operations };
164 0 for my $op (@ops) {
165 0 ▶ next unless $op->isa('DBIO::DuckDB::Diff::Column');
Branch: 1 branch
BranchTrueFalseTotal
unless $op->isa("DBIO::DuckDB::Diff::Column") 0 0 0
166 0 my ($table, $column) = ($op->table_name, $op->column_name);
167  
168 0 ▶ if ($op->action eq 'add') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->action eq 'add') { } 0 0 0
elsif ($op->action eq 'alter') { } 0 0 0
169 0 my $info = $op->new_info;
170 0 ▶ next unless $info->{not_null} && !defined $info->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"not_null"} and not defined $info->{"default_value"} 0 0 0
Condition0%0/3
$info->{"not_null"} and not defined $info->{"default_value"}
!ll&&!rl&&r
000
MC/DC0%0/2
$info->{"not_null"} and not defined $info->{"default_value"}
$info->{"not_null"} not defined $info->{"default_value"}
Truth table0%0/3
A and not(B)
A= $info->{"not_null"}B= not defined $info->{"default_value"}
ABresult
0X0
100
111
171 0 ▶ next unless $any->($table);
Branch: 1 branch
BranchTrueFalseTotal
unless &$any($table) 0 0 0
172 0 DBIO::Exception->throw(sprintf
173 'DuckDB refuses to add the column %s.%s: it is NOT NULL without a '
174 . 'default, and table %s holds rows that would have no value for it',
175 $table, $column, $table);
176 }
177 elsif ($op->action eq 'alter') {
178 0 ▶ next unless $op->new_info->{not_null} && !$op->old_info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
unless $op->new_info->{"not_null"} and not $op->old_info->{"not_null"} 0 0 0
Condition0%0/3
$op->new_info->{"not_null"} and not $op->old_info->{"not_null"}
!ll&&!rl&&r
000
MC/DC0%0/2
$op->new_info->{"not_null"} and not $op->old_info->{"not_null"}
$op->new_info->{"not_null"} not $op->old_info->{"not_null"}
Truth table0%0/3
A and not(B)
A= $op->new_info->{"not_null"}B= not $op->old_info->{"not_null"}
ABresult
0X0
100
111
179 0 ▶ next unless $any->($table, ' WHERE '
Branch: 1 branch
BranchTrueFalseTotal
unless &$any($table, ' WHERE ' . DBIO::DuckDB::DDL::Emit::quote_ident($column) . ' IS NULL') 0 0 0
180 . DBIO::DuckDB::DDL::Emit::quote_ident($column) . ' IS NULL');
181 0 DBIO::Exception->throw(sprintf
182 'DuckDB refuses to make the column %s.%s NOT NULL: table %s holds '
183 . 'rows where it is NULL',
184 $table, $column, $table);
185 }
186 }
187  
188 0 return;
189 }
190  
191  
192 0 ▶sub _new_introspect {
Subroutine _new_introspect: not called CC=3 SCAR=24.8
193 0 my ($self, $dbh) = @_;
194 0 my %opts = (dbh => $dbh);
195 0 ▶ $opts{catalog} = $self->catalog if defined $self->catalog;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->catalog 0 0 0
196 0 my @schemas = DBIO::DuckDB::DDL->source_schemas($self->schema);
197 0 ▶ $opts{schemas} = \@schemas if @schemas;
Branch: 1 branch
BranchTrueFalseTotal
if @schemas 0 0 0
198 0 return $self->_introspect_class->new(%opts);
199 }
200  
201 0 ▶sub install {
Subroutine install: not called CC=1 SCAR=6.9
Pod documented
202 0 my ($self) = @_;
203 0 $self->_resolve_extension_list;
204 0 return $self->SUPER::install;
205 }
206  
207  
208 7 ▶sub _resolve_extension_list {
Subroutine _resolve_extension_list: called CC=1 SCAR=0.0
209 7 my ($self) = @_;
210 7 return DBIO::DuckDB::Extension->resolve_list($self->schema);
211 }
212  
213  
214 1 ▶sub _extension_list {
Subroutine _extension_list: called CC=5 SCAR=29.7
215 1 my ($self) = @_;
216 1 ▶ my $list = $self->_resolve_extension_list or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $list = $self->_resolve_extension_list or return [] 0 1 1
217  
218 0 my $rows = $self->_dbh->selectall_arrayref(
219 'SELECT extension_name, installed, extension_version FROM duckdb_extensions()',
220 { Slice => {} },
221 );
222 0 my %known = map { $_->{extension_name} => $_ } @$rows;
223  
224 0 my %state;
225 0 for my $name ($list->names) {
226 0 my $row = $known{$name};
227 $state{$name} = $row && $row->{installed}
228 ? { installed => 1, version => $row->{extension_version} }
229 0 ▶ : { installed => 0 };
Branch: 1 branch
BranchTrueFalseTotal
$row && $row->{'installed'} ? : 0 0 0
Condition0%0/3
$row and $row->{"installed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$row and $row->{"installed"}
$row $row->{"installed"}
Truth table0%0/3
A and B
A= $rowB= $row->{"installed"}
ABresult
0X0
100
111
230 }
231 0 $list->set_installed(\%state);
232  
233 0 return [ $list->entries ];
234 }
235  
236  
237 0 ▶sub _build_target_model {
Subroutine _build_target_model: not called CC=2 SCAR=17.9
238 0 my ($self) = @_;
239  
240 0 my $temp_dbh = DBI->connect('dbi:DuckDB:dbname=:memory:', '', '', {
241 RaiseError => 1, PrintError => 0, AutoCommit => 1,
242 });
243  
244 # the declared extensions are active here as on a connection of the
245 # schema, or a type an extension adds fails in the target model
246 0 my $storage = $self->schema->storage;
247 0 $temp_dbh->do($_) for $storage->_extension_activation_sql($storage->_extension_activation(undef));
248  
249 0 $self->_execute_ddl($temp_dbh, $self->_install_ddl);
250  
251 0 my $target_model = $self->_new_introspect($temp_dbh)->model;
252 0 $temp_dbh->disconnect;
253  
254 0 return $target_model;
255 }
256  
257  
258 7 1;
259  
260 __END__
« blib/lib/DBIO/DuckDB/Diff.pm ↑ Summary blib/lib/DBIO/DuckDB/DDL.pm »