Oracle/Diff/Table.pm

filter: Statementstmt 100% 123 / 123 Branchbran 92.5% 37 / 40 Conditioncond 69.2% 45 / 65 MC/DCmcdc 47.0% 16 / 34 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 85.4% 240 / 281 CC 55 SCAR 22.5
CC
55
Cov
90%
_owned_sequences
14.4
as_sql
14.1
diff
11.1
CRAP 9.5
« Oracle/Diff/View.pm ↑ Summary Oracle/Storage.pm »
1 package DBIO::Oracle::Diff::Table;
2 # ABSTRACT: Diff operations for Oracle tables
3  
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 our $VERSION = '0.900007';
8  
9 32 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 32 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use DBIO::Oracle::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use DBIO::Oracle::Type ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 32 __PACKAGE__->mk_diff_accessors(qw(table_name table_info columns unique_constraints sequences));
16  
17  
18  
19 116 ▶sub diff {
Subroutine diff: called CC=11 SCAR=24.0
Pod documented
20 116 my ($class, $source, $target, $target_columns, $target_unique, $source_columns) = @_;
21 116 ▶ $target_columns //= {};
Condition100%2/2
$target_columns //= {}
l!l
1133
MC/DC100%1/1
$target_columns //= {}
$target_columns
Truth table100%2/2
A
A= $target_columns
Aresult
11
00
22 116 ▶ $target_unique //= {};
Condition100%2/2
$target_unique //= {}
l!l
1133
MC/DC100%1/1
$target_unique //= {}
$target_unique
Truth table100%2/2
A
A= $target_unique
Aresult
11
00
23 116 ▶ $source_columns //= {};
Condition100%2/2
$source_columns //= {}
l!l
10610
MC/DC100%1/1
$source_columns //= {}
$source_columns
Truth table100%2/2
A
A= $source_columns
Aresult
11
00
24  
25 # A name both models have must be the same kind of object in both.
26 116 ▶ for my $name (sort keys %{ $target // {} }) {
Condition50%1/2
$target // {}
l!l
1160
MC/DC0%0/1
$target // {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
27 165 ▶ next unless $source && exists $source->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $source and exists $source->{$name} 32 133 165
Condition66%2/3
$source and exists $source->{$name}
!ll&&!rl&&r
032133
MC/DC50%1/2
$source and exists $source->{$name}
$source exists $source->{$name}
Truth table66%2/3
A and B
A= $sourceB= exists $source->{$name}
ABresult
0X0
100
111
28 133 my ($was, $is) = map { _kind($_->{$name}) } $source, $target;
29 133 ▶ next if $was eq $is;
Branch: 1 branch
BranchTrueFalseTotal
if $was eq $is 131 2 133
30 2 DBIO::Exception->throw(
31 "DBIO::Oracle::Diff: $name is a $was in the source model and a $is in "
32 . "the target model; the diff does not turn one into the other");
33 }
34  
35 # Views are DBIO::Oracle::Diff::View's: created and dropped there.
36 114 ▶ $source = { map { $_ => $source->{$_} } grep { _kind($source->{$_}) ne 'view' } keys %{ $source // {} } };
Condition50%1/2
$source // {}
l!l
1140
MC/DC0%0/1
$source // {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
37 114 ▶ $target = { map { $_ => $target->{$_} } grep { _kind($target->{$_}) ne 'view' } keys %{ $target // {} } };
Condition50%1/2
$target // {}
l!l
1140
MC/DC0%0/1
$target // {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
38  
39 return $class->diff_toplevel(
40 $source, $target,
41 22 ▶ create => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
42 22 my ($name) = @_;
43 22 ▶ my $columns = $target_columns->{$name} // [];
Condition100%2/2
$target_columns->{$name} // []
l!l
211
MC/DC100%1/1
$target_columns->{$name} // []
$target_columns->{$name}
Truth table100%2/2
A
A= $target_columns->{$name}
Aresult
11
00
44 22 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless @$columns 1 21 22
45 "DBIO::Oracle::Diff: cannot create table '$name': the target model "
46 . 'holds no columns for it')
47 unless @$columns;
48 my $op = $class->new(
49 action => 'create',
50 table_name => $name,
51 table_info => $target->{$name},
52 columns => $columns,
53 21 ▶ unique_constraints => $target_unique->{$name} // [],
Condition100%2/2
$target_unique->{$name} // []
l!l
615
MC/DC100%1/1
$target_unique->{$name} // []
$target_unique->{$name}
Truth table100%2/2
A
A= $target_unique->{$name}
Aresult
11
00
54 );
55 21 $op->_auto_increment; # refuses what cannot be written, before any SQL
56 18 $op;
57 },
58 17 ▶ drop => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 17 my ($name) = @_;
60 $class->new(
61 action => 'drop',
62 table_name => $name,
63 table_info => $source->{$name},
64 sequences => [ $class->_owned_sequences(
65 17 $name, $source->{$name}, $source_columns, $target_columns,
66 ) ],
67 );
68 },
69 114 );
70 }
71  
72 # The sequences the install made for a table, from the columns of the source
73 # model: those of its own schema (the install makes no other), unless a column
74 # of another table of the target model is fed from the same one.
75 17 ▶sub _owned_sequences {
Subroutine _owned_sequences: called CC=14 SCAR=26.6
76 17 my ($class, $name, $info, $source_columns, $target_columns) = @_;
77 17 ▶ my $schema = ref $info eq 'HASH' ? $info->{schema} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $info eq 'HASH' ? : 17 0 17
78  
79 17 my %used_elsewhere;
80 17 for my $other (keys %$target_columns) {
81 13 ▶ next if $other eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $other eq $name 1 12 13
82 $used_elsewhere{ $_->{sequence} } = 1
83 12 for grep { defined $_->{sequence} } @{ $target_columns->{$other} };
84 }
85  
86 17 my @seqs;
87 17 ▶ for my $col (@{ $source_columns->{$name} // [] }) {
Condition100%2/2
$source_columns->{$name} // []
l!l
161
MC/DC100%1/1
$source_columns->{$name} // []
$source_columns->{$name}
Truth table100%2/2
A
A= $source_columns->{$name}
Aresult
11
00
88 next unless $col->{is_auto_increment}
89 25 ▶ && defined $col->{sequence} && length $col->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
unless $col->{"is_auto_increment"} and defined $col->{"sequence"} and length $col->{"sequence"} 18 7 25
Condition83%5/6
$col->{"is_auto_increment"} and defined $col->{"sequence"} and length $col->{"sequence"}
!ll&&!rl&&r
1807
$col->{"is_auto_increment"} and defined $col->{"sequence"}
!ll&&!rl&&r
1717
MC/DC66%2/3
$col->{"is_auto_increment"} and defined $col->{"sequence"} and length $col->{"sequence"}
$col->{"is_auto_increment"} defined $col->{"sequence"} length $col->{"sequence"}
Truth table75%3/4
A and B and C
A= $col->{"is_auto_increment"}B= defined $col->{"sequence"}C= length $col->{"sequence"}
ABCresult
0XX0
10X0
1100
1111
90 7 ▶ next if $used_elsewhere{ $col->{sequence} };
Branch: 1 branch
BranchTrueFalseTotal
if $used_elsewhere{$col->{"sequence"}} 1 6 7
91 6 my ($seq_schema, $seq) = _split_sequence($col->{sequence}, $schema);
92 6 ▶ next if defined $seq_schema && defined $schema && $seq_schema ne $schema;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seq_schema and defined $schema and $seq_schema ne $schema 2 4 6
Condition50%3/6
defined $seq_schema and defined $schema and $seq_schema ne $schema
!ll&&!rl&&r
042
defined $seq_schema and defined $schema
!ll&&!rl&&r
006
MC/DC33%1/3
defined $seq_schema and defined $schema and $seq_schema ne $schema
defined $seq_schema defined $schema $seq_schema ne $schema
Truth table50%2/4
A and B and C
A= defined $seq_schemaB= defined $schemaC= $seq_schema ne $schema
ABCresult
0XX0
10X0
1100
1111
93 4 push @seqs, $seq;
94 }
95 17 return @seqs;
96 }
97  
98 # ( schema, name ) of the model's "schema.sequence". The schema of the table is
99 # taken off when the name starts with it (a name may hold a dot), else the
100 # text before the first dot is the schema.
101 15 ▶sub _split_sequence {
Subroutine _split_sequence: called CC=4 SCAR=14.2
102 15 my ($full, $table_schema) = @_;
103 15 ▶ return ($table_schema, substr $full, length($table_schema) + 1)
Branch: 1 branch
BranchTrueFalseTotal
if defined $table_schema and index($full, "$table_schema.") == 0 12 3 15
Condition66%2/3
defined $table_schema and index($full, "$table_schema.") == 0
!ll&&!rl&&r
0312
MC/DC50%1/2
defined $table_schema and index($full, "$table_schema.") == 0
defined $table_schema index($full, "$table_schema.") == 0
Truth table66%2/3
A and B
A= defined $table_schemaB= index($full, "$table_schema.") == 0
ABresult
0X0
100
111
104 if defined $table_schema && index($full, "$table_schema.") == 0;
105 3 ▶ return $full =~ /\A([^.]*)\.(.+)\z/s ? ($1, $2) : (undef, $full);
Branch: 1 branch
BranchTrueFalseTotal
$full =~ /\A([^.]*)\.(.+)\z/s ? : 3 0 3
106 }
107  
108  
109 40 ▶sub as_sql {
Subroutine as_sql: called CC=14 SCAR=26.5
Pod documented
110 40 my ($self) = @_;
111 40 my $q = \&DBIO::Oracle::Identifier::quote;
112  
113 40 ▶ if ($self->action eq 'drop') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "drop") 17 23 40
114 return join "\n",
115 sprintf('DROP TABLE %s CASCADE CONSTRAINTS;', DBIO::Oracle::Identifier::quote_table($self->table_name)),
116 17 ▶ map { sprintf 'DROP SEQUENCE %s;', DBIO::Oracle::Identifier::quote_in($self->table_name, $_) } @{ $self->sequences // [] };
Condition50%1/2
$self->sequences // []
l!l
170
MC/DC0%0/1
$self->sequences // []
$self->sequences
Truth table50%1/2
A
A= $self->sequences
Aresult
11
00
117 }
118  
119 23 my $table = $self->table_name;
120 23 my @col_defs;
121 my @pk_cols;
122  
123 23 for my $col (@{ $self->columns }) {
124 52 ▶ push @pk_cols, $col if $col->{is_pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"is_pk"} 14 38 52
125  
126 52 my $def = sprintf ' %s %s', $q->($col->{column_name}), _oracle_type($col);
127  
128 # Handle default value. It goes before NOT NULL: Oracle's column
129 # definition is "datatype [DEFAULT expr] [inline_constraint ...]" and it
130 # refuses the other order.
131 52 ▶ if (defined $col->{default_value}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col->{"default_value"} and ref $col->{"default_value"} || $col->{"default_value"} ne "null") 5 47 52
Condition66%4/6
defined $col->{"default_value"} and ref $col->{"default_value"} || $col->{"default_value"} ne "null"
!ll&&!rl&&r
4705
ref $col->{"default_value"} or $col->{"default_value"} ne "null"
l!l&&r!l&&!r
140
MC/DC33%1/3
defined $col->{"default_value"} and ref $col->{"default_value"} || $col->{"default_value"} ne "null"
defined $col->{"default_value"} ref $col->{"default_value"} $col->{"default_value"} ne "null"
Truth table75%3/4
A and B or C
A= defined $col->{"default_value"}B= ref $col->{"default_value"}C= $col->{"default_value"} ne "null"
ABCresult
0XX0
1000
11X1
1011
132 && (ref $col->{default_value} || $col->{default_value} ne 'null')) {
133 $def .= ' DEFAULT '
134 5 . DBIO::Oracle::Type::render_default_value($col->{default_value});
135 }
136  
137 52 ▶ $def .= ' NOT NULL' if $col->{not_null};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"not_null"} 25 27 52
138  
139 52 push @col_defs, $def;
140 }
141  
142 23 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 13 10 23
143 # In the order of the constraint (pk_position, set by the introspection),
144 # not of the table; columns without a position keep the table's order.
145 13 my $i = 0;
146 14 my @ordered = map { $_->[2] }
147 1 ▶ sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] }
Condition33%1/3
$DBIO::Oracle::Diff::Table::a->[0] <=> $DBIO::Oracle::Diff::Table::b->[0] || $DBIO::Oracle::Diff::Table::a->[1] <=> $DBIO::Oracle::Diff::Table::b->[1]
l!l&&r!l&&!r
010
MC/DC0%0/2
$DBIO::Oracle::Diff::Table::a->[0] <=> $DBIO::Oracle::Diff::Table::b->[0] || $DBIO::Oracle::Diff::Table::a->[1] <=> $DBIO::Oracle::Diff::Table::b->[1]
$DBIO::Oracle::Diff::Table::a->[0] <=> $DBIO::Oracle::Diff::Table::b->[0] $DBIO::Oracle::Diff::Table::a->[1] <=> $DBIO::Oracle::Diff::Table::b->[1]
Truth table33%1/3
A || B
A= $DBIO::Oracle::Diff::Table::a->[0] <=> $DBIO::Oracle::Diff::Table::b->[0]B= $DBIO::Oracle::Diff::Table::a->[1] <=> $DBIO::Oracle::Diff::Table::b->[1]
ABresult
1X1
011
000
148 13 ▶ map { [ $_->{pk_position} // 1e9, $i++, $_->{column_name} ] } @pk_cols;
Condition100%2/2
$_->{"pk_position"} // "1000000000"
l!l
86
MC/DC100%1/1
$_->{"pk_position"} // "1000000000"
$_->{"pk_position"}
Truth table100%2/2
A
A= $_->{"pk_position"}
Aresult
11
00
149 push @col_defs, sprintf ' PRIMARY KEY (%s)',
150 13 join(', ', map { $q->($_) } @ordered);
151 }
152  
153 23 for my $uc (@{ $self->unique_constraints }) {
154 4 my ($name, $cols) = @$uc;
155 push @col_defs, sprintf ' CONSTRAINT %s UNIQUE (%s)',
156 4 $q->($name), join(', ', map { $q->($_) } @$cols);
157 }
158  
159 23 my @stmts = sprintf "CREATE TABLE %s (\n%s\n);", DBIO::Oracle::Identifier::quote_table($table),
160 join(",\n", @col_defs);
161  
162 # A sequence and the trigger that fills the column from it, as the install
163 # writes them (DBIO::Oracle::DDL), after the table.
164 23 for my $auto ($self->_auto_increment) {
165 5 my ($col, $seq, $trg) = @$auto;
166 5 push @stmts, sprintf 'CREATE SEQUENCE %s;', DBIO::Oracle::Identifier::quote_in($table, $seq);
167 5 push @stmts, sprintf
168 "CREATE OR REPLACE TRIGGER %s\n"
169 . "BEFORE INSERT ON %s FOR EACH ROW WHEN (new.%s IS NULL)\n"
170 . "BEGIN\n"
171 . " SELECT %s.NEXTVAL INTO :new.%s FROM DUAL;\n"
172 . "END;",
173 DBIO::Oracle::Identifier::quote_in($table, $trg), DBIO::Oracle::Identifier::quote_table($table),
174 $q->($col), DBIO::Oracle::Identifier::quote_in($table, $seq), $q->($col);
175 }
176  
177 23 return join "\n", @stmts;
178 }
179  
180 # [ column, sequence, trigger ] for each column a sequence feeds. The model
181 # names the sequence with its schema, as the server spells them ("U.parent_id_seq"); the
182 # sequence is made in the schema of the table, so one of another is refused.
183 44 ▶sub _auto_increment {
Subroutine _auto_increment: called CC=9 SCAR=22.4
184 44 my ($self) = @_;
185 44 my $table = $self->table_name;
186 44 ▶ my $schema = ref $self->table_info eq 'HASH' ? $self->table_info->{schema} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->table_info eq 'HASH' ? : 44 0 44
187 44 my @auto;
188  
189 44 for my $col (@{ $self->columns }) {
190 93 ▶ next unless $col->{is_auto_increment};
Branch: 1 branch
BranchTrueFalseTotal
unless $col->{"is_auto_increment"} 82 11 93
191 11 my $name = $col->{column_name};
192  
193 DBIO::Exception->throw(
194 "DBIO::Oracle::Diff: cannot create table '$table': column '$name' is "
195 . 'auto-increment without a sequence (a GENERATED ... AS IDENTITY '
196 . 'column), and the model does not say whether the identity is ALWAYS '
197 . 'or BY DEFAULT')
198 11 ▶ unless defined $col->{sequence} && length $col->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col->{"sequence"} and length $col->{"sequence"} 2 9 11
Condition66%2/3
defined $col->{"sequence"} and length $col->{"sequence"}
!ll&&!rl&&r
209
MC/DC50%1/2
defined $col->{"sequence"} and length $col->{"sequence"}
defined $col->{"sequence"} length $col->{"sequence"}
Truth table66%2/3
A and B
A= defined $col->{"sequence"}B= length $col->{"sequence"}
ABresult
0X0
100
111
199  
200 9 my ($seq_schema, $seq) = _split_sequence($col->{sequence}, $schema);
201 9 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if defined $seq_schema and defined $schema and $seq_schema ne $schema 1 8 9
Condition50%3/6
defined $seq_schema and defined $schema and $seq_schema ne $schema
!ll&&!rl&&r
081
defined $seq_schema and defined $schema
!ll&&!rl&&r
009
MC/DC33%1/3
defined $seq_schema and defined $schema and $seq_schema ne $schema
defined $seq_schema defined $schema $seq_schema ne $schema
Truth table50%2/4
A and B and C
A= defined $seq_schemaB= defined $schemaC= $seq_schema ne $schema
ABCresult
0XX0
10X0
1100
1111
202 "DBIO::Oracle::Diff: cannot create table '$table': the sequence "
203 . "'$col->{sequence}' of column '$name' is not one of the schema "
204 . "'$schema' the table is in")
205 if defined $seq_schema && defined $schema && $seq_schema ne $schema;
206  
207 8 push @auto, [
208 $name, $seq,
209 DBIO::Oracle::Identifier::shorten((DBIO::Oracle::Identifier::split_table($table))[1] . "_${name}_trg"),
210 ];
211 }
212 41 return @auto;
213 }
214  
215  
216 5 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
217 5 my ($self) = @_;
218 5 return sprintf '%s %s: %s', $self->summary_prefix,
219 _kind($self->table_info), $self->table_name;
220 }
221  
222 # 'table' or 'view', as Introspect::Tables records it; a model without a kind
223 # holds tables.
224 591 ▶sub _kind {
Subroutine _kind: called CC=4 SCAR=13.9
225 591 my ($info) = @_;
226 591 ▶ return ( ref $info eq 'HASH' && ( $info->{kind} // '' ) eq 'view' ) ? 'view' : 'table';
Branch: 1 branch
BranchTrueFalseTotal
ref $info eq 'HASH' && ($info->{'kind'} // '') eq 'view' ? : 65 526 591
Condition80%4/5
ref $info eq "HASH" and ($info->{"kind"} // "") eq "view"
!ll&&!rl&&r
052665
$info->{"kind"} // ""
l!l
289302
MC/DC50%1/2
ref $info eq "HASH" and ($info->{"kind"} // "") eq "view"
ref $info eq "HASH" $info->{"kind"}
Truth table66%2/3
A and B
A= ref $info eq "HASH"B= $info->{"kind"}
ABresult
0X0
100
111
227 }
228  
229 52 ▶sub _oracle_type {
Subroutine _oracle_type: called CC=2 SCAR=7.2
230 52 my ($col) = @_;
231 return DBIO::Oracle::Type::map_dbio_type_to_oracle(
232 $col->{data_type} || 'VARCHAR2', size => $col->{size},
233 52 ▶ );
Condition50%1/2
$col->{'data_type'} || 'VARCHAR2'
l!l
520
MC/DC0%0/1
$col->{'data_type'} || 'VARCHAR2'
$col->{'data_type'}
Truth table50%1/2
A
A= $col->{'data_type'}
Aresult
11
00
234 }
235  
236 32 1;
237  
238 __END__
« Oracle/Diff/View.pm ↑ Summary Oracle/Storage.pm »