Firebird/Diff/Table.pm

filter: Statementstmt 100% 98 / 98 Branchbran 86.6% 26 / 30 Conditioncond 80.0% 40 / 50 MC/DCmcdc 62.5% 15 / 24 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 89.5% 196 / 219 CC 48 SCAR 28.8
CC
48
Cov
92%
diff
30.1
refusal
8.2
as_sql
7.0
CRAP 17.8
« Firebird/Storage/InterBase.pm ↑ Summary Firebird/Introspect/Uniques.pm »
1 package DBIO::Firebird::Diff::Table;
2 # ABSTRACT: Diff operations for Firebird tables
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::Diff::Constraint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use DBIO::Firebird::DDL qw(_firebird_quote_ident _firebird_column_clauses);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use DBIO::Firebird::Type qw(render_size);
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 table_info columns foreign_keys old_info/);
19  
20  
21 152 ▶sub diff {
Subroutine diff: called CC=30 SCAR=34.1
Pod documented
22 152 my ($class, $source, $target, $target_columns, $target_fks, $source_columns, $source_fks) = @_;
23 152 ▶ $source_fks //= {};
Condition100%2/2
$source_fks //= {}
l!l
1466
MC/DC100%1/1
$source_fks //= {}
$source_fks
Truth table100%2/2
A
A= $source_fks
Aresult
11
00
24 152 ▶ $target_columns //= {};
Condition100%2/2
$target_columns //= {}
l!l
1511
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
25 152 ▶ $target_fks //= {};
Condition100%2/2
$target_fks //= {}
l!l
1511
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
Aresult
11
00
26  
27 152 my @ops;
28  
29 152 for my $name (sort keys %$target) {
30 181 ▶ next if exists $source->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $source->{$name} 149 32 181
31 32 ▶ next if ($target->{$name}{kind} // 'table') eq 'view';
Branch: 1 branch
BranchTrueFalseTotal
if ($target->{$name}{"kind"} // "table") eq "view" 7 25 32
Condition100%2/2
$target->{$name}{"kind"} // "table"
l!l
266
MC/DC100%1/1
$target->{$name}{"kind"} // "table"
$target->{$name}{"kind"}
Truth table100%2/2
A
A= $target->{$name}{"kind"}
Aresult
11
00
32 push @ops, $class->new(
33 action => 'create',
34 table_name => $name,
35 table_info => $target->{$name},
36 columns => $target_columns->{$name} // [],
37 25 ▶ foreign_keys => $target_fks->{$name} // [],
Condition75%3/4
$target_columns->{$name} // []
l!l
250
$target_fks->{$name} // []
l!l
520
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_fks->{$name} // []
$target_fks->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Aresult
11
00
Truth table100%2/2
A
A= $target_fks->{$name}
Aresult
11
00
38 );
39 }
40  
41 # DROP TABLE of a table another table's foreign key points at is refused, so
42 # the tables go child before parent (karr k34). Only keys between tables
43 # dropped here count. Where tables point at each other no order exists: the
44 # keys among the rest are dropped first (ALTER TABLE ... DROP CONSTRAINT),
45 # then the tables.
46 152 ▶ my %gone = map { $_ => 1 } grep { !exists $target->{$_} && ($source->{$_}{kind} // 'table') ne 'view' } keys %$source;
Condition100%5/5
!exists $target->{$_} && ($source->{$_}{'kind'} // 'table') ne 'view'
!ll&&!rl&&r
149424
$source->{$_}{"kind"} // "table"
l!l
262
MC/DC100%2/2
!exists $target->{$_} && ($source->{$_}{'kind'} // 'table') ne 'view'
!exists $target->{$_} $source->{$_}{"kind"}
Truth table100%3/3
not(A) && B
A= !exists $target->{$_}B= $source->{$_}{"kind"}
ABresult
0X0
100
111
47 152 my %refs; # parent => { child => [ fk, ... ] }
48 152 for my $child (keys %gone) {
49 24 ▶ for my $fk (@{ $source_fks->{$child} // [] }) {
Condition100%2/2
$source_fks->{$child} // []
l!l
618
MC/DC100%1/1
$source_fks->{$child} // []
$source_fks->{$child}
Truth table100%2/2
A
A= $source_fks->{$child}
Aresult
11
00
50 6 my $parent = $fk->{to_table};
51 6 ▶ next unless defined $parent && $gone{$parent} && $parent ne $child;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent and $gone{$parent} and $parent ne $child 1 5 6
Condition50%3/6
defined $parent and $gone{$parent} and $parent ne $child
!ll&&!rl&&r
015
defined $parent and $gone{$parent}
!ll&&!rl&&r
006
MC/DC33%1/3
defined $parent and $gone{$parent} and $parent ne $child
defined $parent $gone{$parent} $parent ne $child
Truth table50%2/4
A and B and C
A= defined $parentB= $gone{$parent}C= $parent ne $child
ABCresult
0XX0
10X0
1100
1111
52 5 push @{ $refs{$parent}{$child} }, $fk;
53 }
54 }
55 152 my %left = %gone;
56 152 ▶ while (%left) {
Branch: 1 branch
BranchTrueFalseTotal
if (%left) 22 152 174
57 # droppable now: no table still left points at it
58 22 ▶ my @now = sort grep { my $p = $_; !grep { $left{$_} } keys %{ $refs{$p} // {} } } keys %left;
Condition100%2/2
$refs{$p} // {}
l!l
1119
MC/DC100%1/1
$refs{$p} // {}
$refs{$p}
Truth table100%2/2
A
A= $refs{$p}
Aresult
11
00
59 22 ▶ unless (@now) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@now) 1 21 22
60 # a cycle: drop every key among the tables left, then they are all free
61 1 for my $parent (sort keys %left) {
62 2 ▶ for my $child (sort grep { $left{$_} } keys %{ $refs{$parent} // {} }) {
Condition50%1/2
$refs{$parent} // {}
l!l
20
MC/DC0%0/1
$refs{$parent} // {}
$refs{$parent}
Truth table50%1/2
A
A= $refs{$parent}
Aresult
11
00
63 push @ops, DBIO::Firebird::Diff::Constraint->new(
64 action => 'drop', kind => 'fk', table_name => $child,
65 constraint_name => $_->{constraint_name}, info => $_,
66 2 ) for @{ $refs{$parent}{$child} };
67 }
68 }
69 1 %refs = map { $_ => {} } keys %left;
70 1 next;
71 }
72 21 for my $name (@now) {
73 push @ops, $class->new(
74 action => 'drop',
75 table_name => $name,
76 24 table_info => $source->{$name},
77 );
78 24 delete $left{$name};
79 }
80 }
81  
82 # A name that is a table on one side and a view on the other: the data of
83 # the table or the definition of the view would be lost, so no statement is
84 # written (refusal). A view on both sides is Diff::View's.
85 152 for my $name (sort keys %$target) {
86 181 ▶ next unless exists $source->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $source->{$name} 32 149 181
87 149 ▶ my ($was, $is) = map { ($_->{$name}{kind} // 'table') } $source, $target;
Condition100%2/2
$_->{$name}{"kind"} // "table"
l!l
2926
MC/DC100%1/1
$_->{$name}{"kind"} // "table"
$_->{$name}{"kind"}
Truth table100%2/2
A
A= $_->{$name}{"kind"}
Aresult
11
00
88 149 ▶ next if $was eq $is;
Branch: 1 branch
BranchTrueFalseTotal
if $was eq $is 147 2 149
89 push @ops, $class->new(
90 action => 'change',
91 table_name => $name,
92 table_info => $target->{$name},
93 2 old_info => $source->{$name},
94 );
95 }
96  
97 152 return @ops;
98 }
99  
100  
101 61 ▶sub refusal {
Subroutine refusal: called CC=8 SCAR=21.1
Pod documented
102 61 my ($self) = @_;
103 61 ▶ my ($is, $was) = map { $_ // '' } (($self->table_info // {})->{kind}, ($self->{old_info} // {})->{kind});
Condition100%6/6
$_ // ""
l!l
5171
$self->table_info // {}
l!l
592
$self->{"old_info"} // {}
l!l
457
MC/DC100%1/1
$_ // ""
$_
MC/DC100%1/1
$self->table_info // {}
$self->table_info
MC/DC100%1/1
$self->{"old_info"} // {}
$self->{"old_info"}
Truth table100%2/2
A
A= $_
Aresult
11
00
Truth table100%2/2
A
A= $self->table_info
Aresult
11
00
Truth table100%2/2
A
A= $self->{"old_info"}
Aresult
11
00
104 61 ▶ return unless $self->action eq 'change' && $is ne $was;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->action eq "change" and $is ne $was 57 4 61
Condition66%2/3
$self->action eq "change" and $is ne $was
!ll&&!rl&&r
5704
MC/DC50%1/2
$self->action eq "change" and $is ne $was
$self->action eq "change" $is ne $was
Truth table66%2/3
A and B
A= $self->action eq "change"B= $is ne $was
ABresult
0X0
100
111
105 4 ▶ return sprintf 'the database has a %s and the schema a %s under this name', $was || 'table', $is || 'table';
Condition50%2/4
$was || 'table'
l!l
40
$is || 'table'
l!l
40
MC/DC0%0/1
$was || 'table'
$was
MC/DC0%0/1
$is || 'table'
$is
Truth table50%1/2
A
A= $was
Aresult
11
00
Truth table50%1/2
A
A= $is
Aresult
11
00
106 }
107  
108  
109 2 ▶sub refusal_error {
Subroutine refusal_error: called CC=3 SCAR=11.7
Pod documented
110 2 my ($class, @ops) = @_;
111 return join "\n",
112 sprintf('refusing to diff %d name%s that change%s between table and view',
113 scalar @ops, (@ops == 1 ? '' : 's'), (@ops == 1 ? 's' : '')),
114 '',
115 2 ▶ (map { sprintf ' %s: %s', _firebird_quote_ident($_->table_name), $_->refusal } @ops),
Branch: 2 branches
BranchTrueFalseTotal
@ops == 1 ? : 2 0 2
@ops == 1 ? : 2 0 2
116 '',
117 'Turning a table into a view (or a view into a table) would drop the one to',
118 'create the other. Drop it yourself, then run the diff again. No statement was',
119 'generated. The difference is still reported by diff, has_changes and summary.',
120 '', '';
121 }
122  
123  
124 35 ▶sub as_sql {
Subroutine as_sql: called CC=7 SCAR=19.5
Pod documented
125 35 my ($self) = @_;
126  
127 35 ▶ DBIO::Exception->throw(ref($self)->refusal_error($self)) if $self->refusal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->refusal 0 35 35
128  
129 35 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 16 19 35
130 16 return sprintf 'DROP TABLE %s;', _firebird_quote_ident($self->table_name);
131 }
132  
133 19 my @col_defs;
134 my @pk_cols;
135  
136 19 for my $col (@{ $self->columns }) {
137 34 ▶ push @pk_cols, $col->{column_name} if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 9 25 34
138  
139 34 ▶ my $type = $col->{data_type} || 'VARCHAR';
Condition50%1/2
$col->{'data_type'} || 'VARCHAR'
l!l
340
MC/DC0%0/1
$col->{'data_type'} || 'VARCHAR'
$col->{'data_type'}
Truth table50%1/2
A
A= $col->{'data_type'}
Aresult
11
00
140 my $def = sprintf ' %s %s%s', _firebird_quote_ident($col->{column_name}), $type,
141 34 render_size($col->{size});
142 # IDENTITY, DEFAULT, NOT NULL: the one order of the column clause, shared
143 # with install_ddl (karr k46).
144 $def .= _firebird_column_clauses(
145 identity => $col->{is_auto_increment},
146 default => $col->{default_value},
147 not_null => $col->{not_null},
148 34 );
149 34 push @col_defs, $def;
150 }
151  
152 19 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 8 11 19
153 push @col_defs, sprintf ' PRIMARY KEY (%s)',
154 8 join(', ', map { _firebird_quote_ident($_) } @pk_cols);
155 }
156  
157 # No foreign key here: they are ALTER TABLE ... ADD CONSTRAINT after every
158 # table of the diff (DBIO::Firebird::Diff::Constraint, karr k34), so the
159 # order of the tables and a cycle between them do not matter.
160  
161 19 return sprintf "CREATE TABLE %s (\n%s\n);",
162 _firebird_quote_ident($self->table_name), join(",\n", @col_defs);
163 }
164  
165  
166 10 ▶sub summary {
Subroutine summary: called CC=4 SCAR=14.3
Pod documented
167 10 my ($self) = @_;
168 10 ▶ my $kind = (($self->table_info // {})->{kind} // '') eq 'view' ? 'view' : 'table';
Branch: 1 branch
BranchTrueFalseTotal
(+($self->table_info // {})->{'kind'} // '') eq 'view' ? : 0 10 10
Condition75%3/4
+($self->table_info // {})->{"kind"} // ""
l!l
73
$self->table_info // {}
l!l
100
MC/DC0%0/1
+($self->table_info // {})->{"kind"} // ""
$self->table_info
Truth table50%1/2
A
A= $self->table_info
Aresult
11
00
169 10 return sprintf '%s %s: %s', $self->summary_prefix, $kind, $self->table_name;
170 }
171  
172 13 1;
173  
174 __END__
« Firebird/Storage/InterBase.pm ↑ Summary Firebird/Introspect/Uniques.pm »