PostgreSQL/Diff/Column.pm

filter: Statementstmt 86.2% 69 / 80 Branchbran 75.0% 30 / 40 Conditioncond 56.2% 18 / 32 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 87.5% 7 / 8 Podpod 100% 3 / 3 totaltotal 72.9% 132 / 181 CC 39 SCAR 33.7
CC
39
Cov
77%
as_sql
44.0
diff
13.1
summary
6.0
CRAP 29.0
« PostgreSQL/Introspect/Indexes.pm ↑ Summary PostgreSQL/Introspect/CheckConstraints.pm »
1 package DBIO::PostgreSQL::Diff::Column;
2 # ABSTRACT: Diff operations for PostgreSQL columns
3  
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use DBIO::PostgreSQL::Introspect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 __PACKAGE__->mk_diff_accessors(qw(table_key column_name old_info new_info));
13  
14  
15  
16  
17  
18  
19  
20 # Field comparison spec for the PostgreSQL column model. Extends the
21 # canonical data_type / not_null / default_value / size set with the
22 # PostgreSQL-specific identity and generated flags (attidentity,
23 # attgenerated from pg_catalog). Both are compared as scalars — they are
24 # short opcodes ('a'/'d'/'s'/''), not user data.
25 9 my %_COLUMN_FIELD_SPEC = (
26 type => [qw/data_type/],
27 bool => [qw/not_null/],
28 scalar => [qw/default_value identity generated/],
29 );
30  
31 11 ▶sub diff {
Subroutine diff: called CC=13 SCAR=25.7
Pod documented
32 11 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
33 11 my @ops;
34  
35 # Only diff columns for tables that exist in both source and target.
36 # Brand new tables are handled by Diff::Table as full CREATE TABLE
37 # statements with columns inline.
38 11 for my $table_key (sort keys %$target_cols) {
39 10 ▶ next unless exists $source_tables->{$table_key} && exists $target_tables->{$table_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_tables->{$table_key} and exists $target_tables->{$table_key} 1 9 10
Condition66%2/3
exists $source_tables->{$table_key} and exists $target_tables->{$table_key}
!ll&&!rl&&r
109
MC/DC50%1/2
exists $source_tables->{$table_key} and exists $target_tables->{$table_key}
exists $source_tables->{$table_key} exists $target_tables->{$table_key}
Truth table66%2/3
A and B
A= exists $source_tables->{$table_key}B= exists $target_tables->{$table_key}
ABresult
0X0
100
111
40  
41 9 ▶ my %source_by_name = map { $_->{column_name} => $_ } @{ $source_cols->{$table_key} // [] };
Condition50%1/2
$source_cols->{$table_key} // []
l!l
90
MC/DC0%0/1
$source_cols->{$table_key} // []
$source_cols->{$table_key}
Truth table50%1/2
A
A= $source_cols->{$table_key}
Aresult
11
00
42 9 ▶ my %target_by_name = map { $_->{column_name} => $_ } @{ $target_cols->{$table_key} // [] };
Condition50%1/2
$target_cols->{$table_key} // []
l!l
90
MC/DC0%0/1
$target_cols->{$table_key} // []
$target_cols->{$table_key}
Truth table50%1/2
A
A= $target_cols->{$table_key}
Aresult
11
00
43  
44 # New columns
45 9 for my $col_name (sort keys %target_by_name) {
46 10 ▶ next if exists $source_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $source_by_name{$col_name} 7 3 10
47 push @ops, $class->new(
48 action => 'add',
49 table_key => $table_key,
50 column_name => $col_name,
51 3 new_info => $target_by_name{$col_name},
52 );
53 }
54  
55 # Dropped columns
56 9 for my $col_name (sort keys %source_by_name) {
57 8 ▶ next if exists $target_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target_by_name{$col_name} 7 1 8
58 push @ops, $class->new(
59 action => 'drop',
60 table_key => $table_key,
61 column_name => $col_name,
62 1 old_info => $source_by_name{$col_name},
63 );
64 }
65  
66 # Altered columns
67 9 for my $col_name (sort keys %target_by_name) {
68 10 ▶ next unless exists $source_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_by_name{$col_name} 3 7 10
69 7 my $src = $source_by_name{$col_name};
70 7 my $tgt = $target_by_name{$col_name};
71  
72 7 my @changed = changed_fields($src, $tgt, %_COLUMN_FIELD_SPEC);
73 7 ▶ next unless @changed;
Branch: 1 branch
BranchTrueFalseTotal
unless @changed 3 4 7
74  
75 4 push @ops, $class->new(
76 action => 'alter',
77 table_key => $table_key,
78 column_name => $col_name,
79 old_info => $src,
80 new_info => $tgt,
81 );
82 }
83 }
84  
85 11 return @ops;
86 }
87  
88  
89 9 ▶sub as_sql {
Subroutine as_sql: called CC=26 SCAR=37.9
Pod documented
90 9 my ($self) = @_;
91 9 ▶ if ($self->action eq 'add') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->action eq 'add') { } 4 5 9
elsif ($self->action eq 'drop') { } 1 4 5
elsif ($self->action eq 'alter') { } 4 0 4
92 4 my $type = $self->new_info->{data_type};
93 4 my $sql = sprintf 'ALTER TABLE %s ADD COLUMN %s %s',
94 $self->table_key, $self->column_name, $type;
95 4 ▶ $sql .= ' NOT NULL' if $self->new_info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $self->new_info->{"not_null"} 3 1 4
96 4 my $kind = DBIO::PostgreSQL::Introspect->identity_kind($self->new_info->{identity});
97 4 ▶ if ($kind) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind) { } 3 1 4
elsif (defined $self->new_info->{'default_value'}) { } 0 1 1
98 3 $sql .= " GENERATED $kind AS IDENTITY";
99 }
100 elsif (defined $self->new_info->{default_value}) {
101 0 $sql .= sprintf ' DEFAULT %s', $self->new_info->{default_value};
102 }
103 4 return "$sql;";
104 }
105 elsif ($self->action eq 'drop') {
106 1 return sprintf 'ALTER TABLE %s DROP COLUMN %s;',
107 $self->table_key, $self->column_name;
108 }
109 elsif ($self->action eq 'alter') {
110 4 my @stmts;
111 4 my $src = $self->old_info;
112 4 my $tgt = $self->new_info;
113  
114 4 ▶ if (($src->{data_type} // '') ne ($tgt->{data_type} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (($src->{"data_type"} // "") ne ($tgt->{"data_type"} // "")) 1 3 4
Condition50%2/4
$src->{"data_type"} // ""
l!l
40
$tgt->{"data_type"} // ""
l!l
40
MC/DC0%0/1
$src->{"data_type"} // ""
$src->{"data_type"}
MC/DC0%0/1
$tgt->{"data_type"} // ""
$tgt->{"data_type"}
Truth table50%1/2
A
A= $src->{"data_type"}
Aresult
11
00
Truth table50%1/2
A
A= $tgt->{"data_type"}
Aresult
11
00
115 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s TYPE %s;',
116 1 $self->table_key, $self->column_name, $tgt->{data_type};
117 }
118 4 ▶ if (($src->{not_null} // 0) != ($tgt->{not_null} // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($src->{"not_null"} // 0) != ($tgt->{"not_null"} // 0)) 0 4 4
Condition50%2/4
$src->{"not_null"} // 0
l!l
40
$tgt->{"not_null"} // 0
l!l
40
MC/DC0%0/1
$src->{"not_null"} // 0
$src->{"not_null"}
MC/DC0%0/1
$tgt->{"not_null"} // 0
$tgt->{"not_null"}
Truth table50%1/2
A
A= $src->{"not_null"}
Aresult
11
00
Truth table50%1/2
A
A= $tgt->{"not_null"}
Aresult
11
00
119 0 ▶ if ($tgt->{not_null}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tgt->{'not_null'}) { } 0 0 0
120 0 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;',
121 $self->table_key, $self->column_name;
122 } else {
123 0 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL;',
124 $self->table_key, $self->column_name;
125 }
126 }
127 4 ▶ if (($src->{default_value} // '') ne ($tgt->{default_value} // '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (($src->{"default_value"} // "") ne ($tgt->{"default_value"} // "")) 0 4 4
Condition50%2/4
$src->{"default_value"} // ""
l!l
04
$tgt->{"default_value"} // ""
l!l
04
MC/DC0%0/1
$src->{"default_value"} // ""
$src->{"default_value"}
MC/DC0%0/1
$tgt->{"default_value"} // ""
$tgt->{"default_value"}
Truth table50%1/2
A
A= $src->{"default_value"}
Aresult
11
00
Truth table50%1/2
A
A= $tgt->{"default_value"}
Aresult
11
00
128 0 ▶ if (defined $tgt->{default_value} && $tgt->{default_value} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tgt->{'default_value'} and $tgt->{'default_value'} ne '') { } 0 0 0
Condition0%0/3
defined $tgt->{"default_value"} and $tgt->{"default_value"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tgt->{"default_value"} and $tgt->{"default_value"} ne ""
defined $tgt->{"default_value"} $tgt->{"default_value"} ne ""
Truth table0%0/3
A and B
A= defined $tgt->{"default_value"}B= $tgt->{"default_value"} ne ""
ABresult
0X0
100
111
129 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;',
130 0 $self->table_key, $self->column_name, $tgt->{default_value};
131 } else {
132 0 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT;',
133 $self->table_key, $self->column_name;
134 }
135 }
136  
137 # Identity transitions. attidentity is '' (none), 'a' (ALWAYS), 'd' (BY DEFAULT).
138 4 ▶ my $src_id = $src->{identity} // '';
Condition100%2/2
$src->{"identity"} // ""
l!l
31
MC/DC100%1/1
$src->{"identity"} // ""
$src->{"identity"}
Truth table100%2/2
A
A= $src->{"identity"}
Aresult
11
00
139 4 ▶ my $tgt_id = $tgt->{identity} // '';
Condition100%2/2
$tgt->{"identity"} // ""
l!l
31
MC/DC100%1/1
$tgt->{"identity"} // ""
$tgt->{"identity"}
Truth table100%2/2
A
A= $tgt->{"identity"}
Aresult
11
00
140 4 ▶ if ($src_id ne $tgt_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_id ne $tgt_id) 3 1 4
141 3 my $src_kind = DBIO::PostgreSQL::Introspect->identity_kind($src_id);
142 3 my $tgt_kind = DBIO::PostgreSQL::Introspect->identity_kind($tgt_id);
143 3 ▶ if (!$src_kind && $tgt_kind) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $src_kind and $tgt_kind) { } 1 2 3
elsif ($src_kind and not $tgt_kind) { } 1 1 2
Condition66%2/3
not $src_kind and $tgt_kind
!ll&&!rl&&r
201
MC/DC50%1/2
not $src_kind and $tgt_kind
not $src_kind $tgt_kind
Truth table66%2/3
not(A) and B
A= not $src_kindB= $tgt_kind
ABresult
0X0
100
111
144 1 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s ADD GENERATED %s AS IDENTITY;',
145 $self->table_key, $self->column_name, $tgt_kind;
146 }
147 ▶ elsif ($src_kind && !$tgt_kind) {
Condition66%2/3
$src_kind and not $tgt_kind
!ll&&!rl&&r
011
MC/DC50%1/2
$src_kind and not $tgt_kind
$src_kind not $tgt_kind
Truth table66%2/3
A and not(B)
A= $src_kindB= not $tgt_kind
ABresult
0X0
100
111
148 1 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s DROP IDENTITY;',
149 $self->table_key, $self->column_name;
150 }
151 else {
152 1 push @stmts, sprintf 'ALTER TABLE %s ALTER COLUMN %s SET GENERATED %s;',
153 $self->table_key, $self->column_name, $tgt_kind;
154 }
155 }
156 4 return join "\n", @stmts;
157 }
158 }
159  
160  
161 0 ▶sub summary {
Subroutine summary: not called CC=2 SCAR=17.9
Pod documented
162 0 my ($self) = @_;
163 0 my $prefix = $self->summary_prefix;
164 0 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 0 0 0
165 0 return sprintf ' %scolumn: %s.%s%s', $prefix, $self->table_key, $self->column_name, $type;
166 }
167  
168 9 1;
169  
170 __END__
171  
172 =pod
173  
174 =encoding UTF-8
175  
176 =head1 NAME
177  
178 DBIO::PostgreSQL::Diff::Column - Diff operations for PostgreSQL columns
179  
180 =head1 VERSION
181  
182 version 0.900001
183  
184 =head1 DESCRIPTION
185  
186 Represents a column-level diff operation: C<ADD COLUMN>, C<DROP COLUMN>, or
187 C<ALTER COLUMN> (type change, nullability change, or default change). Only
188 columns on tables that exist in both source and target are compared.
189  
190 =head1 ATTRIBUTES
191  
192 =head2 table_key
193  
194 The C<schema.table> key identifying which table the column belongs to.
195  
196 =head2 column_name
197  
198 The column name.
199  
200 =head2 old_info
201  
202 The source column metadata hashref (present for C<drop> and C<alter>).
203  
204 =head2 new_info
205  
206 The target column metadata hashref (present for C<add> and C<alter>).
207  
208 =head1 METHODS
209  
210 =head2 diff
211  
212 my @ops = DBIO::PostgreSQL::Diff::Column->diff(
213 $source_cols, $target_cols, $source_tables, $target_tables,
214 );
215  
216 Compares column lists for tables that exist in both source and target.
217 Detects added columns, dropped columns, and altered columns (data type,
218 C<NOT NULL>, default value, or identity kind).
219  
220 =head2 as_sql
221  
222 Returns one or more C<ALTER TABLE> statements for this operation. For C<alter>,
223 may return multiple statements (one per changed attribute).
224  
225 =head2 summary
226  
227 Returns a one-line description such as C<+column: auth.users.avatar (text)>.
228  
229 =head1 AUTHOR
230  
231 DBIO & DBIx::Class Authors
232  
233 =head1 COPYRIGHT AND LICENSE
234  
235 Copyright (C) 2026 DBIO Authors
236 Portions Copyright (C) 2005-2025 DBIx::Class Authors
237 Based on DBIx::Class, heavily modified.
238  
239 This is free software; you can redistribute it and/or modify it under
240 the same terms as the Perl 5 programming language system itself.
241  
242 =cut
« PostgreSQL/Introspect/Indexes.pm ↑ Summary PostgreSQL/Introspect/CheckConstraints.pm »