SQLite/Diff/Column.pm

filter: Statementstmt 100% 46 / 46 Branchbran 65.0% 13 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 85.7% 78 / 91 CC 14 SCAR 18.2
CC
14
Cov
86%
as_sql
11.7
summary
5.0
BEGIN
1.0
CRAP 6.2
« SQLite/Introspect/Indexes.pm ↑ Summary SQLite/Introspect/Columns.pm »
1 package DBIO::SQLite::Diff::Column;
2 # ABSTRACT: Diff operations for SQLite columns
3  
4 198 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 198 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 198 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 198 ▶use DBIO::Diff::Compare qw(changed_column_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 198 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 198 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 198 __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info/);
15  
16  
17 25 ▶sub diff {
Subroutine diff: called CC=1 SCAR=0.0
Pod documented
18 25 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
19  
20 # The create/change/drop walk over columns nested under retained tables is
21 # the generic DBIO::Diff::Op->diff_nested. Only tables present in both models
22 # are walked (scope 'both') -- columns of brand-new / dropped tables ride
23 # along with the table op. changed_column_fields is the per-engine "did this
24 # column change" predicate; everything else here is SQLite-shaped ops.
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 56 ▶ changed_when => sub { scalar changed_column_fields($_[0], $_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 5 ▶ on_new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 5 my ($table, $name, $new) = @_;
33 5 $class->new(
34 action => 'add',
35 table_name => $table,
36 column_name => $name,
37 new_info => $new,
38 );
39 },
40 7 ▶ on_changed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 7 my ($table, $name, $old, $new) = @_;
42 7 $class->new(
43 action => 'alter',
44 table_name => $table,
45 column_name => $name,
46 old_info => $old,
47 new_info => $new,
48 );
49 },
50 1 ▶ on_gone => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 1 my ($table, $name, $old) = @_;
52 1 $class->new(
53 action => 'drop',
54 table_name => $table,
55 column_name => $name,
56 old_info => $old,
57 );
58 },
59 25 );
60 }
61  
62  
63 9 ▶sub as_sql {
Subroutine as_sql: called CC=11 SCAR=24.6
Pod documented
64 9 my ($self) = @_;
65  
66 9 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 5 4 9
67 5 my $info = $self->new_info;
68 5 ▶ my $type = $info->{data_type} // 'TEXT';
Condition50%1/2
$info->{"data_type"} // "TEXT"
l!l
50
MC/DC0%0/1
$info->{"data_type"} // "TEXT"
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
69 5 my $sql = sprintf 'ALTER TABLE %s ADD COLUMN %s %s',
70 _quote_ident($self->table_name),
71 _quote_ident($self->column_name),
72 $type;
73 5 ▶ $sql .= ' NOT NULL' if $info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"not_null"} 1 4 5
74 5 ▶ if (defined $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"}) 1 4 5
75 1 $sql .= " DEFAULT $info->{default_value}";
76 }
77 5 return "$sql;";
78 }
79 4 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 1 3 4
80 1 return sprintf 'ALTER TABLE %s DROP COLUMN %s;',
81 _quote_ident($self->table_name),
82 _quote_ident($self->column_name);
83 }
84 3 ▶ if ($self->action eq 'alter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "alter") 3 0 3
85 return sprintf
86 "-- ALTER COLUMN not supported by SQLite ALTER TABLE; rebuild required for %s.%s\n"
87 . "-- old: %s%s\n-- new: %s%s",
88 $self->table_name, $self->column_name,
89 ($self->old_info->{data_type} // ''),
90 ($self->old_info->{not_null} ? ' NOT NULL' : ''),
91 ($self->new_info->{data_type} // ''),
92 3 ▶ ($self->new_info->{not_null} ? ' NOT NULL' : '');
Branch: 2 branches
BranchTrueFalseTotal
$self->old_info->{'not_null'} ? : 0 3 3
$self->new_info->{'not_null'} ? : 3 0 3
Condition50%2/4
$self->old_info->{"data_type"} // ""
l!l
30
$self->new_info->{"data_type"} // ""
l!l
30
MC/DC0%0/1
$self->old_info->{"data_type"} // ""
$self->old_info->{"data_type"}
MC/DC0%0/1
$self->new_info->{"data_type"} // ""
$self->new_info->{"data_type"}
Truth table50%1/2
A
A= $self->old_info->{"data_type"}
Aresult
11
00
Truth table50%1/2
A
A= $self->new_info->{"data_type"}
Aresult
11
00
93 }
94 }
95  
96  
97 1 ▶sub summary {
Subroutine summary: called CC=4 SCAR=16.1
Pod documented
98 1 my ($self) = @_;
99 1 ▶ my $prefix = $self->action eq 'add' ? '+' : $self->action eq 'drop' ? '-' : '~';
Branch: 2 branches
BranchTrueFalseTotal
$self->action eq 'add' ? : 1 0 1
$self->action eq 'drop' ? : 0 0 0
100 1 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 1 0 1
101 1 return sprintf ' %scolumn: %s.%s%s', $prefix, $self->table_name, $self->column_name, $type;
102 }
103  
104 198 1;
105  
106 __END__
107  
108 =pod
109  
110 =encoding UTF-8
111  
112 =head1 NAME
113  
114 DBIO::SQLite::Diff::Column - Diff operations for SQLite columns
115  
116 =head1 VERSION
117  
118 version 0.900001
119  
120 =head1 DESCRIPTION
121  
122 Represents a column-level diff operation in SQLite. Only ADD COLUMN is
123 supported as a true ALTER -- SQLite has very limited C<ALTER TABLE>:
124 since 3.25 it can rename columns and since 3.35 it can drop columns,
125 but type changes still require the create-new-table-and-copy dance.
126  
127 For now this module emits:
128  
129 =over 4
130  
131 =item * C<ALTER TABLE ... ADD COLUMN ...> for added columns
132  
133 =item * C<ALTER TABLE ... DROP COLUMN ...> for dropped columns (3.35+)
134  
135 =item * A descriptive comment for type / nullability changes -- a true
136 in-place ALTER is impossible. When the target table's original
137 C<CREATE> statement is known, L<DBIO::SQLite::Diff> hoists such a
138 column into a whole-table L<DBIO::SQLite::Diff::Rebuild> instead, and
139 this comment is never emitted; it only stands as a fallback when no
140 captured DDL is available (the compiled-model path).
141  
142 =back
143  
144 Brand-new tables get their columns inline via L<DBIO::SQLite::Diff::Table>
145 -- this module only sees columns of tables that exist in both source
146 and target.
147  
148 =head1 METHODS
149  
150 =head2 diff
151  
152 my @ops = DBIO::SQLite::Diff::Column->diff(
153 $source_columns, $target_columns,
154 $source_tables, $target_tables,
155 );
156  
157 Compares column lists for tables that exist in both source and target.
158  
159 =head2 as_sql
160  
161 Returns the C<ALTER TABLE> statement for this operation, or a comment
162 for unsupported alterations.
163  
164 =head2 summary
165  
166 =head1 AUTHOR
167  
168 DBIO & DBIx::Class Authors
169  
170 =head1 COPYRIGHT AND LICENSE
171  
172 Copyright (C) 2026 DBIO Authors
173 Portions Copyright (C) 2005-2025 DBIx::Class Authors
174 Based on DBIx::Class, heavily modified.
175  
176 This is free software; you can redistribute it and/or modify it under
177 the same terms as the Perl 5 programming language system itself.
178  
179 =cut
« SQLite/Introspect/Indexes.pm ↑ Summary SQLite/Introspect/Columns.pm »