DuckDB/Diff/Column.pm

filter: Statementstmt 43.6% 24 / 55 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.5% 8 / 13 Podpod 100% 3 / 3 totaltotal 33.3% 35 / 105 CC 16 SCAR 48.6
CC
16
Cov
28%
as_sql
240.0
summary
6.0
__ANON__
2.0
CRAP 129.5
« DuckDB/Introspect/Indexes.pm ↑ Summary DuckDB/Introspect/ForeignKeys.pm »
1 package DBIO::DuckDB::Diff::Column;
2 # ABSTRACT: Diff operations for DuckDB columns
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DBIO::DuckDB::DDL::Emit qw(column_def);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DBIO::Diff::Compare qw(norm_type changed_column_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info/);
14  
15  
16  
17 2 ▶sub diff {
Subroutine diff: called CC=1 SCAR=2.0
Pod documented
18 2 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
19  
20 return $class->diff_nested($source_cols, $target_cols,
21 index_by => 'column_name',
22 scope => 'both',
23 source_tables => $source_tables,
24 target_tables => $target_tables,
25 1 ▶ changed_when => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 my ($old, $new) = @_;
27 1 scalar changed_column_fields($old, $new);
28 },
29 0 ▶ on_new => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 my ($table_name, $col_name, $tgt) = @_;
31 0 $class->new(
32 action => 'add',
33 table_name => $table_name,
34 column_name => $col_name,
35 new_info => $tgt,
36 );
37 },
38 0 ▶ on_changed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 0 my ($table_name, $col_name, $old, $new) = @_;
40 0 $class->new(
41 action => 'alter',
42 table_name => $table_name,
43 column_name => $col_name,
44 old_info => $old,
45 new_info => $new,
46 );
47 },
48 0 ▶ on_gone => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 my ($table_name, $col_name, $old) = @_;
50 0 $class->new(
51 action => 'drop',
52 table_name => $table_name,
53 column_name => $col_name,
54 old_info => $old,
55 );
56 },
57 2 );
58 }
59  
60  
61 0 ▶sub as_sql {
Subroutine as_sql: not called CC=15 SCAR=54.8
Pod documented
62 0 my ($self) = @_;
63  
64 0 my $tbl = _quote_ident($self->table_name);
65 0 my $col = _quote_ident($self->column_name);
66  
67 0 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 0 0 0
68 0 my $info = $self->new_info;
69 my $def = column_def(
70 name => $self->column_name,
71 type => ($info->{data_type} || 'VARCHAR'),
72 not_null => $info->{not_null},
73 default => $info->{default_value},
74 0 ▶ );
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
75 0 return sprintf 'ALTER TABLE %s ADD COLUMN %s;', $tbl, $def;
76 }
77  
78 0 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 0 0 0
79 0 return sprintf 'ALTER TABLE %s DROP COLUMN %s;', $tbl, $col;
80 }
81  
82 0 ▶ if ($self->action eq 'alter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "alter") 0 0 0
83 0 my $old = $self->old_info;
84 0 my $new = $self->new_info;
85 0 my @stmts;
86  
87 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
88 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE %s;',
89 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
90 }
91 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
92 push @stmts, $new->{not_null}
93 0 ▶ ? sprintf('ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;', $tbl, $col)
Branch: 1 branch
BranchTrueFalseTotal
$new->{'not_null'} ? : 0 0 0
94 : sprintf('ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;', $tbl, $col);
95 }
96 0 ▶ my $old_d = defined $old->{default_value} ? $old->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $old->{'default_value'} ? : 0 0 0
97 0 ▶ my $new_d = defined $new->{default_value} ? $new->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $new->{'default_value'} ? : 0 0 0
98 0 ▶ if ($old_d ne $new_d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_d ne $new_d) 0 0 0
99 0 ▶ push @stmts, length $new_d
Branch: 1 branch
BranchTrueFalseTotal
length $new_d ? : 0 0 0
100 ? sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;', $tbl, $col, $new_d)
101 : sprintf('ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;', $tbl, $col);
102 }
103 0 return join "\n", @stmts;
104 }
105 }
106  
107  
108 0 ▶sub summary {
Subroutine summary: not called CC=2 SCAR=17.9
Pod documented
109 0 my ($self) = @_;
110 0 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 0 0 0
111 0 return sprintf ' %scolumn: %s.%s%s',
112 $self->summary_prefix, $self->table_name, $self->column_name, $type;
113 }
114  
115 4 1;
116  
117 __END__
118  
119 =pod
120  
121 =encoding UTF-8
122  
123 =head1 NAME
124  
125 DBIO::DuckDB::Diff::Column - Diff operations for DuckDB columns
126  
127 =head1 VERSION
128  
129 version 0.900001
130  
131 =head1 DESCRIPTION
132  
133 Column-level diff operations for DuckDB. Unlike SQLite, DuckDB has a
134 reasonably complete C<ALTER TABLE>:
135  
136 =over 4
137  
138 =item * C<ALTER TABLE ... ADD COLUMN>
139  
140 =item * C<ALTER TABLE ... DROP COLUMN>
141  
142 =item * C<ALTER TABLE ... ALTER COLUMN ... SET DATA TYPE>
143  
144 =item * C<ALTER TABLE ... ALTER COLUMN ... SET / DROP NOT NULL>
145  
146 =item * C<ALTER TABLE ... ALTER COLUMN ... SET / DROP DEFAULT>
147  
148 =back
149  
150 So actual type and nullability changes are emitted as real ALTER statements
151 rather than commented-out warnings.
152  
153 Brand-new tables get their columns inline via L<DBIO::DuckDB::Diff::Table>
154 -- this module only sees columns of tables that exist in both models.
155  
156 =head1 METHODS
157  
158 =head2 diff
159  
160 =head2 as_sql
161  
162 =head2 summary
163  
164 =head1 AUTHOR
165  
166 DBIO Authors
167  
168 =head1 COPYRIGHT AND LICENSE
169  
170 Copyright (C) 2026 DBIO Authors
171  
172 This is free software; you can redistribute it and/or modify it under
173 the same terms as the Perl 5 programming language system itself.
174  
175 =cut
« DuckDB/Introspect/Indexes.pm ↑ Summary DuckDB/Introspect/ForeignKeys.pm »