Informix/Diff/Table.pm

filter: Statementstmt 100% 68 / 68 Branchbran 100% 30 / 30 Conditioncond 81.8% 27 / 33 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 92.5% 148 / 160 CC 34 SCAR 24.3
CC
34
Cov
95%
diff
16.1
as_sql
14.0
is_view
5.0
CRAP 11.4
« Informix/Types.pm ↑ Summary Informix/Diff/Column.pm »
1 package DBIO::Informix::Diff::Table;
2 # ABSTRACT: Diff operations for Informix tables
3  
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 our $VERSION = '0.900007';
8  
9 25 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 25 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 25 ▶use DBIO::Informix::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use DBIO::Informix::ColumnDef qw(column_def_sql);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 25 __PACKAGE__->mk_diff_accessors(qw(table_name table_info columns replaced));
17  
18  
19  
20 59 ▶sub diff {
Subroutine diff: called CC=16 SCAR=27.8
Pod documented
21 59 my ($class, $source, $target, $target_columns) = @_;
22 59 ▶ $target_columns //= {};
Condition100%2/2
$target_columns //= {}
l!l
581
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
23  
24 59 my @ops;
25  
26 59 for my $name (sort keys %$target) {
27 # A view that is in both with another definition is dropped and created.
28 94 ▶ if (exists $source->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $source->{$name}) 71 23 94
29 71 my ($old, $new) = ($source->{$name}, $target->{$name});
30 71 ▶ if (($old->{kind} // 'table') ne ($new->{kind} // 'table')) {
Branch: 2 branches
BranchTrueFalseTotal
if (($old->{'kind'} // 'table') ne ($new->{'kind'} // 'table')) { } 2 69 71
elsif (($new->{'kind'} // '') eq 'view' and ($old->{'definition'} // '') ne ($new->{'definition'} // '')) { } 2 133 135
Condition100%4/4
$old->{"kind"} // "table"
l!l
638
$new->{"kind"} // "table"
l!l
638
MC/DC100%1/1
$old->{"kind"} // "table"
$old->{"kind"}
MC/DC100%1/1
$new->{"kind"} // "table"
$new->{"kind"}
Truth table100%2/2
A
A= $old->{"kind"}
Aresult
11
00
Truth table100%2/2
A
A= $new->{"kind"}
Aresult
11
00
31 # DROP TABLE would take the rows, DROP VIEW is not what the table is:
32 # refused when the statement is asked for (as_sql), not simplified.
33 2 my $op = $class->new(action => 'change', table_name => $name, table_info => $new);
34 2 $op->{old_info} = $old;
35 2 push @ops, $op;
36 }
37 ▶ elsif ( (($new->{kind} // '') eq 'view' and ($old->{definition} // '') ne ($new->{definition} // ''))) {
Condition77%7/9
($new->{"kind"} // "") eq "view" and ($old->{"definition"} // "") ne ($new->{"definition"} // "")
!ll&&!rl&&r
6612
$new->{"kind"} // ""
l!l
618
$old->{"definition"} // ""
l!l
30
$new->{"definition"} // ""
l!l
30
MC/DC50%1/2
($new->{"kind"} // "") eq "view" and ($old->{"definition"} // "") ne ($new->{"definition"} // "")
$new->{"kind"} $old->{"definition"}
MC/DC0%0/1
$new->{"definition"} // ""
$new->{"definition"}
Truth table66%2/3
A and B
A= $new->{"kind"}B= $old->{"definition"}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $new->{"definition"}
Aresult
11
00
38 2 push @ops, $class->new(action => 'drop', table_name => $name, table_info => $old, replaced => 1);
39 push @ops, $class->new(action => 'create', table_name => $name, table_info => $new,
40 2 ▶ columns => $target_columns->{$name} // []);
Condition100%2/2
$target_columns->{$name} // []
l!l
11
MC/DC100%1/1
$target_columns->{$name} // []
$target_columns->{$name}
Truth table100%2/2
A
A= $target_columns->{$name}
Aresult
11
00
41 }
42 71 next;
43 }
44 push @ops, $class->new(
45 action => 'create',
46 table_name => $name,
47 table_info => $target->{$name},
48 23 ▶ columns => $target_columns->{$name} // [],
Condition50%1/2
$target_columns->{$name} // []
l!l
230
MC/DC0%0/1
$target_columns->{$name} // []
$target_columns->{$name}
Truth table50%1/2
A
A= $target_columns->{$name}
Aresult
11
00
49 );
50 }
51  
52 59 for my $name (sort keys %$source) {
53 82 ▶ next if exists $target->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$name} 71 11 82
54 push @ops, $class->new(
55 action => 'drop',
56 table_name => $name,
57 11 table_info => $source->{$name},
58 );
59 }
60  
61 59 return @ops;
62 }
63  
64  
65 125 ▶sub is_view {
Subroutine is_view: called CC=5 SCAR=16.1
Pod documented
66 125 my ($self) = @_;
67 125 ▶ return 0 if $self->action eq 'change';
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "change" 6 119 125
68 119 my $i = $self->table_info;
69 119 ▶ return (ref $i eq 'HASH' && ($i->{kind} // '') eq 'view') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $i eq 'HASH' && ($i->{'kind'} // '') eq 'view' ? : 47 72 119
Condition80%4/5
ref $i eq "HASH" and ($i->{"kind"} // "") eq "view"
!ll&&!rl&&r
07247
$i->{"kind"} // ""
l!l
8336
MC/DC50%1/2
ref $i eq "HASH" and ($i->{"kind"} // "") eq "view"
ref $i eq "HASH" $i->{"kind"}
Truth table66%2/3
A and B
A= ref $i eq "HASH"B= $i->{"kind"}
ABresult
0X0
100
111
70 }
71  
72  
73 31 ▶sub as_sql {
Subroutine as_sql: called CC=14 SCAR=26.4
Pod documented
74 31 my ($self) = @_;
75  
76 31 ▶ if ($self->action eq 'change') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "change") 2 29 31
77 2 ▶ my $old = $self->{old_info}{kind} // 'table';
Condition50%1/2
$self->{"old_info"}{"kind"} // "table"
l!l
20
MC/DC0%0/1
$self->{"old_info"}{"kind"} // "table"
$self->{"old_info"}{"kind"}
Truth table50%1/2
A
A= $self->{"old_info"}{"kind"}
Aresult
11
00
78 2 ▶ DBIO::Exception->throw(sprintf
Branch: 1 branch
BranchTrueFalseTotal
$old eq 'view' ? : 1 1 2
79 "%s: the database has a %s and the schema a %s (a table and view of one name); "
80 . "the rows of a table are not dropped to make a view, nor a view replaced by a table here",
81 $self->table_name, $old, $old eq 'view' ? 'table' : 'view');
82 }
83  
84 29 ▶ if ($self->is_view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_view) 7 22 29
85 7 ▶ return sprintf 'DROP VIEW %s;', _quote_ident($self->table_name)
Branch: 1 branch
BranchTrueFalseTotal
if $self->action eq "drop" 3 4 7
86 if $self->action eq 'drop';
87 # the definition is the CREATE VIEW the server keeps (owner-qualified)
88 4 my $def = $self->table_info->{definition};
89 4 ▶ DBIO::Exception->throw(sprintf 'view %s: the model holds no definition to write it from',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 1 3 4
Condition66%2/3
defined $def and length $def
!ll&&!rl&&r
103
MC/DC50%1/2
defined $def and length $def
defined $def length $def
Truth table66%2/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
90 $self->table_name) unless defined $def && length $def;
91 3 return $def . ';';
92 }
93  
94 22 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 6 16 22
95 6 return sprintf 'DROP TABLE %s;', _quote_ident($self->table_name);
96 }
97  
98 16 my @col_defs;
99 my @pk_cols;
100  
101 16 for my $col (@{ $self->columns }) {
102 28 ▶ push @pk_cols, $col if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 11 17 28
103 push @col_defs, ' ' . column_def_sql(
104 name => $col->{column_name},
105 type => $col->{data_type},
106 size => $col->{size},
107 not_null => $col->{not_null},
108 default_value => $col->{default_value},
109 is_auto_increment => $col->{is_auto_increment},
110 28 );
111 }
112  
113 # The PK is a table-level constraint, in key order (pk_position);
114 # columns without a position keep their table order.
115 16 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 9 7 16
116 11 my @ordered = map { $_->{column_name} }
117 9 ▶ sort { ($a->{pk_position} || 0) <=> ($b->{pk_position} || 0) } @pk_cols;
Condition100%4/4
$DBIO::Informix::Diff::Table::a->{'pk_position'} || 0
l!l
11
$DBIO::Informix::Diff::Table::b->{'pk_position'} || 0
l!l
11
MC/DC100%1/1
$DBIO::Informix::Diff::Table::a->{'pk_position'} || 0
$DBIO::Informix::Diff::Table::a->{'pk_position'}
MC/DC100%1/1
$DBIO::Informix::Diff::Table::b->{'pk_position'} || 0
$DBIO::Informix::Diff::Table::b->{'pk_position'}
Truth table100%2/2
A
A= $DBIO::Informix::Diff::Table::a->{'pk_position'}
Aresult
11
00
Truth table100%2/2
A
A= $DBIO::Informix::Diff::Table::b->{'pk_position'}
Aresult
11
00
118 push @col_defs, sprintf ' PRIMARY KEY (%s)',
119 9 join(', ', map { _quote_ident($_) } @ordered);
120 }
121  
122 16 return sprintf "CREATE TABLE %s (\n%s\n);",
123 _quote_ident($self->table_name), join(",\n", @col_defs);
124 }
125  
126  
127 9 ▶sub summary {
Subroutine summary: called CC=2 SCAR=6.9
Pod documented
128 9 my ($self) = @_;
129 9 ▶ return sprintf '%s %s: %s', $self->summary_prefix,
Branch: 1 branch
BranchTrueFalseTotal
$self->is_view ? : 1 8 9
130 ($self->is_view ? 'view' : 'table'), $self->table_name;
131 }
132  
133  
134 25 1;
135  
136 __END__
« Informix/Types.pm ↑ Summary Informix/Diff/Column.pm »