blib/lib/DBIO/DuckDB/Diff/Column.pm

filter: Statementstmt 39.1% 36 / 92 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 44.4% 8 / 18 Podpod 100% 6 / 6 totaltotal 26.4% 50 / 189 CC 30 SCAR 54.9
CC
30
Cov
24%
_parts
506.0
_sized
30.0
lossy
12.0
CRAP 241.7
« blib/lib/DBIO/DuckDB/Introspect/Indexes.pm ↑ Summary blib/lib/DBIO/DuckDB/Diff.pm »
1 package DBIO::DuckDB::Diff::Column;
2 # ABSTRACT: Diff operations for DuckDB columns
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::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use DBIO::DuckDB::DDL::Emit qw(quote_ident quote_table);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use DBIO::DuckDB::DDL::Emit qw(column_def);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use DBIO::Diff::Compare qw(norm_type changed_column_fields column_change_lossy);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info/);
16  
17 # a default DuckDB takes in ADD COLUMN: a number, a quoted string, NULL, a boolean
18 8 our $CONSTANT_DEFAULT = qr/\A\s*(?:-?\d+(?:\.\d+)?|'(?:[^']|'')*'|NULL|TRUE|FALSE)\s*\z/i;
19  
20  
21  
22 2 ▶sub diff {
Subroutine diff: called CC=1 SCAR=2.0
Pod documented
23 2 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
24  
25 return $class->diff_nested($source_cols, $target_cols,
26 index_by => 'column_name',
27 scope => 'both',
28 source_tables => $source_tables,
29 target_tables => $target_tables,
30 1 ▶ changed_when => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
31 1 my ($old, $new) = @_;
32 1 scalar changed_column_fields($old, $new);
33 },
34 0 ▶ on_new => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 my ($table_name, $col_name, $tgt) = @_;
36 0 $class->new(
37 action => 'add',
38 table_name => $table_name,
39 column_name => $col_name,
40 new_info => $tgt,
41 );
42 },
43 0 ▶ on_changed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 my ($table_name, $col_name, $old, $new) = @_;
45 0 $class->new(
46 action => 'alter',
47 table_name => $table_name,
48 column_name => $col_name,
49 old_info => $old,
50 new_info => $new,
51 );
52 },
53 0 ▶ on_gone => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my ($table_name, $col_name, $old) = @_;
55 0 $class->new(
56 action => 'drop',
57 table_name => $table_name,
58 column_name => $col_name,
59 old_info => $old,
60 );
61 },
62 2 );
63 }
64  
65  
66 0 ▶sub as_sql {
Subroutine as_sql: not called CC=2 SCAR=17.9
Pod documented
67 0 my ($self) = @_;
68 0 my ($early, $late) = $self->_parts;
69 0 ▶ return length $late ? "$early\n$late" : $early;
Branch: 1 branch
BranchTrueFalseTotal
length $late ? : 0 0 0
70 }
71  
72  
73 0 ▶sub early_sql { ($_[0]->_parts)[0] }
Subroutine early_sql: not called CC=1 SCAR=6.9
Pod documented
74 0 ▶sub late_sql { ($_[0]->_parts)[1] }
Subroutine late_sql: not called CC=1 SCAR=6.9
Pod documented
75  
76 0 ▶sub _parts {
Subroutine _parts: not called CC=22 SCAR=62.3
77 0 my ($self) = @_;
78 0 my $late = '';
79  
80 0 my $tbl = quote_table($self->table_name);
81 0 my $col = quote_ident($self->column_name);
82  
83 0 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 0 0 0
84 0 my $info = $self->new_info;
85 0 my $default = $info->{default_value};
86 0 ▶ my $type = $info->{data_type} || 'VARCHAR';
Condition0%0/2
$info->{'data_type'} || 'VARCHAR'
l!l
00
MC/DC0%0/1
$info->{'data_type'} || 'VARCHAR'
$info->{'data_type'}
Truth table0%0/2
A
A= $info->{'data_type'}
Aresult
11
00
87  
88 # DuckDB takes ADD COLUMN with a constant default only: NOT NULL is
89 # refused there ("Adding columns with constraints not yet supported") and
90 # so is an expression default ("Cannot prepare multiple statements at
91 # once"). The rest is done after the column exists.
92 0 ▶ my $constant = defined $default && $default =~ $CONSTANT_DEFAULT;
Condition0%0/3
defined $default && $default =~ /$DBIO::DuckDB::Diff::Column::CONSTANT_DEFAULT/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $default && $default =~ /$DBIO::DuckDB::Diff::Column::CONSTANT_DEFAULT/
defined $default $default =~ /$DBIO::DuckDB::Diff::Column::CONSTANT_DEFAULT/
Truth table0%0/3
A && B
A= defined $defaultB= $default =~ /$DBIO::DuckDB::Diff::Column::CONSTANT_DEFAULT/
ABresult
0X0
100
111
93 0 ▶ my @stmts = sprintf 'ALTER TABLE %s ADD COLUMN %s;', $tbl, column_def(
Branch: 1 branch
BranchTrueFalseTotal
$constant ? : 0 0 0
94 name => $self->column_name,
95 type => $type,
96 default => ($constant ? $default : undef),
97 );
98 0 ▶ if (defined $default && !$constant) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default and not $constant) 0 0 0
Condition0%0/3
defined $default and not $constant
!ll&&!rl&&r
000
MC/DC0%0/2
defined $default and not $constant
defined $default not $constant
Truth table0%0/3
A and not(B)
A= defined $defaultB= not $constant
ABresult
0X0
100
111
99 0 push @stmts,
100 sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;', $tbl, $col, $default),
101 sprintf('UPDATE %s SET %s = %s;', $tbl, $col, $default);
102 }
103 0 ▶ if ($info->{not_null}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"not_null"}) 0 0 0
104 0 my $not_null = sprintf 'ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;', $tbl, $col;
105 # after an UPDATE of the same transaction DuckDB cannot rebuild the
106 # table's indexes ("Cannot create index with outstanding updates"), so
107 # this one waits for the transaction of the rest (late_sql)
108 0 ▶ if (defined $default && !$constant) { $late = $not_null }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default and not $constant) { } 0 0 0
Condition0%0/3
defined $default and not $constant
!ll&&!rl&&r
000
MC/DC0%0/2
defined $default and not $constant
defined $default not $constant
Truth table0%0/3
A and not(B)
A= defined $defaultB= not $constant
ABresult
0X0
100
111
109 0 else { push @stmts, $not_null }
110 }
111 0 return (join("\n", @stmts), $late);
112 }
113  
114 0 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 0 0 0
115 0 return (sprintf('ALTER TABLE %s DROP COLUMN %s;', $tbl, $col), '');
116 }
117  
118 0 ▶ if ($self->action eq 'alter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "alter") 0 0 0
119 0 my $old = $self->old_info;
120 0 my $new = $self->new_info;
121 0 my @stmts;
122  
123 0 ▶ if (norm_type($old->{data_type}) ne norm_type($new->{data_type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"})) 0 0 0
124 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE %s;',
125 0 ▶ $tbl, $col, ($new->{data_type} || 'VARCHAR');
Condition0%0/2
$new->{'data_type'} || 'VARCHAR'
l!l
00
MC/DC0%0/1
$new->{'data_type'} || 'VARCHAR'
$new->{'data_type'}
Truth table0%0/2
A
A= $new->{'data_type'}
Aresult
11
00
126 }
127 0 ▶ if (($old->{not_null} // 0) != ($new->{not_null} // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($old->{"not_null"} // 0) != ($new->{"not_null"} // 0)) 0 0 0
Condition0%0/4
$old->{"not_null"} // 0
l!l
00
$new->{"not_null"} // 0
l!l
00
MC/DC0%0/1
$old->{"not_null"} // 0
$old->{"not_null"}
MC/DC0%0/1
$new->{"not_null"} // 0
$new->{"not_null"}
Truth table0%0/2
A
A= $old->{"not_null"}
Aresult
11
00
Truth table0%0/2
A
A= $new->{"not_null"}
Aresult
11
00
128 push @stmts, $new->{not_null}
129 0 ▶ ? sprintf('ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;', $tbl, $col)
Branch: 1 branch
BranchTrueFalseTotal
$new->{'not_null'} ? : 0 0 0
130 : sprintf('ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;', $tbl, $col);
131 }
132 0 ▶ my $old_d = defined $old->{default_value} ? $old->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $old->{'default_value'} ? : 0 0 0
133 0 ▶ my $new_d = defined $new->{default_value} ? $new->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $new->{'default_value'} ? : 0 0 0
134 0 ▶ if ($old_d ne $new_d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_d ne $new_d) 0 0 0
135 0 ▶ push @stmts, length $new_d
Branch: 1 branch
BranchTrueFalseTotal
length $new_d ? : 0 0 0
136 ? sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;', $tbl, $col, $new_d)
137 : sprintf('ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;', $tbl, $col);
138 }
139 0 return (join("\n", @stmts), '');
140 }
141 }
142  
143  
144 8 our %LOSSLESS_PAIRS;
145 {
146 8 my @signed = qw(TINYINT SMALLINT INTEGER BIGINT HUGEINT);
147 8 my @unsigned = qw(UTINYINT USMALLINT UINTEGER UBIGINT);
148 8 for my $ladder (\@signed, \@unsigned) {
149 16 for my $from (0 .. $#$ladder - 1) {
150 56 $LOSSLESS_PAIRS{lc "$ladder->[$from]->$ladder->[$_]"} = 1 for $from + 1 .. $#$ladder;
151 }
152 }
153 # an unsigned integer fits every signed one of more bits
154 8 for my $at (0 .. $#unsigned) {
155 32 $LOSSLESS_PAIRS{lc "$unsigned[$at]->$_"} = 1 for @signed[$at + 1 .. $#signed];
156 }
157 8 $LOSSLESS_PAIRS{'date->timestamp'} = 1;
158 }
159  
160  
161 0 ▶sub lossy {
Subroutine lossy: not called CC=3 SCAR=24.8
Pod documented
162 0 my ($self) = @_;
163 0 ▶ return 0 unless $self->action eq 'alter';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->action eq "alter" 0 0 0
164 0 ▶ return column_change_lossy(
Branch: 1 branch
BranchTrueFalseTotal
column_change_lossy(_sized($self->old_info), _sized($self->new_info), \%DBIO::DuckDB::Diff::Column::LOSSLESS_PAIRS) ? : 0 0 0
165 _sized($self->old_info), _sized($self->new_info), \%LOSSLESS_PAIRS,
166 ) ? 1 : 0;
167 }
168  
169 # The info as column_change_lossy reads it: the base type, and the precision
170 # and scale of DECIMAL(p,s) as the size.
171 0 ▶sub _sized {
Subroutine _sized: not called CC=5 SCAR=34.0
172 0 my ($info) = @_;
173 0 ▶ $info ||= {};
Condition0%0/2
$info ||= {}
l!l
00
MC/DC0%0/1
$info ||= {}
$info
Truth table0%0/2
A
A= $info
Aresult
11
00
174 0 my $type = $info->{data_type};
175 0 ▶ return { %$info } unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
176 0 ▶ return { %$info, data_type => $1, size => [ $2, $3 // 0 ] }
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\A\s*(\w+)\s*\(\s*(\d+)\s*(?:,\s*(\d+)\s*)?\)\s*\z/ 0 0 0
Condition0%0/2
$3 // 0
l!l
00
MC/DC0%0/1
$3 // 0
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
177 if $type =~ /\A\s*(\w+)\s*\(\s*(\d+)\s*(?:,\s*(\d+)\s*)?\)\s*\z/;
178 0 return { %$info };
179 }
180  
181  
182 0 ▶sub summary {
Subroutine summary: not called CC=2 SCAR=17.9
Pod documented
183 0 my ($self) = @_;
184 0 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 0 0 0
185 0 return sprintf ' %scolumn: %s.%s%s',
186 $self->summary_prefix, $self->table_name, $self->column_name, $type;
187 }
188  
189 8 1;
190  
191 __END__
« blib/lib/DBIO/DuckDB/Introspect/Indexes.pm ↑ Summary blib/lib/DBIO/DuckDB/Diff.pm »