DB2/Diff/Table.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 16 / 16 Conditioncond 85.7% 24 / 28 MC/DCmcdc 69.2% 9 / 13 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 94.8% 146 / 154 CC 29 SCAR 19.8
CC
29
Cov
97%
as_sql
11.0
diff
10.0
_fk_visit
6.0
CRAP 7.3
« DB2/Deploy.pm ↑ Summary DB2/Storage/DateTimeFormat.pm »
1 package DBIO::DB2::Diff::Table;
2 # ABSTRACT: Diff operations for DB2 tables
3  
4 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 47 our $VERSION = '0.900007';
8  
9 47 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 47 ▶use DBIO::DB2::Type qw(_db2_column_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 47 ▶use DBIO::DB2::DDL qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 _fk_constraint_clause _db2_quote_ident _db2_quote_table _db2_identity_clause
14 _unique_constraint_clause _refuse_nullable_unique
15 47 );
16  
17  
18 47 __PACKAGE__->mk_diff_accessors(qw/table_name table_info columns foreign_keys unique_constraints/);
19  
20  
21 169 ▶sub diff {
Subroutine diff: called CC=10 SCAR=23.0
Pod documented
22 169 my ($class, $source, $target, $target_columns, $target_fks, $target_ucs) = @_;
23 169 ▶ $target_columns //= {};
Condition100%2/2
$target_columns //= {}
l!l
1663
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
24 169 ▶ $target_fks //= {};
Condition100%2/2
$target_fks //= {}
l!l
1663
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
Aresult
11
00
25 169 ▶ $target_ucs //= {};
Condition100%2/2
$target_ucs //= {}
l!l
68101
MC/DC100%1/1
$target_ucs //= {}
$target_ucs
Truth table100%2/2
A
A= $target_ucs
Aresult
11
00
26  
27 # A view is no table: DBIO::DB2::Diff::View has the operations for it (karr
28 # k45, ADR 0011). A name that is a table on one side and a view on the other
29 # is a table to drop, or to create, here.
30 169 $source = _without_views($source);
31 169 $target = _without_views($target);
32  
33 169 my @ops;
34  
35 169 for my $name (_fk_order($target, $target_fks, $source)) {
36 push @ops, $class->new(
37 action => 'create',
38 table_name => $name,
39 table_info => $target->{$name},
40 columns => $target_columns->{$name} // [],
41 foreign_keys => $target_fks->{$name} // [],
42 27 ▶ unique_constraints => $target_ucs->{$name} // [],
Condition100%6/6
$target_columns->{$name} // []
l!l
207
$target_fks->{$name} // []
l!l
1314
$target_ucs->{$name} // []
l!l
225
MC/DC100%1/1
$target_columns->{$name} // []
$target_columns->{$name}
MC/DC100%1/1
$target_fks->{$name} // []
$target_fks->{$name}
MC/DC100%1/1
$target_ucs->{$name} // []
$target_ucs->{$name}
Truth table100%2/2
A
A= $target_columns->{$name}
Aresult
11
00
Truth table100%2/2
A
A= $target_fks->{$name}
Aresult
11
00
Truth table100%2/2
A
A= $target_ucs->{$name}
Aresult
11
00
43 );
44 }
45  
46 169 for my $name (sort keys %$source) {
47 165 ▶ next if exists $target->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$name} 157 8 165
48 push @ops, $class->new(
49 action => 'drop',
50 table_name => $name,
51 8 table_info => $source->{$name},
52 );
53 }
54  
55 169 return @ops;
56 }
57  
58 # The tables of $target that $source lacks, in the order they can be created:
59 # the FKs of a new table are inline in its CREATE TABLE, so the table a key
60 # references has to be there first (SQL0204N otherwise, karr k57). Depth first
61 # in name order, so tables that depend on nothing keep the name order. A
62 # self-reference and a reference to a table that exists already or is not new
63 # count for nothing; a cycle, which no order can create, is cut where it is met.
64 169 ▶sub _fk_order {
Subroutine _fk_order: called CC=2 SCAR=6.9
65 169 my ($target, $fks, $source) = @_;
66 169 my %new = map { $_ => 1 } grep { !exists $source->{$_} } keys %$target;
67 169 my (@order, %seen);
68 169 _fk_visit($_, \%new, $fks, \%seen, \@order) for sort keys %new;
69 169 return @order;
70 }
71  
72 31 ▶sub _fk_visit {
Subroutine _fk_visit: called CC=6 SCAR=17.9
73 31 my ($name, $new, $fks, $seen, $order) = @_;
74 31 ▶ return if $seen->{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$name}++ 4 27 31
75 27 ▶ _fk_visit($_, $new, $fks, $seen, $order) for sort grep { defined && $new->{$_} && $_ ne $name }
Condition83%5/6
defined $_ && $new->{$_} && $_ ne $name
!ll&&!rl&&r
414
defined $_ and $new->{$_}
!ll&&!rl&&r
045
MC/DC66%2/3
defined $_ && $new->{$_} && $_ ne $name
defined $_ $new->{$_} $_ ne $name
Truth table75%3/4
A and B && C
A= defined $_B= $new->{$_}C= $_ ne $name
ABCresult
0XX0
10X0
1100
1111
76 9 ▶ map { $_->{to_table} } @{ $fks->{$name} || [] };
Condition100%2/2
$fks->{$name} || []
l!l
1314
MC/DC100%1/1
$fks->{$name} || []
$fks->{$name}
Truth table100%2/2
A
A= $fks->{$name}
Aresult
11
00
77 27 push @$order, $name;
78 27 return;
79 }
80  
81 338 ▶sub _without_views {
Subroutine _without_views: called CC=4 SCAR=14.1
82 338 my ($tables) = @_;
83 349 return { map { $_ => $tables->{$_} }
84 338 ▶ grep { (($tables->{$_} || {})->{kind} // 'table') ne 'view' } keys %{ $tables || {} } };
Condition66%4/6
+($tables->{$_} || {})->{"kind"} // "table"
l!l
35634
$tables->{$_} || {}
l!l
3900
$tables || {}
l!l
3380
MC/DC0%0/1
+($tables->{$_} || {})->{"kind"} // "table"
$tables->{$_}
MC/DC0%0/1
$tables || {}
$tables
Truth table50%1/2
A
A= $tables->{$_}
Aresult
11
00
Truth table50%1/2
A
A= $tables
Aresult
11
00
85 }
86  
87  
88 29 ▶sub as_sql {
Subroutine as_sql: called CC=11 SCAR=24.0
Pod documented
89 29 my ($self) = @_;
90  
91 29 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 8 21 29
92 8 return sprintf 'DROP TABLE %s;', _db2_quote_table($self->table_name);
93 }
94  
95 21 my @col_defs;
96 my @pk_cols;
97  
98 21 for my $col (@{ $self->columns }) {
99 40 ▶ push @pk_cols, $col->{column_name} if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 13 27 40
100  
101 40 my $type = _db2_column_type($col->{catalog_type}, $col->{catalog_size}, $col->{column_name});
102 40 my $def = sprintf ' %s %s', _db2_quote_ident($col->{column_name}), $type;
103 40 ▶ $def .= ' NOT NULL' if $col->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"not_null"} 16 24 40
104 40 ▶ if ($col->{is_auto_increment}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($col->{'is_auto_increment'}) { } 4 36 40
elsif (defined $col->{'catalog_default'}) { } 10 26 36
105 4 $def .= ' ' . _db2_identity_clause();
106 }
107 elsif (defined $col->{catalog_default}) {
108 # As it is: the model's default_value is SYSCAT.COLUMNS.DEFAULT, which is
109 # the SQL text of the default already ('it''s', 0, CURRENT TIMESTAMP).
110 # Quoting it once more made the text itself the default (karr k41).
111 10 $def .= ' DEFAULT ' . $col->{catalog_default};
112 }
113 40 push @col_defs, $def;
114 }
115  
116 21 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 12 9 21
117 push @col_defs, sprintf ' PRIMARY KEY (%s)',
118 12 join(', ', map { _db2_quote_ident($_) } @pk_cols);
119 }
120  
121 # Named inline UNIQUE constraints, the clause of install_ddl (karr k39, ADR
122 # 0009). A nullable column is refused as there; the columns of this op are
123 # the catalog shape, so not_null is the test.
124 21 my %not_null = map { $_->{column_name} => $_->{not_null} } @{ $self->columns };
125 21 ▶ for my $uc (@{ $self->unique_constraints || [] }) {
Condition50%1/2
$self->unique_constraints || []
l!l
210
MC/DC0%0/1
$self->unique_constraints || []
$self->unique_constraints
Truth table50%1/2
A
A= $self->unique_constraints
Aresult
11
00
126 3 my ($name, $cols) = @$uc;
127 _refuse_nullable_unique($self->table_name, $name,
128 3 [ grep { !$not_null{$_} } @$cols ]);
129 2 push @col_defs, ' ' . _unique_constraint_clause($name, $cols);
130 }
131  
132 # Named inline FK constraints, rendered through the same shared clause builder
133 # as DBIO::DB2::DDL->install_ddl so a table created via the diff apply/upgrade
134 # path gets the deterministic fk_<table>_<cols> name and its ON DELETE/UPDATE
135 # rule -- both come straight from the target model. Without this, the server
136 # would assign a random FK name (the next compare phantom-drops+adds it) and
137 # the referential rule would be silently lost (ADR 0005).
138 20 for my $fk (@{ $self->foreign_keys }) {
139 3 push @col_defs, ' ' . _fk_constraint_clause($fk);
140 }
141  
142 20 return sprintf "CREATE TABLE %s (\n%s\n);",
143 _db2_quote_table($self->table_name), join(",\n", @col_defs);
144 }
145  
146  
147 6 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
148 6 my ($self) = @_;
149 6 return sprintf '%s table: %s', $self->summary_prefix, $self->table_name;
150 }
151  
152 47 1;
153  
154 __END__
« DB2/Deploy.pm ↑ Summary DB2/Storage/DateTimeFormat.pm »