Firebird/Diff/Column.pm

filter: Statementstmt 93.4% 158 / 169 Branchbran 82.3% 84 / 102 Conditioncond 60.6% 80 / 132 MC/DCmcdc 38.8% 26 / 67 Subroutinesub 88.2% 15 / 17 Podpod 85.7% 6 / 7 totaltotal 74.7% 369 / 494 CC 109 SCAR 38.8
CC
109
Cov
80%
lossy
240.0
refusal
40.7
diff
19.1
CRAP 48.4
« Firebird/DDL.pm ↑ Summary Firebird/Introspect/Columns.pm »
1 package DBIO::Firebird::Diff::Column;
2 # ABSTRACT: Diff operations for Firebird columns
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '0.900007';
8  
9 13 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use DBIO::Firebird::DDL qw(_firebird_quote_ident _firebird_column_clauses);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use DBIO::Firebird::Type qw(render_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use DBIO::Diff::Compare qw(changed_fields norm_type column_change_lossy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # new() and the action accessor come from DBIO::Diff::Op.
18 13 __PACKAGE__->mk_diff_accessors(qw/table_name column_name old_info new_info constraints/);
19  
20  
21 154 ▶sub diff {
Subroutine diff: called CC=19 SCAR=29.5
Pod documented
22 154 my ($class, $source_cols, $target_cols, $source_tables, $target_tables,
23 $source_uniques, $source_fks) = @_;
24 154 my @ops;
25  
26 154 for my $table_name (sort keys %$target_cols) {
27 next unless exists $source_tables->{$table_name}
28 177 ▶ && exists $target_tables->{$table_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source_tables->{$table_name} and exists $target_tables->{$table_name} 27 150 177
Condition66%2/3
exists $source_tables->{$table_name} and exists $target_tables->{$table_name}
!ll&&!rl&&r
270150
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
29 # A view's columns are not a table's: no ALTER TABLE on a view. A view that
30 # differs is Diff::Table's refusal (karr k33).
31 next if ($source_tables->{$table_name}{kind} // '') eq 'view'
32 150 ▶ || ($target_tables->{$table_name}{kind} // '') eq 'view';
Branch: 1 branch
BranchTrueFalseTotal
if ($source_tables->{$table_name}{"kind"} // "") eq "view" or ($target_tables->{$table_name}{"kind"} // "") eq "view" 5 145 150
Condition100%7/7
($source_tables->{$table_name}{"kind"} // "") eq "view" or ($target_tables->{$table_name}{"kind"} // "") eq "view"
l!l&&r!l&&!r
41145
$source_tables->{$table_name}{"kind"} // ""
l!l
14010
$target_tables->{$table_name}{"kind"} // ""
l!l
13610
MC/DC50%1/2
($source_tables->{$table_name}{"kind"} // "") eq "view" or ($target_tables->{$table_name}{"kind"} // "") eq "view"
$source_tables->{$table_name}{"kind"} $target_tables->{$table_name}{"kind"}
Truth table66%2/3
A or B
A= $source_tables->{$table_name}{"kind"}B= $target_tables->{$table_name}{"kind"}
ABresult
1X1
011
000
33  
34 145 ▶ my %src_by_name = map { $_->{column_name} => $_ } @{ $source_cols->{$table_name} // [] };
Condition50%1/2
$source_cols->{$table_name} // []
l!l
1450
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
35 145 ▶ my %tgt_by_name = map { $_->{column_name} => $_ } @{ $target_cols->{$table_name} // [] };
Condition50%1/2
$target_cols->{$table_name} // []
l!l
1450
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
36  
37 145 for my $col_name (sort keys %tgt_by_name) {
38 268 my $tgt = $tgt_by_name{$col_name};
39  
40 268 ▶ if (!exists $src_by_name{$col_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $src_by_name{$col_name}) 18 250 268
41 18 push @ops, $class->new(
42 action => 'add',
43 table_name => $table_name,
44 column_name => $col_name,
45 new_info => $tgt,
46 );
47 18 next;
48 }
49  
50 250 my $src = $src_by_name{$col_name};
51 # Target is the desired state: fields absent from it are left as-is
52 # (desired_state => 1). Firebird size may be a scalar length or a
53 # [precision, scale] pair -- the `dim` field handles both.
54 250 my $changed = changed_fields($src, $tgt,
55 type => ['data_type'],
56 scalar => ['default_value'],
57 bool => ['not_null', 'is_auto_increment'],
58 dim => ['size'],
59 desired_state => 1,
60 );
61  
62 # The target is an introspected model, which carries default_value for
63 # every column, undef where there is none: there undef is "no default",
64 # not "no opinion" as changed_fields takes it. A model without the key
65 # (hand made) still says nothing (karr k60).
66 $changed ||= exists $tgt->{default_value}
67 && !defined $tgt->{default_value}
68 && defined $src->{default_value}
69 250 ▶ && length $src->{default_value};
Condition91%11/12
$changed ||= exists $tgt->{"default_value"} && !defined($tgt->{"default_value"}) && defined $src->{"default_value"} && length $src->{"default_value"}
l!l&&r!l&&!r
774169
exists $tgt->{'default_value'} && !defined($tgt->{'default_value'}) && defined $src->{'default_value'} && length $src->{'default_value'}
!ll&&!rl&&r
16904
exists $tgt->{"default_value"} and not defined $tgt->{"default_value"} and defined $src->{"default_value"}
!ll&&!rl&&r
91784
exists $tgt->{"default_value"} and not defined $tgt->{"default_value"}
!ll&&!rl&&r
90182
MC/DC0%0/5
$changed ||= exists $tgt->{"default_value"} && !defined($tgt->{"default_value"}) && defined $src->{"default_value"} && length $src->{"default_value"}
$changed exists $tgt->{"default_value"} not defined $tgt->{"default_value"} defined $src->{"default_value"} length $src->{'default_value'}
Truth table0%0/6
A ||= B and not(C) and D && E
A= $changedB= exists $tgt->{"default_value"}C= not defined $tgt->{"default_value"}D= defined $src->{"default_value"}E= length $src->{'default_value'}
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
70  
71 250 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 81 169 250
72 81 push @ops, $class->new(
73 action => 'alter',
74 table_name => $table_name,
75 column_name => $col_name,
76 old_info => $src,
77 new_info => $tgt,
78 constraints => _constraints_of($table_name, $col_name, $src,
79 $source_uniques, $source_fks),
80 );
81 }
82 }
83  
84 145 for my $col_name (sort keys %src_by_name) {
85 263 ▶ next if exists $tgt_by_name{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tgt_by_name{$col_name} 250 13 263
86 push @ops, $class->new(
87 action => 'drop',
88 table_name => $table_name,
89 column_name => $col_name,
90 13 old_info => $src_by_name{$col_name},
91 );
92 }
93 }
94  
95 154 return @ops;
96 }
97  
98 # The constraints of the source model that use a column, as the words the error
99 # message puts them in. The primary key is the column's own is_pk.
100 81 ▶sub _constraints_of {
Subroutine _constraints_of: called CC=11 SCAR=24.4
101 81 my ($table, $column, $info, $uniques, $fks) = @_;
102 81 my @found;
103  
104 81 ▶ push @found, 'PRIMARY KEY' if $info->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"is_pk"} 4 77 81
105  
106 81 ▶ for my $unique (@{ $uniques->{$table} // [] }) {
Condition100%2/2
$uniques->{$table} // []
l!l
279
MC/DC100%1/1
$uniques->{$table} // []
$uniques->{$table}
Truth table100%2/2
A
A= $uniques->{$table}
Aresult
11
00
107 2 my ($name, $columns) = @$unique;
108 push @found, sprintf 'UNIQUE constraint %s', _firebird_quote_ident($name)
109 2 ▶ if grep { $_ eq $column } @{ $columns // [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $column;} @{$columns // [];} 2 0 2
Condition50%1/2
$columns // []
l!l
20
MC/DC0%0/1
$columns // []
$columns
Truth table50%1/2
A
A= $columns
Aresult
11
00
110 }
111  
112 81 ▶ for my $fk (@{ $fks->{$table} // [] }) {
Condition100%2/2
$fks->{$table} // []
l!l
180
MC/DC100%1/1
$fks->{$table} // []
$fks->{$table}
Truth table100%2/2
A
A= $fks->{$table}
Aresult
11
00
113 push @found, sprintf 'FOREIGN KEY %s', _firebird_quote_ident($fk->{constraint_name} // '')
114 1 ▶ if grep { $_ eq $column } @{ $fk->{from_columns} // [] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $column;} @{$fk->{"from_columns"} // [];} 1 0 1
Condition50%2/4
$fk->{"from_columns"} // []
l!l
10
$fk->{"constraint_name"} // ""
l!l
10
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC0%0/1
$fk->{"constraint_name"} // ""
$fk->{"constraint_name"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
Truth table50%1/2
A
A= $fk->{"constraint_name"}
Aresult
11
00
115 }
116  
117 81 return \@found;
118 }
119  
120 # What the server stores a type in, for the rules below. Undef for a type (or a
121 # size) this module has no opinion on.
122 13 my %STORAGE_OF = (
123 smallint => 'SMALLINT', integer => 'INTEGER', bigint => 'BIGINT', int128 => 'INT128',
124 float => 'FLOAT', 'double precision' => 'DOUBLE',
125 date => 'DATE', time => 'TIME', timestamp => 'TIMESTAMP', boolean => 'BOOLEAN',
126 blob => 'BLOB', 'blob sub_type text' => 'BLOB',
127 char => 'CHAR', varchar => 'CHAR', binary => 'CHAR', varbinary => 'CHAR',
128 );
129  
130 # The storage a type is converted within. Measured on Firebird 5.0.4: the
131 # server converts to the same or a wider one, per this table, and to no other.
132 13 my %CONVERTS_TO = (
133 SMALLINT => [qw/SMALLINT INTEGER BIGINT INT128 FLOAT DOUBLE CHAR/],
134 INTEGER => [qw/INTEGER BIGINT INT128 DOUBLE CHAR/],
135 BIGINT => [qw/BIGINT INT128 CHAR/],
136 INT128 => [qw/INT128 CHAR/],
137 FLOAT => [qw/FLOAT DOUBLE CHAR/],
138 DOUBLE => [qw/DOUBLE CHAR/],
139 DATE => [qw/DATE TIMESTAMP CHAR/],
140 TIME => [qw/TIME CHAR/],
141 TIMESTAMP => [qw/TIMESTAMP DATE TIME CHAR/],
142 BOOLEAN => [qw/BOOLEAN/],
143 CHAR => [qw/CHAR/],
144 BLOB => [],
145 );
146  
147 # The characters the text of each type needs when it goes into a CHAR/VARCHAR.
148 13 my %TEXT_LENGTH = (
149 SMALLINT => 6, INTEGER => 11, BIGINT => 20, INT128 => 47,
150 FLOAT => 15, DOUBLE => 24, DATE => 10, TIME => 13, TIMESTAMP => 25,
151 );
152  
153 # ( storage, [precision, scale] or undef ) of a model column; empty for a type
154 # this module has no opinion on.
155 370 ▶sub _storage {
Subroutine _storage: called CC=11 SCAR=25.0
156 370 my ($info) = @_;
157 370 my $type = lc norm_type($info->{data_type});
158 370 my $size = $info->{size};
159  
160 370 ▶ if ($type eq 'numeric' || $type eq 'decimal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "numeric" or $type eq "decimal") 70 300 370
Condition100%3/3
$type eq "numeric" or $type eq "decimal"
l!l&&r!l&&!r
1654300
MC/DC100%2/2
$type eq "numeric" or $type eq "decimal"
$type eq "numeric" $type eq "decimal"
Truth table100%3/3
A or B
A= $type eq "numeric"B= $type eq "decimal"
ABresult
1X1
011
000
161 70 ▶ return unless ref $size eq 'ARRAY' && defined $size->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $size eq "ARRAY" and defined $size->[0] 0 70 70
Condition33%1/3
ref $size eq "ARRAY" and defined $size->[0]
!ll&&!rl&&r
0070
MC/DC0%0/2
ref $size eq "ARRAY" and defined $size->[0]
ref $size eq "ARRAY" defined $size->[0]
Truth table33%1/3
A and B
A= ref $size eq "ARRAY"B= defined $size->[0]
ABresult
0X0
100
111
162 70 ▶ my ($p, $s) = ($size->[0], $size->[1] // 0);
Condition50%1/2
$size->[1] // 0
l!l
700
MC/DC0%0/1
$size->[1] // 0
$size->[1]
Truth table50%1/2
A
A= $size->[1]
Aresult
11
00
163 # NUMERIC(1..4) is a SMALLINT; DECIMAL(1..9) is at least an INTEGER.
164 70 ▶ my $storage = $p <= 4 && $type eq 'numeric' ? 'SMALLINT'
Branch: 3 branches
BranchTrueFalseTotal
$p <= 4 && $type eq 'numeric' ? : 2 68 70
$p <= 9 ? : 32 36 68
$p <= 18 ? : 36 0 36
Condition66%2/3
$p <= 4 and $type eq "numeric"
!ll&&!rl&&r
6802
MC/DC50%1/2
$p <= 4 and $type eq "numeric"
$p <= 4 $type eq "numeric"
Truth table66%2/3
A and B
A= $p <= 4B= $type eq "numeric"
ABresult
0X0
100
111
165 : $p <= 9 ? 'INTEGER'
166 : $p <= 18 ? 'BIGINT'
167 : 'INT128';
168 70 return ($storage, [ $p, $s ]);
169 }
170  
171 300 ▶ my $storage = $STORAGE_OF{$type} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $storage = $STORAGE_OF{$type} or return 300 0 300
172 300 return ($storage, undef);
173 }
174  
175 # A type the way an error message names it: "varchar(150)", "decimal(10,2)".
176 170 ▶sub _type_text {
Subroutine _type_text: called CC=3 SCAR=11.1
177 170 my ($info) = @_;
178 return lc(norm_type($info->{data_type}) || 'varchar') . render_size($info->{size})
179 170 ▶ . ($info->{is_auto_increment} ? ' identity' : '');
Branch: 1 branch
BranchTrueFalseTotal
$info->{'is_auto_increment'} ? : 3 167 170
Condition50%1/2
norm_type($info->{'data_type'}) || 'varchar'
l!l
1700
MC/DC0%0/1
norm_type($info->{'data_type'}) || 'varchar'
norm_type($info->{'data_type'})
Truth table50%1/2
A
A= norm_type($info->{'data_type'})
Aresult
11
00
180 }
181  
182  
183 256 ▶sub refusal {
Subroutine refusal: called CC=37 SCAR=37.1
Pod documented
184 256 my ($self) = @_;
185 256 ▶ return unless ($self->action // '') eq 'alter';
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->action // "") eq "alter" 29 227 256
Condition50%1/2
$self->action // ""
l!l
2560
MC/DC0%0/1
$self->action // ""
$self->action
Truth table50%1/2
A
A= $self->action
Aresult
11
00
186  
187 227 my ($old, $new) = ($self->old_info, $self->new_info);
188  
189 # IDENTITY is not something an existing column gains, loses or changes by an
190 # ALTER here (karr k46): nothing in the Firebird column syntax this module
191 # writes does it, and ALTER ... ADD of an identity column is refused by the
192 # server on a table that holds rows. Refused before any statement runs.
193 227 ▶ if (($old->{is_auto_increment} // 0) != ($new->{is_auto_increment} // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($old->{"is_auto_increment"} // 0) != ($new->{"is_auto_increment"} // 0)) 6 221 227
Condition100%4/4
$old->{"is_auto_increment"} // 0
l!l
10217
$new->{"is_auto_increment"} // 0
l!l
10217
MC/DC100%1/1
$old->{"is_auto_increment"} // 0
$old->{"is_auto_increment"}
MC/DC100%1/1
$new->{"is_auto_increment"} // 0
$new->{"is_auto_increment"}
Truth table100%2/2
A
A= $old->{"is_auto_increment"}
Aresult
11
00
Truth table100%2/2
A
A= $new->{"is_auto_increment"}
Aresult
11
00
194 return $new->{is_auto_increment}
195 6 ▶ ? 'the column is not an identity column and DBIO::Firebird does not turn an existing '
Branch: 1 branch
BranchTrueFalseTotal
$new->{'is_auto_increment'} ? : 4 2 6
196 . 'column into one (no ALTER is written for it; adding an identity column to a table '
197 . 'that holds rows is refused by the server)'
198 : 'the column is an identity column and DBIO::Firebird does not remove IDENTITY from an '
199 . 'existing column (no ALTER is written for it)';
200 }
201  
202 # No type change, no refusal: nullability and default are never refused.
203 return unless norm_type($old->{data_type}) ne norm_type($new->{data_type})
204 221 ▶ || changed_fields($old, $new, dim => ['size']);
Branch: 1 branch
BranchTrueFalseTotal
unless norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"}) or changed_fields($old, $new, "dim", ["size"]) 14 207 221
Condition100%3/3
norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"}) or changed_fields($old, $new, "dim", ["size"])
l!l&&r!l&&!r
1565114
MC/DC100%2/2
norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"}) or changed_fields($old, $new, "dim", ["size"])
norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"}) changed_fields($old, $new, "dim", ["size"])
Truth table100%3/3
A or B
A= norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"})B= changed_fields($old, $new, "dim", ["size"])
ABresult
1X1
011
000
205  
206 # The constraint rule stands before the type rules: it holds for a widening.
207 207 ▶ if (my @keys = @{ $self->constraints // [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@keys) = @{$self->constraints // [];}) 22 185 207
Condition50%1/2
$self->constraints // []
l!l
2070
MC/DC0%0/1
$self->constraints // []
$self->constraints
Truth table50%1/2
A
A= $self->constraints
Aresult
11
00
208 22 return sprintf
209 'the column is used by %s; the server refuses to change the type of such a column '
210 . '("Cannot update index segment used by an Integrity Constraint"), whatever the change',
211 join ', ', @keys;
212 }
213  
214 185 ▶ my ($from, $from_dim) = _storage($old) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = _storage($old) or return 185 0 185
215 185 ▶ my ($to, $to_dim) = _storage($new) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = _storage($new) or return 185 0 185
216  
217 185 ▶ if ($from eq 'BLOB' || $to eq 'BLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq "BLOB" or $to eq "BLOB") 12 173 185
Condition100%3/3
$from eq "BLOB" or $to eq "BLOB"
l!l&&r!l&&!r
84173
MC/DC100%2/2
$from eq "BLOB" or $to eq "BLOB"
$from eq "BLOB" $to eq "BLOB"
Truth table100%3/3
A or B
A= $from eq "BLOB"B= $to eq "BLOB"
ABresult
1X1
011
000
218 12 return 'the server does not change the type of a BLOB column, nor convert anything into one';
219 }
220 173 ▶ if ($from eq 'CHAR' && $to ne 'CHAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq "CHAR" and $to ne "CHAR") 18 155 173
Condition100%3/3
$from eq "CHAR" and $to ne "CHAR"
!ll&&!rl&&r
1203518
MC/DC100%2/2
$from eq "CHAR" and $to ne "CHAR"
$from eq "CHAR" $to ne "CHAR"
Truth table100%3/3
A and B
A= $from eq "CHAR"B= $to ne "CHAR"
ABresult
0X0
100
111
221 18 return 'the server does not turn a character type into a non-character type';
222 }
223 155 ▶ if ($to eq 'BOOLEAN' && $from ne 'BOOLEAN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "BOOLEAN" and $from ne "BOOLEAN") 4 151 155
Condition66%2/3
$to eq "BOOLEAN" and $from ne "BOOLEAN"
!ll&&!rl&&r
15104
MC/DC50%1/2
$to eq "BOOLEAN" and $from ne "BOOLEAN"
$to eq "BOOLEAN" $from ne "BOOLEAN"
Truth table66%2/3
A and B
A= $to eq "BOOLEAN"B= $from ne "BOOLEAN"
ABresult
0X0
100
111
224 4 return 'the server does not convert anything into BOOLEAN';
225 }
226 151 ▶ if (!grep { $_ eq $to } @{ $CONVERTS_TO{$from} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $to;} @{$CONVERTS_TO{$from};}) 42 109 151
227 42 return "the server has no conversion from base type $from to $to";
228 }
229  
230 109 ▶ if ($from eq 'CHAR' && $to eq 'CHAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq "CHAR" and $to eq "CHAR") 35 74 109
Condition66%2/3
$from eq "CHAR" and $to eq "CHAR"
!ll&&!rl&&r
74035
MC/DC50%1/2
$from eq "CHAR" and $to eq "CHAR"
$from eq "CHAR" $to eq "CHAR"
Truth table66%2/3
A and B
A= $from eq "CHAR"B= $to eq "CHAR"
ABresult
0X0
100
111
231 35 my ($was, $is) = ($old->{size}, $new->{size});
232 35 ▶ if (defined $was && defined $is && !ref $was && !ref $is && $is < $was) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $was and defined $is and not ref $was and not ref $is and $is < $was) 22 13 35
Condition41%5/12
defined $was and defined $is and not ref $was and not ref $is and $is < $was
!ll&&!rl&&r
01322
defined $was and defined $is and not ref $was and not ref $is
!ll&&!rl&&r
0035
defined $was and defined $is and not ref $was
!ll&&!rl&&r
0035
defined $was and defined $is
!ll&&!rl&&r
0035
MC/DC20%1/5
defined $was and defined $is and not ref $was and not ref $is and $is < $was
defined $was defined $is not ref $was not ref $is $is < $was
Truth table33%2/6
A and B and not(C) and not(D) and E
A= defined $wasB= defined $isC= not ref $wasD= not ref $isE= $is < $was
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
233 22 return "the server never narrows a character column ($was characters to $is)";
234 }
235 13 return;
236 }
237  
238 74 ▶ if ($from_dim && $to_dim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_dim and $to_dim) 19 55 74
Condition100%3/3
$from_dim and $to_dim
!ll&&!rl&&r
411419
MC/DC100%2/2
$from_dim and $to_dim
$from_dim $to_dim
Truth table100%3/3
A and B
A= $from_dimB= $to_dim
ABresult
0X0
100
111
239 19 my $was = $from_dim->[0] - $from_dim->[1];
240 19 my $is = $to_dim->[0] - $to_dim->[1];
241 19 ▶ if ($is < $was) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is < $was) 12 7 19
242 12 return "the digits before the decimal point would shrink from $was to $is "
243 . '(precision minus scale); the server refuses that';
244 }
245 }
246  
247 62 ▶ if ($to eq 'CHAR' && !ref $new->{size} && defined $new->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "CHAR" and not ref $new->{"size"} and defined $new->{"size"}) 38 24 62
Condition66%4/6
$to eq "CHAR" and not ref $new->{"size"} and defined $new->{"size"}
!ll&&!rl&&r
24038
$to eq "CHAR" and not ref $new->{"size"}
!ll&&!rl&&r
24038
MC/DC33%1/3
$to eq "CHAR" and not ref $new->{"size"} and defined $new->{"size"}
$to eq "CHAR" not ref $new->{"size"} defined $new->{"size"}
Truth table50%2/4
A and not(B) and C
A= $to eq "CHAR"B= not ref $new->{"size"}C= defined $new->{"size"}
ABCresult
0XX0
10X0
1100
1111
248 38 ▶ my $needs = $TEXT_LENGTH{$from} // return;
Condition50%1/2
$TEXT_LENGTH{$from} // (return)
l!l
380
MC/DC0%0/1
$TEXT_LENGTH{$from} // (return)
$TEXT_LENGTH{$from}
Truth table50%1/2
A
A= $TEXT_LENGTH{$from}
Aresult
11
00
249 38 ▶ $needs++ if $from_dim && $from_dim->[1] > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $from_dim and $from_dim->[1] > 0 10 28 38
Condition66%2/3
$from_dim and $from_dim->[1] > 0
!ll&&!rl&&r
28010
MC/DC50%1/2
$from_dim and $from_dim->[1] > 0
$from_dim $from_dim->[1] > 0
Truth table66%2/3
A and B
A= $from_dimB= $from_dim->[1] > 0
ABresult
0X0
100
111
250 38 ▶ if ($new->{size} < $needs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->{"size"} < $needs) 32 6 38
251 32 return "a $from needs at least $needs characters as text, the new column has $new->{size}";
252 }
253 }
254  
255 30 return;
256 }
257  
258 # The text of the error for a list of refused column operations.
259 82 ▶sub refusal_error {
Subroutine refusal_error: called CC=2 SCAR=6.9
Pod undocumented
260 82 my ($class, @ops) = @_;
261  
262 my @found = map {
263 82 sprintf ' %s.%s: %s -> %s%s',
264 _firebird_quote_ident($_->table_name), _firebird_quote_ident($_->column_name),
265 _type_text($_->old_info), _type_text($_->new_info),
266 "\n " . $_->refusal;
267 } @ops;
268  
269 82 ▶ return join "\n",
Branch: 1 branch
BranchTrueFalseTotal
@ops == 1 ? : 80 2 82
270 sprintf(
271 'refusing to change %d column%s: the Firebird server refuses %s',
272 scalar @ops, (@ops == 1 ? ('', 'this change') : ('s', 'these changes')),
273 ),
274 '',
275 @found,
276 '',
277 'The server refuses these whatever the table holds, so the statement would',
278 'fail on it with the server\'s own error and leave an upgrade half applied.',
279 'No statement was generated. The difference is still reported by diff,',
280 'has_changes and summary. DBIO::Firebird does not rebuild the column',
281 '(new column, copy, drop, rename); that is yours to do, or to give up.',
282 # throw() takes one newline off and appends where it was called from.
283 '', '';
284 }
285  
286  
287 87 ▶sub as_sql {
Subroutine as_sql: called CC=17 SCAR=28.4
Pod documented
288 87 my ($self) = @_;
289  
290 87 ▶ if ($self->refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->refusal) 38 49 87
291 38 DBIO::Exception->throw(ref($self)->refusal_error($self));
292 }
293  
294 49 my $tbl = _firebird_quote_ident($self->table_name);
295 49 my $col = _firebird_quote_ident($self->column_name);
296  
297 49 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 12 37 49
298 12 my $info = $self->new_info;
299 12 ▶ my $type = $info->{data_type} || 'VARCHAR';
Condition50%1/2
$info->{'data_type'} || 'VARCHAR'
l!l
120
MC/DC0%0/1
$info->{'data_type'} || 'VARCHAR'
$info->{'data_type'}
Truth table50%1/2
A
A= $info->{'data_type'}
Aresult
11
00
300 my $sql = sprintf 'ALTER TABLE %s ADD %s %s%s', $tbl, $col, $type,
301 12 render_size($info->{size});
302 $sql .= _firebird_column_clauses(
303 identity => $info->{is_auto_increment},
304 default => $info->{default_value},
305 not_null => $info->{not_null},
306 12 );
307 12 return "$sql;";
308 }
309  
310 37 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 6 31 37
311 6 return sprintf 'ALTER TABLE %s DROP %s;', $tbl, $col;
312 }
313  
314 31 ▶ if ($self->action eq 'alter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "alter") 31 0 31
315 31 my $old = $self->old_info;
316 31 my $new = $self->new_info;
317 31 my @stmts;
318  
319 # The size is part of the type: a changed size alone is a TYPE change too.
320 # Compared the way diff() compares it, so a size the target does not
321 # prescribe is not a change here either.
322 31 ▶ 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"}) or changed_fields($old, $new, "dim", ["size"])) 23 8 31
Condition100%3/3
norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"}) or changed_fields($old, $new, "dim", ["size"])
l!l&&r!l&&!r
1498
MC/DC100%2/2
norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"}) or changed_fields($old, $new, "dim", ["size"])
norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"}) changed_fields($old, $new, "dim", ["size"])
Truth table100%3/3
A or B
A= norm_type($old->{"data_type"}) ne norm_type($new->{"data_type"})B= changed_fields($old, $new, "dim", ["size"])
ABresult
1X1
011
000
323 || changed_fields($old, $new, dim => ['size'])) {
324 push @stmts, sprintf 'ALTER TABLE %s ALTER %s TYPE %s%s;',
325 23 ▶ $tbl, $col, ($new->{data_type} || 'VARCHAR'), render_size($new->{size});
Condition50%1/2
$new->{'data_type'} || 'VARCHAR'
l!l
230
MC/DC0%0/1
$new->{'data_type'} || 'VARCHAR'
$new->{'data_type'}
Truth table50%1/2
A
A= $new->{'data_type'}
Aresult
11
00
326 }
327 31 ▶ if (($old->{not_null} // 0) != ($new->{not_null} // 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (($old->{"not_null"} // 0) != ($new->{"not_null"} // 0)) 4 27 31
Condition100%4/4
$old->{"not_null"} // 0
l!l
274
$new->{"not_null"} // 0
l!l
274
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
328 push @stmts, $new->{not_null}
329 4 ▶ ? sprintf('ALTER TABLE %s ALTER %s SET NOT NULL;', $tbl, $col)
Branch: 1 branch
BranchTrueFalseTotal
$new->{'not_null'} ? : 3 1 4
330 : sprintf('ALTER TABLE %s ALTER %s DROP NOT NULL;', $tbl, $col);
331 }
332 31 ▶ my $old_d = defined $old->{default_value} ? $old->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $old->{'default_value'} ? : 4 27 31
333 31 ▶ my $new_d = defined $new->{default_value} ? $new->{default_value} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $new->{'default_value'} ? : 5 26 31
334 31 ▶ if ($old_d ne $new_d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_d ne $new_d) 7 24 31
335 7 ▶ push @stmts, length $new_d
Branch: 1 branch
BranchTrueFalseTotal
length $new_d ? : 5 2 7
336 ? sprintf('ALTER TABLE %s ALTER %s SET DEFAULT %s;', $tbl, $col, $new_d)
337 : sprintf('ALTER TABLE %s ALTER %s DROP DEFAULT;', $tbl, $col);
338 }
339 31 return join "\n", @stmts;
340 }
341 }
342  
343  
344 13 our %LOSSLESS_PAIRS;
345 {
346 13 my @ladder = qw(smallint integer bigint int128);
347 13 for my $from (0 .. $#ladder - 1) {
348 39 $LOSSLESS_PAIRS{"$ladder[$from]->$ladder[$_]"} = 1 for $from + 1 .. $#ladder;
349 }
350 13 $LOSSLESS_PAIRS{"$_->double precision"} = 1 for qw(smallint integer);
351 13 $LOSSLESS_PAIRS{'date->timestamp'} = 1;
352 13 $LOSSLESS_PAIRS{'char->varchar'} = 1;
353 13 $LOSSLESS_PAIRS{'binary->varbinary'} = 1;
354 }
355  
356  
357 0 ▶sub lossy_pairs { return { %LOSSLESS_PAIRS } }
Subroutine lossy_pairs: not called CC=1 SCAR=6.9
Pod documented
358  
359  
360 0 ▶sub lossy {
Subroutine lossy: not called CC=15 SCAR=54.8
Pod documented
361 0 my ($self) = @_;
362 0 ▶ return 0 unless ($self->action // '') eq 'alter';
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->action // "") eq "alter" 0 0 0
Condition0%0/2
$self->action // ""
l!l
00
MC/DC0%0/1
$self->action // ""
$self->action
Truth table0%0/2
A
A= $self->action
Aresult
11
00
363 0 ▶ my ($old, $new) = ($self->old_info // {}, $self->new_info // {});
Condition0%0/4
$self->old_info // {}
l!l
00
$self->new_info // {}
l!l
00
MC/DC0%0/1
$self->old_info // {}
$self->old_info
MC/DC0%0/1
$self->new_info // {}
$self->new_info
Truth table0%0/2
A
A= $self->old_info
Aresult
11
00
Truth table0%0/2
A
A= $self->new_info
Aresult
11
00
364  
365 0 my @pair = map { lc norm_type($_->{data_type}) } $old, $new;
366 0 ▶ $pair[1] = $pair[0] unless length $pair[1];
Branch: 1 branch
BranchTrueFalseTotal
unless length $pair[1] 0 0 0
367  
368 # A CHAR is stored padded to its length and the server pads it to the new
369 # length when it becomes a VARCHAR of another one (measured, t/99-diff-
370 # column-lossless-live.t: char(5) 'ab' reads 'ab ' as varchar(5) and
371 # 'ab ' as varchar(9)): only the same length keeps the value.
372 0 ▶ if ($pair[0] ne $pair[1] && $pair[0] =~ /\A(?:char|binary)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair[0] ne $pair[1] and $pair[0] =~ /\A(?:char|binary)\z/) 0 0 0
Condition0%0/3
$pair[0] ne $pair[1] and $pair[0] =~ /\A(?:char|binary)\z/
!ll&&!rl&&r
000
MC/DC0%0/2
$pair[0] ne $pair[1] and $pair[0] =~ /\A(?:char|binary)\z/
$pair[0] ne $pair[1] $pair[0] =~ /\A(?:char|binary)\z/
Truth table0%0/3
A and B
A= $pair[0] ne $pair[1]B= $pair[0] =~ /\A(?:char|binary)\z/
ABresult
0X0
100
111
373 0 my ($was, $is) = ($old->{size}, $new->{size});
374 0 ▶ return 1 if defined $is && (ref $is || ref($was // '') || !defined $was || $is != $was);
Branch: 1 branch
BranchTrueFalseTotal
if defined $is and ref $is || ref($was // "") || !defined($was) || $is != $was 0 0 0
Condition0%0/14
defined $is and ref $is || ref($was // "") || !defined($was) || $is != $was
!ll&&!rl&&r
000
ref $is or ref($was // "") or not defined $was or $is != $was
l!l&&r!l&&!r
000
ref $is or ref($was // "") or not defined $was
l!l&&r!l&&!r
000
ref $is or ref($was // "")
l!l&&r!l&&!r
000
$was // ""
l!l
00
MC/DC0%0/5
defined $is and ref $is || ref($was // "") || !defined($was) || $is != $was
defined $is ref $is $was not defined $was $is != $was
Truth table0%0/6
A and B or C or not(D) or E
A= defined $isB= ref $isC= $wasD= not defined $wasE= $is != $was
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
375 }
376  
377 0 ▶ return column_change_lossy($old, $new, \%LOSSLESS_PAIRS) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
column_change_lossy($old, $new, \%DBIO::Firebird::Diff::Column::LOSSLESS_PAIRS) ? : 0 0 0
378 }
379  
380  
381 49 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
382 49 my ($self) = @_;
383 49 ▶ my $type = $self->new_info ? " ($self->{new_info}{data_type})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->new_info ? : 48 1 49
384 49 return sprintf ' %scolumn: %s.%s%s',
385 $self->summary_prefix, $self->table_name, $self->column_name, $type;
386 }
387  
388 13 1;
389  
390 __END__
« Firebird/DDL.pm ↑ Summary Firebird/Introspect/Columns.pm »