MySQL/Diff/Column.pm

filter: Statementstmt 90.0% 54 / 60 Branchbran 50.0% 12 / 24 Conditioncond 46.1% 6 / 13 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 91.6% 11 / 12 Podpod 100% 3 / 3 totaltotal 73.7% 87 / 118 CC 18 SCAR 22.9
CC
18
Cov
74%
summary
20.0
as_sql
15.5
_mysql_column_changed
3.0
CRAP 9.9
« MySQL/Diff/Table.pm ↑ Summary MySQL/SQLMaker.pm »
1 package DBIO::MySQL::Diff::Column;
2 # ABSTRACT: Diff operations for MySQL/MariaDB 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::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 __PACKAGE__->mk_diff_accessors(qw(
12 table_name column_name old_info new_info
13 ));
14  
15  
16  
17 7 ▶sub diff {
Subroutine diff: called CC=1 SCAR=0.0
Pod documented
18 7 my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_;
19  
20 return $class->diff_nested(
21 $source_cols, $target_cols,
22 index_by => 'column_name',
23 source_tables => $source_tables,
24 target_tables => $target_tables,
25 changed_when => \&_mysql_column_changed,
26 1 ▶ on_new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 my ($table_name, $column_name, $new_info) = @_;
28 1 $class->new(action => 'add', table_name => $table_name,
29 column_name => $column_name, new_info => $new_info);
30 },
31 1 ▶ on_changed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 my ($table_name, $column_name, $old_info, $new_info) = @_;
33 1 $class->new(action => 'modify', table_name => $table_name,
34 column_name => $column_name, old_info => $old_info, new_info => $new_info);
35 },
36 1 ▶ on_gone => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 my ($table_name, $column_name, $old_info) = @_;
38 1 $class->new(action => 'drop', table_name => $table_name,
39 column_name => $column_name, old_info => $old_info);
40 },
41 7 );
42 }
43  
44 # MySQL-specific field spec. Compares type/character-set/precision as
45 # scalar strings, not_null/default_value/comment as the canonical model
46 # does. desired_state => 1 means "if the target left it undef, treat as
47 # don't-care" (we do not know what the live DB will assign for charset
48 # on a portable text column, see Diff.pm ESCALATION NOTE).
49 5 ▶sub _mysql_column_changed {
Subroutine _mysql_column_changed: called CC=3 SCAR=11.0
50 5 my ($old, $new) = @_;
51 5 my @changed = changed_fields($old, $new,
52 type => ['data_type'],
53 scalar => [qw(default_value character_set collation comment)],
54 bool => ['not_null'],
55 dim => [qw(numeric_precision numeric_scale datetime_precision)],
56 array => ['values'],
57 desired_state => 1,
58 );
59  
60 # column_type carries the parameterised type (char(32), decimal(10,2)) and is
61 # the real width/precision discriminator, so it is compared here rather than as
62 # a plain scalar: MySQL 8.0.17+ drops the *display width* from integer types
63 # (bigint(20) -> bigint) while MariaDB and MySQL < 8.0.17 keep it. Normalising
64 # the integer display width away on BOTH sides keeps that server-version
65 # difference from surfacing as a phantom MODIFY. Desired-state contract: only
66 # compare when the target ($new) prescribes a column_type.
67 5 ▶ if (defined $new->{column_type}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new->{"column_type"} and _norm_int_display_width($old->{"column_type"}) ne _norm_int_display_width($new->{"column_type"})) 1 4 5
Condition66%2/3
defined $new->{"column_type"} and _norm_int_display_width($old->{"column_type"}) ne _norm_int_display_width($new->{"column_type"})
!ll&&!rl&&r
041
MC/DC50%1/2
defined $new->{"column_type"} and _norm_int_display_width($old->{"column_type"}) ne _norm_int_display_width($new->{"column_type"})
defined $new->{"column_type"} _norm_int_display_width($old->{"column_type"}) ne _norm_int_display_width($new->{"column_type"})
Truth table66%2/3
A and B
A= defined $new->{"column_type"}B= _norm_int_display_width($old->{"column_type"}) ne _norm_int_display_width($new->{"column_type"})
ABresult
0X0
100
111
68 && _norm_int_display_width($old->{column_type})
69 ne _norm_int_display_width($new->{column_type})) {
70 1 push @changed, 'column_type';
71 }
72  
73 5 return scalar @changed;
74 }
75  
76 # Strip the parenthesised display width from integer-family column types
77 # (int / integer / tinyint / smallint / mediumint / bigint), e.g.
78 # 'bigint(20)' -> 'bigint', 'int(10) unsigned' -> 'int unsigned'. tinyint(1) is
79 # preserved because MySQL keeps that width to signal BOOLEAN. decimal / numeric
80 # / char / varchar widths are NOT stripped -- there the parenthesised value is
81 # semantic, not a display width.
82 10 ▶sub _norm_int_display_width {
Subroutine _norm_int_display_width: called CC=2 SCAR=7.0
83 10 my ($ct) = @_;
84 10 ▶ return '' unless defined $ct;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ct 0 10 10
85 10 my $s = lc $ct;
86 10 $s =~ s/\s+/ /g;
87 10 $s =~ s/^\s+|\s+$//g;
88 10 $s =~ s/\btinyint\((?!1\))\d+\)/tinyint/g;
89 10 $s =~ s/\b(bigint|smallint|mediumint|integer|int)\(\d+\)/$1/g;
90 10 return $s;
91 }
92  
93  
94 3 ▶sub as_sql {
Subroutine as_sql: called CC=12 SCAR=27.4
Pod documented
95 3 my ($self) = @_;
96 3 my $tbl = $self->table_name;
97 3 my $col = $self->column_name;
98  
99 3 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 1 2 3
100 1 my $info = $self->new_info;
101 1 ▶ my $type = $info->{column_type} || $info->{data_type} || 'text';
Condition40%2/5
$info->{'column_type'} || $info->{'data_type'} || 'text'
l!l
10
$info->{"column_type"} or $info->{"data_type"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$info->{'column_type'} || $info->{'data_type'} || 'text'
$info->{"column_type"} $info->{"data_type"}
Truth table33%1/3
A or B
A= $info->{"column_type"}B= $info->{"data_type"}
ABresult
1X1
011
000
102 1 my $sql = sprintf 'ALTER TABLE `%s` ADD COLUMN `%s` %s', $tbl, $col, $type;
103 1 ▶ $sql .= ' NOT NULL' if $info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"not_null"} 1 0 1
104 1 ▶ if (defined $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"}) 0 1 1
105 0 $sql .= " DEFAULT '$info->{default_value}'";
106 }
107 1 return "$sql;";
108 }
109 2 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 1 1 2
110 1 return sprintf 'ALTER TABLE `%s` DROP COLUMN `%s`;', $tbl, $col;
111 }
112 1 ▶ if ($self->action eq 'modify') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "modify") 1 0 1
113 1 my $info = $self->new_info;
114 1 ▶ my $type = $info->{column_type} || $info->{data_type} || 'text';
Condition40%2/5
$info->{'column_type'} || $info->{'data_type'} || 'text'
l!l
10
$info->{"column_type"} or $info->{"data_type"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$info->{'column_type'} || $info->{'data_type'} || 'text'
$info->{"column_type"} $info->{"data_type"}
Truth table33%1/3
A or B
A= $info->{"column_type"}B= $info->{"data_type"}
ABresult
1X1
011
000
115 1 my $sql = sprintf 'ALTER TABLE `%s` MODIFY COLUMN `%s` %s', $tbl, $col, $type;
116 1 ▶ $sql .= ' NOT NULL' if $info->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"not_null"} 0 1 1
117 1 ▶ if (defined $info->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"default_value"}) 0 1 1
118 0 $sql .= " DEFAULT '$info->{default_value}'";
119 }
120 1 return "$sql;";
121 }
122 }
123  
124  
125 0 ▶sub summary {
Subroutine summary: not called CC=4 SCAR=30.0
Pod documented
126 0 my ($self) = @_;
127 0 ▶ my $prefix = $self->action eq 'add' ? '+'
Branch: 2 branches
BranchTrueFalseTotal
$self->action eq 'add' ? : 0 0 0
$self->action eq 'drop' ? : 0 0 0
128 : $self->action eq 'drop' ? '-' : '~';
129 0 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 0 0 0
130 0 return sprintf ' %scolumn: %s.%s%s',
131 $prefix, $self->table_name, $self->column_name, $type;
132 }
133  
134 4 1;
135  
136 __END__
137  
138 =pod
139  
140 =encoding UTF-8
141  
142 =head1 NAME
143  
144 DBIO::MySQL::Diff::Column - Diff operations for MySQL/MariaDB columns
145  
146 =head1 VERSION
147  
148 version 0.900001
149  
150 =head1 DESCRIPTION
151  
152 Represents a column-level diff operation in MySQL/MariaDB. Unlike
153 SQLite, MySQL has full C<ALTER TABLE> support so all of C<ADD COLUMN>,
154 C<DROP COLUMN>, and C<MODIFY COLUMN> are emitted directly.
155  
156 Brand-new tables get their columns inline via L<DBIO::MySQL::Diff::Table>
157 -- this module only sees columns of tables that exist in both source
158 and target.
159  
160 =head1 METHODS
161  
162 =head2 diff
163  
164 my @ops = DBIO::MySQL::Diff::Column->diff(
165 $source_cols, $target_cols,
166 $source_tables, $target_tables,
167 );
168  
169 Compares column lists for tables that exist in both source and target.
170 Detects added columns, dropped columns, and modified columns (data type,
171 C<NOT NULL>, or default value changes). Returns a list of
172 C<DBIO::MySQL::Diff::Column> objects.
173  
174 The MySQL column shape carries extras beyond the canonical model
175 (C<column_type>, C<character_set>, C<collation>, C<numeric_precision>,
176 C<numeric_scale>, C<datetime_precision>, C<values>, C<comment>) so we
177 use L<DBIO::Diff::Compare/changed_fields> with a MySQL-specific field
178 spec instead of the canonical L<changed_column_fields|/changed_column_fields>.
179  
180 =head2 as_sql
181  
182 =head2 summary
183  
184 =head1 AUTHOR
185  
186 DBIO & DBIx::Class Authors
187  
188 =head1 COPYRIGHT AND LICENSE
189  
190 Copyright (C) 2026 DBIO Authors
191 Portions Copyright (C) 2005-2025 DBIx::Class Authors
192 Based on DBIx::Class, heavily modified.
193  
194 This is free software; you can redistribute it and/or modify it under
195 the same terms as the Perl 5 programming language system itself.
196  
197 =cut
« MySQL/Diff/Table.pm ↑ Summary MySQL/SQLMaker.pm »