DB2/Diff/Table.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 92.8% 13 / 14 Conditioncond 87.5% 7 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 95.6% 87 / 91 CC 16 SCAR 18.5
CC
16
Cov
96%
diff
9.0
as_sql
8.0
BEGIN
1.0
CRAP 6.3
« DB2/Storage.pm ↑ Summary DB2/Type.pm »
1 package DBIO::DB2::Diff::Table;
2 # ABSTRACT: Diff operations for DB2 tables
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::DB2::Type qw(_db2_column_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use DBIO::DB2::DDL qw(_fk_constraint_clause);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 4 __PACKAGE__->mk_diff_accessors(qw/table_name table_info columns foreign_keys/);
15  
16  
17 11 ▶sub diff {
Subroutine diff: called CC=9 SCAR=22.0
Pod documented
18 11 my ($class, $source, $target, $target_columns, $target_fks) = @_;
19 11 ▶ $target_columns //= {};
Condition100%2/2
$target_columns //= {}
l!l
101
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
20 11 ▶ $target_fks //= {};
Condition100%2/2
$target_fks //= {}
l!l
101
MC/DC100%1/1
$target_fks //= {}
$target_fks
Truth table100%2/2
A
A= $target_fks
Aresult
11
00
21  
22 11 my @ops;
23  
24 11 for my $name (sort keys %$target) {
25 11 ▶ next if exists $source->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $source->{$name} 5 6 11
26 push @ops, $class->new(
27 action => 'create',
28 table_name => $name,
29 table_info => $target->{$name},
30 columns => $target_columns->{$name} // [],
31 6 ▶ foreign_keys => $target_fks->{$name} // [],
Condition75%3/4
$target_columns->{$name} // []
l!l
60
$target_fks->{$name} // []
l!l
33
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
32 );
33 }
34  
35 11 for my $name (sort keys %$source) {
36 7 ▶ next if exists $target->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $target->{$name} 5 2 7
37 push @ops, $class->new(
38 action => 'drop',
39 table_name => $name,
40 2 table_info => $source->{$name},
41 );
42 }
43  
44 11 return @ops;
45 }
46  
47  
48 10 ▶sub as_sql {
Subroutine as_sql: called CC=8 SCAR=20.8
Pod documented
49 10 my ($self) = @_;
50  
51 10 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 2 8 10
52 2 return sprintf 'DROP TABLE %s;', _quote_ident($self->table_name);
53 }
54  
55 8 my @col_defs;
56 my @pk_cols;
57  
58 8 for my $col (@{ $self->columns }) {
59 11 ▶ push @pk_cols, $col->{column_name} if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 8 3 11
60  
61 11 my $type = _db2_column_type($col->{data_type}, $col->{size});
62 11 my $def = sprintf ' %s %s', _quote_ident($col->{column_name}), $type;
63 11 ▶ $def .= ' NOT NULL' if $col->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"not_null"} 2 9 11
64 11 ▶ if (defined $col->{default_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"default_value"}) 0 11 11
65 0 $def .= " DEFAULT $col->{default_value}";
66 }
67 11 push @col_defs, $def;
68 }
69  
70 8 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 7 1 8
71 push @col_defs, sprintf ' PRIMARY KEY (%s)',
72 7 join(', ', map { _quote_ident($_) } @pk_cols);
73 }
74  
75 # Named inline FK constraints, rendered through the same shared clause builder
76 # as DBIO::DB2::DDL->install_ddl so a table created via the diff apply/upgrade
77 # path gets the deterministic fk_<table>_<cols> name and its ON DELETE/UPDATE
78 # rule -- both come straight from the target model. Without this, the server
79 # would assign a random FK name (the next compare phantom-drops+adds it) and
80 # the referential rule would be silently lost (ADR 0005).
81 8 for my $fk (@{ $self->foreign_keys }) {
82 2 push @col_defs, ' ' . _fk_constraint_clause($fk);
83 }
84  
85 8 return sprintf "CREATE TABLE %s (\n%s\n);",
86 _quote_ident($self->table_name), join(",\n", @col_defs);
87 }
88  
89  
90 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
91 2 my ($self) = @_;
92 2 return sprintf '%s table: %s', $self->summary_prefix, $self->table_name;
93 }
94  
95 4 1;
96  
97 __END__
98  
99 =pod
100  
101 =encoding UTF-8
102  
103 =head1 NAME
104  
105 DBIO::DB2::Diff::Table - Diff operations for DB2 tables
106  
107 =head1 VERSION
108  
109 version 0.900001
110  
111 =head1 DESCRIPTION
112  
113 Represents a table-level diff operation in DB2: C<CREATE TABLE> or
114 C<DROP TABLE>. C<create> ops capture the target columns and foreign keys
115 so C<as_sql> can emit the full inline definition in a single statement.
116  
117 C<new>, the C<action> accessor and C<summary_prefix> come from
118 L<DBIO::Diff::Op>.
119  
120 =head1 METHODS
121  
122 =head2 diff
123  
124 my @ops = DBIO::DB2::Diff::Table->diff(
125 $source_tables, $target_tables,
126 $target_columns, $target_fks,
127 );
128  
129 =head2 as_sql
130  
131 =head2 summary
132  
133 =head1 AUTHOR
134  
135 DBIO & DBIx::Class Authors
136  
137 =head1 COPYRIGHT AND LICENSE
138  
139 Copyright (C) 2026 DBIO Authors
140 Portions Copyright (C) 2005-2025 DBIx::Class Authors
141 Based on DBIx::Class, heavily modified.
142  
143 This is free software; you can redistribute it and/or modify it under
144 the same terms as the Perl 5 programming language system itself.
145  
146 =cut
« DB2/Storage.pm ↑ Summary DB2/Type.pm »