DB2/Diff/Column.pm

filter: Statementstmt 95.7% 68 / 71 Branchbran 91.1% 31 / 34 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 90.3% 121 / 134 CC 26 SCAR 23.7
CC
26
Cov
92%
as_sql
15.0
diff
11.1
summary
6.0
CRAP 10.7
« DB2/Introspect/Tables.pm ↑ Summary DB2/Diff/Index.pm »
1 package DBIO::DB2::Diff::Column;
2 # ABSTRACT: Diff operations for DB2 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::DB2::Type qw(_db2_column_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DBIO::Diff::Compare qw(changed_column_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 4 __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info/);
15  
16  
17 13 ▶sub diff {
Subroutine diff: called CC=11 SCAR=24.0
Pod documented
18 13 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
19 13 my @ops;
20  
21 13 for my $table_name (sort keys %$target_cols) {
22 next unless exists $source_tables->{$table_name}
23 14 ▶ && exists $target_tables->{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_tables->{$table_name} and exists $target_tables->{$table_name} 3 11 14
Condition66%2/3
exists $source_tables->{$table_name} and exists $target_tables->{$table_name}
!ll&&!rl&&r
3011
MC/DC50%1/2
exists $source_tables->{$table_name} and exists $target_tables->{$table_name}
exists $source_tables->{$table_name} exists $target_tables->{$table_name}
Truth table66%2/3
A and B
A= exists $source_tables->{$table_name}B= exists $target_tables->{$table_name}
ABresult
0X0
100
111
24  
25 11 ▶ my %src_by_name = map { $_->{column_name} => $_ } @{ $source_cols->{$table_name} // [] };
Condition50%1/2
$source_cols->{$table_name} // []
l!l
110
MC/DC0%0/1
$source_cols->{$table_name} // []
$source_cols->{$table_name}
Truth table50%1/2
A
A= $source_cols->{$table_name}
Aresult
11
00
26 11 ▶ my %tgt_by_name = map { $_->{column_name} => $_ } @{ $target_cols->{$table_name} // [] };
Condition50%1/2
$target_cols->{$table_name} // []
l!l
110
MC/DC0%0/1
$target_cols->{$table_name} // []
$target_cols->{$table_name}
Truth table50%1/2
A
A= $target_cols->{$table_name}
Aresult
11
00
27  
28 11 for my $col_name (sort keys %tgt_by_name) {
29 16 my $tgt = $tgt_by_name{$col_name};
30  
31 16 ▶ if (!exists $src_by_name{$col_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src_by_name{$col_name}) 4 12 16
32 4 push @ops, $class->new(
33 action => 'add',
34 table_name => $table_name,
35 column_name => $col_name,
36 new_info => $tgt,
37 );
38 4 next;
39 }
40  
41 12 my $src = $src_by_name{$col_name};
42  
43 12 ▶ if (scalar changed_column_fields($src, $tgt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar changed_column_fields($src, $tgt)) 2 10 12
44 2 push @ops, $class->new(
45 action => 'alter',
46 table_name => $table_name,
47 column_name => $col_name,
48 old_info => $src,
49 new_info => $tgt,
50 );
51 }
52 }
53  
54 11 for my $col_name (sort keys %src_by_name) {
55 13 ▶ next if exists $tgt_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt_by_name{$col_name} 12 1 13
56 push @ops, $class->new(
57 action => 'drop',
58 table_name => $table_name,
59 column_name => $col_name,
60 1 old_info => $src_by_name{$col_name},
61 );
62 }
63 }
64  
65 13 return @ops;
66 }
67  
68  
69 12 ▶sub as_sql {
Subroutine as_sql: called CC=15 SCAR=27.1
Pod documented
70 12 my ($self) = @_;
71  
72 12 my $tbl = _quote_ident($self->table_name);
73 12 my $col = _quote_ident($self->column_name);
74  
75 12 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 8 4 12
76 8 my $info = $self->new_info;
77 8 my $type = _db2_column_type($info->{data_type}, $info->{size});
78 8 my $sql = sprintf 'ALTER TABLE %s ADD COLUMN %s %s', $tbl, $col, $type;
79 8 ▶ $sql .= ' NOT NULL' if $info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"not_null"} 1 7 8
80 8 ▶ if (defined $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"}) 1 7 8
81 1 $sql .= " DEFAULT $info->{default_value}";
82 }
83 8 return "$sql;";
84 }
85  
86 4 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 1 3 4
87 1 return sprintf 'ALTER TABLE %s DROP COLUMN %s;', $tbl, $col;
88 }
89  
90 3 ▶ if ($self->action eq 'alter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "alter") 3 0 3
91 3 my $old = $self->old_info;
92 3 my $new = $self->new_info;
93 3 my @stmts;
94  
95 3 ▶ if (_db2_column_type($old->{data_type}, $old->{size})
Branch: 1 branch
BranchTrueFalseTotal
if (_db2_column_type($old->{"data_type"}, $old->{"size"}) ne _db2_column_type($new->{"data_type"}, $new->{"size"})) 1 2 3
96 ne _db2_column_type($new->{data_type}, $new->{size})) {
97 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE %s;',
98 1 $tbl, $col, _db2_column_type($new->{data_type}, $new->{size});
99 }
100 3 ▶ if (($old->{not_null} // 0) != ($new->{not_null} // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($old->{"not_null"} // 0) != ($new->{"not_null"} // 0)) 2 1 3
Condition100%4/4
$old->{"not_null"} // 0
l!l
21
$new->{"not_null"} // 0
l!l
21
MC/DC100%1/1
$old->{"not_null"} // 0
$old->{"not_null"}
MC/DC100%1/1
$new->{"not_null"} // 0
$new->{"not_null"}
Truth table100%2/2
A
A= $old->{"not_null"}
Aresult
11
00
Truth table100%2/2
A
A= $new->{"not_null"}
Aresult
11
00
101 push @stmts, $new->{not_null}
102 2 ▶ ? sprintf('ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;', $tbl, $col)
Branch: 1 branch
BranchTrueFalseTotal
$new->{'not_null'} ? : 1 1 2
103 : sprintf('ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;', $tbl, $col);
104 }
105 3 ▶ my $old_d = defined $old->{default_value} ? $old->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $old->{'default_value'} ? : 2 1 3
106 3 ▶ my $new_d = defined $new->{default_value} ? $new->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $new->{'default_value'} ? : 1 2 3
107 3 ▶ if ($old_d ne $new_d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_d ne $new_d) 2 1 3
108 2 ▶ push @stmts, length $new_d
Branch: 1 branch
BranchTrueFalseTotal
length $new_d ? : 1 1 2
109 ? sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;', $tbl, $col, $new_d)
110 : sprintf('ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;', $tbl, $col);
111 }
112 3 return join "\n", @stmts;
113 }
114 }
115  
116  
117 0 ▶sub summary {
Subroutine summary: not called CC=2 SCAR=17.9
Pod documented
118 0 my ($self) = @_;
119 0 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 0 0 0
120 0 return sprintf ' %scolumn: %s.%s%s',
121 $self->summary_prefix, $self->table_name, $self->column_name, $type;
122 }
123  
124 4 1;
125  
126 __END__
127  
128 =pod
129  
130 =encoding UTF-8
131  
132 =head1 NAME
133  
134 DBIO::DB2::Diff::Column - Diff operations for DB2 columns
135  
136 =head1 VERSION
137  
138 version 0.900001
139  
140 =head1 DESCRIPTION
141  
142 Column-level diff operations for DB2. DB2 has a reasonably complete
143 C<ALTER TABLE> repertoire:
144  
145 =over 4
146  
147 =item * C<ALTER TABLE ... ADD COLUMN>
148  
149 =item * C<ALTER TABLE ... DROP COLUMN>
150  
151 =item * C<ALTER TABLE ... ALTER COLUMN ... SET DATA TYPE>
152  
153 =item * C<ALTER TABLE ... ALTER COLUMN ... SET NOT NULL / DROP NOT NULL>
154  
155 =item * C<ALTER TABLE ... ALTER COLUMN ... SET DEFAULT / DROP DEFAULT>
156  
157 =back
158  
159 Brand-new tables get their columns inline via L<DBIO::DB2::Diff::Table>
160 -- this module only sees columns of tables that exist in both models.
161  
162 C<new>, C<action> and C<summary_prefix> come from L<DBIO::Diff::Op>.
163  
164 =head1 METHODS
165  
166 =head2 diff
167  
168 =head2 as_sql
169  
170 =head2 summary
171  
172 =head1 AUTHOR
173  
174 DBIO & DBIx::Class Authors
175  
176 =head1 COPYRIGHT AND LICENSE
177  
178 Copyright (C) 2026 DBIO Authors
179 Portions Copyright (C) 2005-2025 DBIx::Class Authors
180 Based on DBIx::Class, heavily modified.
181  
182 This is free software; you can redistribute it and/or modify it under
183 the same terms as the Perl 5 programming language system itself.
184  
185 =cut
« DB2/Introspect/Tables.pm ↑ Summary DB2/Diff/Index.pm »