Oracle/Diff.pm

filter: Statementstmt 100% 114 / 114 Branchbran 80.7% 21 / 26 Conditioncond 77.0% 57 / 74 MC/DCmcdc 60.0% 21 / 35 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 86.2% 226 / 262 CC 52 SCAR 31.4
CC
52
Cov
90%
_build_operations
31.6
_keys_over_gone_unique
23.4
__ANON__
2.0
CRAP 23.2
« Oracle/Introspect/ForeignKeys.pm ↑ Summary Oracle/Deploy.pm »
1 package DBIO::Oracle::Diff;
2 # ABSTRACT: Compare two introspected Oracle models
3  
4 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 our $VERSION = '0.900007';
8  
9 30 ▶use base 'DBIO::Diff::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 30 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use DBIO::Oracle::Diff::Table ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 ▶use DBIO::Oracle::Diff::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use DBIO::Oracle::Diff::Index ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use DBIO::Oracle::Diff::ForeignKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use DBIO::Oracle::Diff::Unique ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 30 ▶use DBIO::Oracle::Diff::View ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 30 my %_REGISTRY = (
21 tables => 'DBIO::Oracle::Diff::Table',
22 columns => 'DBIO::Oracle::Diff::Column',
23 indexes => 'DBIO::Oracle::Diff::Index',
24 );
25  
26 30 my @_ORDER = qw(tables columns indexes);
27  
28 # The sections of the models a kind of operation is built from besides its own:
29 # which tables the models have, to tell a table both have from a new one.
30 30 my %_NEEDS_TABLES = map { $_ => 1 } qw(columns indexes);
31  
32 121 ▶sub _build_operations {
Subroutine _build_operations: called CC=31 SCAR=34.5
33 121 my ($self) = @_;
34 121 my (@ops, @gone_fk, @gone_unique, @new_unique, @new_fk);
35  
36 121 my ($src, $tgt) = ($self->source, $self->target);
37  
38 # Foreign keys and unique constraints (karr k64): the ones that are gone are
39 # dropped before anything else happens to tables and columns, the new ones
40 # are added after it.
41 121 ▶ my @tables = ($src->{tables} // {}, $tgt->{tables} // {});
Condition75%3/4
$src->{"tables"} // {}
l!l
1192
$tgt->{"tables"} // {}
l!l
1210
MC/DC100%1/1
$src->{"tables"} // {}
$src->{"tables"}
MC/DC0%0/1
$tgt->{"tables"} // {}
$tgt->{"tables"}
Truth table100%2/2
A
A= $src->{"tables"}
Aresult
11
00
Truth table50%1/2
A
A= $tgt->{"tables"}
Aresult
11
00
42 my @fk = DBIO::Oracle::Diff::ForeignKey->diff(
43 $src->{foreign_keys} // {}, $tgt->{foreign_keys} // {}, @tables, $tgt->{columns} // {},
44 121 ▶ );
Condition100%6/6
$src->{"foreign_keys"} // {}
l!l
7843
$tgt->{"foreign_keys"} // {}
l!l
7051
$tgt->{"columns"} // {}
l!l
1192
MC/DC100%1/1
$src->{"foreign_keys"} // {}
$src->{"foreign_keys"}
MC/DC100%1/1
$tgt->{"foreign_keys"} // {}
$tgt->{"foreign_keys"}
MC/DC100%1/1
$tgt->{"columns"} // {}
$tgt->{"columns"}
Truth table100%2/2
A
A= $src->{"foreign_keys"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"foreign_keys"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"columns"}
Aresult
11
00
45 my @uq = DBIO::Oracle::Diff::Unique->diff(
46 $src->{unique_constraints} // {}, $tgt->{unique_constraints} // {}, @tables, $tgt->{columns} // {},
47 118 ▶ );
Condition100%6/6
$src->{"unique_constraints"} // {}
l!l
6454
$tgt->{"unique_constraints"} // {}
l!l
5662
$tgt->{"columns"} // {}
l!l
1162
MC/DC100%1/1
$src->{"unique_constraints"} // {}
$src->{"unique_constraints"}
MC/DC100%1/1
$tgt->{"unique_constraints"} // {}
$tgt->{"unique_constraints"}
MC/DC100%1/1
$tgt->{"columns"} // {}
$tgt->{"columns"}
Truth table100%2/2
A
A= $src->{"unique_constraints"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"unique_constraints"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"columns"}
Aresult
11
00
48 118 @gone_fk = grep { $_->action eq 'drop' } @fk;
49 118 @new_fk = grep { $_->action eq 'create' } @fk;
50 118 @gone_unique = grep { $_->action eq 'drop' } @uq;
51 118 @new_unique = grep { $_->action eq 'create' } @uq;
52  
53 # A key that stays over a unique constraint the target drops or changes
54 # (ORA-02273, karr k64) goes first and is created again, unchanged, after.
55 118 my ($again_drop, $again_new) = $self->_keys_over_gone_unique(\@gone_unique);
56 116 push @gone_fk, @$again_drop;
57 116 push @new_fk, @$again_new;
58  
59 # Views (karr k65): the ones that are gone are dropped before the tables they
60 # rest on are touched, the new and changed ones written after everything else.
61 my @views = DBIO::Oracle::Diff::View->diff(
62 @tables, $src->{views} // {}, $tgt->{views} // {},
63 116 ▶ );
Condition100%4/4
$src->{"views"} // {}
l!l
4274
$tgt->{"views"} // {}
l!l
4274
MC/DC100%1/1
$src->{"views"} // {}
$src->{"views"}
MC/DC100%1/1
$tgt->{"views"} // {}
$tgt->{"views"}
Truth table100%2/2
A
A= $src->{"views"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"views"}
Aresult
11
00
64 106 my @gone_view = grep { $_->action eq 'drop' } @views;
65 106 my @new_view = grep { $_->action ne 'drop' } @views;
66  
67 106 push @ops, @gone_fk, @gone_unique, @gone_view;
68  
69 106 for my $key (@_ORDER) {
70 308 ▶ my $diff_cls = $_REGISTRY{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $diff_cls = $_REGISTRY{$key} or next 308 0 308
71 308 ▶ next unless exists $src->{$key} || exists $tgt->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $src->{$key} or exists $tgt->{$key} 62 246 308
Condition100%3/3
exists $src->{$key} or exists $tgt->{$key}
l!l&&r!l&&!r
242462
MC/DC100%2/2
exists $src->{$key} or exists $tgt->{$key}
exists $src->{$key} exists $tgt->{$key}
Truth table100%3/3
A or B
A= exists $src->{$key}B= exists $tgt->{$key}
ABresult
1X1
011
000
72  
73 246 my @extra;
74 246 ▶ if ($_NEEDS_TABLES{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_NEEDS_TABLES{$key}) { } 140 106 246
elsif ($key eq 'tables') { } 106 0 106
75 140 @extra = @tables;
76 }
77 elsif ($key eq 'tables') {
78 # what a table only the target has is created from
79 @extra = (
80 $tgt->{columns} // {},
81 $tgt->{unique_constraints} // {},
82 $src->{columns} // {}, # the sequences of a dropped table
83 106 ▶ );
Condition100%6/6
$tgt->{"columns"} // {}
l!l
1051
$tgt->{"unique_constraints"} // {}
l!l
4561
$src->{"columns"} // {}
l!l
1033
MC/DC100%1/1
$tgt->{"columns"} // {}
$tgt->{"columns"}
MC/DC100%1/1
$tgt->{"unique_constraints"} // {}
$tgt->{"unique_constraints"}
MC/DC100%1/1
$src->{"columns"} // {}
$src->{"columns"}
Truth table100%2/2
A
A= $tgt->{"columns"}
Aresult
11
00
Truth table100%2/2
A
A= $tgt->{"unique_constraints"}
Aresult
11
00
Truth table100%2/2
A
A= $src->{"columns"}
Aresult
11
00
84 }
85  
86 push @ops, $diff_cls->diff(
87 $src->{$key} // {},
88 246 ▶ $tgt->{$key} // {},
Condition75%3/4
$src->{$key} // {}
l!l
2424
$tgt->{$key} // {}
l!l
2460
MC/DC100%1/1
$src->{$key} // {}
$src->{$key}
MC/DC0%0/1
$tgt->{$key} // {}
$tgt->{$key}
Truth table100%2/2
A
A= $src->{$key}
Aresult
11
00
Truth table50%1/2
A
A= $tgt->{$key}
Aresult
11
00
89 @extra,
90 );
91 }
92  
93 101 push @ops, @new_unique, @new_fk, @new_view;
94  
95 # An index goes with a column it is on (ALTER TABLE ... DROP COLUMN): a
96 # DROP INDEX after that fails with ORA-01418, so it is not written (karr k63).
97 101 my %dropped;
98 101 for my $op (@ops) {
99 141 ▶ next unless $op->isa('DBIO::Oracle::Diff::Column') && $op->action eq 'drop';
Branch: 1 branch
BranchTrueFalseTotal
unless $op->isa("DBIO::Oracle::Diff::Column") and $op->action eq "drop" 135 6 141
Condition100%3/3
$op->isa("DBIO::Oracle::Diff::Column") and $op->action eq "drop"
!ll&&!rl&&r
108276
MC/DC100%2/2
$op->isa("DBIO::Oracle::Diff::Column") and $op->action eq "drop"
$op->isa("DBIO::Oracle::Diff::Column") $op->action eq "drop"
Truth table100%3/3
A and B
A= $op->isa("DBIO::Oracle::Diff::Column")B= $op->action eq "drop"
ABresult
0X0
100
111
100 6 $dropped{ $op->table_name }{ $op->column_name } = 1;
101 }
102 101 ▶ if (%dropped) {
Branch: 1 branch
BranchTrueFalseTotal
if (%dropped) 5 96 101
103 @ops = grep {
104 5 my $op = $_;
105 !( $op->isa('DBIO::Oracle::Diff::Index') && $op->action eq 'drop'
106 && $dropped{ $op->table_name }
107 12 ▶ && grep { $dropped{ $op->table_name }{$_} } @{ ($op->index_info // {})->{columns} // [] } );
Condition61%8/13
$op->isa('DBIO::Oracle::Diff::Index') && $op->action eq 'drop' && $dropped{$op->table_name} && grep({$dropped{$op->table_name}{$_};} @{+($op->index_info // {})->{'columns'} // [];})
!ll&&!rl&&r
1002
$op->isa("DBIO::Oracle::Diff::Index") and $op->action eq "drop" and $dropped{$op->table_name}
!ll&&!rl&&r
1002
$op->isa("DBIO::Oracle::Diff::Index") and $op->action eq "drop"
!ll&&!rl&&r
1002
+($op->index_info // {})->{"columns"} // []
l!l
20
$op->index_info // {}
l!l
20
MC/DC25%1/4
$op->isa('DBIO::Oracle::Diff::Index') && $op->action eq 'drop' && $dropped{$op->table_name} && grep({$dropped{$op->table_name}{$_};} @{+($op->index_info // {})->{'columns'} // [];})
$op->isa("DBIO::Oracle::Diff::Index") $op->action eq "drop" $dropped{$op->table_name} grep({$dropped{$op->table_name}{$_};} @{+($op->index_info // {})->{'columns'} // [];})
MC/DC0%0/1
+($op->index_info // {})->{"columns"} // []
$op->index_info
Truth table40%2/5
A and B and C && D
A= $op->isa("DBIO::Oracle::Diff::Index")B= $op->action eq "drop"C= $dropped{$op->table_name}D= grep({$dropped{$op->table_name}{$_};} @{+($op->index_info // {})->{'columns'} // [];})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
Truth table50%1/2
A
A= $op->index_info
Aresult
11
00
108 } @ops;
109 }
110  
111 101 return \@ops;
112 }
113  
114 # The foreign keys that stay as they are over a unique constraint the diff
115 # drops (ORA-02273: Oracle will not drop a constraint a foreign key rests on).
116 # Returns the drops and the creates to put around the constraint operations;
117 # throws when the target has nothing for the key to point to.
118 118 ▶sub _keys_over_gone_unique {
Subroutine _keys_over_gone_unique: called CC=21 SCAR=31.5
119 118 my ($self, $gone) = @_;
120 118 ▶ return ([], []) unless @$gone;
Branch: 1 branch
BranchTrueFalseTotal
unless @$gone 110 8 118
121 8 my ($src, $tgt) = ($self->source, $self->target);
122  
123 8 ▶ my $set = sub { join ',', sort map { lc } @{ $_[0] // [] } };
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition50%1/2
$_[0] // []
l!l
180
MC/DC0%0/1
$_[0] // []
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
124 8 my %gone = map { ($_->table_name . "\x1f" . $set->($_->columns)) => 1 } @$gone;
125  
126 8 my (@drop, @new);
127 8 ▶ for my $table (sort keys %{ $src->{foreign_keys} // {} }) {
Condition100%2/2
$src->{"foreign_keys"} // {}
l!l
62
MC/DC100%1/1
$src->{"foreign_keys"} // {}
$src->{"foreign_keys"}
Truth table100%2/2
A
A= $src->{"foreign_keys"}
Aresult
11
00
128 6 ▶ next unless exists( ($tgt->{tables} // {})->{$table} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists +($tgt->{"tables"} // {})->{$table} 0 6 6
Condition50%1/2
$tgt->{"tables"} // {}
l!l
60
MC/DC0%0/1
$tgt->{"tables"} // {}
$tgt->{"tables"}
Truth table50%1/2
A
A= $tgt->{"tables"}
Aresult
11
00
129 5 my %want = map { DBIO::Oracle::Diff::ForeignKey::_signature($_) => $_ }
130 6 ▶ @{ ($tgt->{foreign_keys} // {})->{$table} // [] };
Condition50%2/4
+($tgt->{"foreign_keys"} // {})->{$table} // []
l!l
60
$tgt->{"foreign_keys"} // {}
l!l
60
MC/DC0%0/1
+($tgt->{"foreign_keys"} // {})->{$table} // []
$tgt->{"foreign_keys"}
Truth table50%1/2
A
A= $tgt->{"foreign_keys"}
Aresult
11
00
131  
132 6 for my $fk (@{ $src->{foreign_keys}{$table} }) {
133 6 ▶ my $stays = $want{ DBIO::Oracle::Diff::ForeignKey::_signature($fk) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $stays = $want{DBIO::Oracle::Diff::ForeignKey::_signature($fk)} or next 5 1 6
134 5 my $to = $fk->{to_table};
135 5 my $cols = $set->($fk->{to_columns});
136 5 ▶ next unless $gone{ $to . "\x1f" . $cols };
Branch: 1 branch
BranchTrueFalseTotal
unless $gone{$to . "\037" . $cols} 1 4 5
137  
138 4 ▶ my $held = grep { $set->($_->[1]) eq $cols } @{ ($tgt->{unique_constraints} // {})->{$to} // [] };
Condition50%2/4
+($tgt->{"unique_constraints"} // {})->{$to} // []
l!l
40
$tgt->{"unique_constraints"} // {}
l!l
40
MC/DC0%0/1
+($tgt->{"unique_constraints"} // {})->{$to} // []
$tgt->{"unique_constraints"}
Truth table50%1/2
A
A= $tgt->{"unique_constraints"}
Aresult
11
00
139 4 ▶ $held ||= $set->(($tgt->{primary_keys} // {})->{$to}) eq $cols;
Condition80%4/5
$held ||= &$set(+($tgt->{"primary_keys"} // {})->{$to}) eq $cols
l!l&&r!l&&!r
112
$tgt->{"primary_keys"} // {}
l!l
30
MC/DC100%2/2
$held ||= &$set(+($tgt->{"primary_keys"} // {})->{$to}) eq $cols
$held &$set(+($tgt->{"primary_keys"} // {})->{$to}) eq $cols
MC/DC0%0/1
$tgt->{"primary_keys"} // {}
$tgt->{"primary_keys"}
Truth table100%3/3
A ||= B
A= $heldB= &$set(+($tgt->{"primary_keys"} // {})->{$to}) eq $cols
ABresult
1X1
011
000
Truth table50%1/2
A
A= $tgt->{"primary_keys"}
Aresult
11
00
140 4 ▶ unless ($held) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($held) 2 2 4
141 2 ▶ my @from = @{ $fk->{from_columns} // [] };
Condition50%1/2
$fk->{"from_columns"} // []
l!l
20
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
142 2 ▶ my @to = @{ $fk->{to_columns} // [] };
Condition50%1/2
$fk->{"to_columns"} // []
l!l
20
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
Aresult
11
00
143 DBIO::Exception->throw(sprintf
144 "DBIO::Oracle::Diff: the foreign key '%s' of table '%s' (column%s %s) stays, "
145 . "but it points to column%s %s of table '%s', over which the target model has "
146 . 'no unique constraint and no primary key (Oracle refuses to drop the constraint '
147 . 'under a foreign key, ORA-02273)',
148 2 ▶ $stays->{constraint_name} // '', $table, (@from > 1 ? 's' : ''), join(', ', @from),
Branch: 2 branches
BranchTrueFalseTotal
@from > 1 ? : 0 2 2
@to > 1 ? : 0 2 2
Condition50%1/2
$stays->{"constraint_name"} // ""
l!l
20
MC/DC0%0/1
$stays->{"constraint_name"} // ""
$stays->{"constraint_name"}
Truth table50%1/2
A
A= $stays->{"constraint_name"}
Aresult
11
00
149 (@to > 1 ? 's' : ''), join(', ', @to), $to);
150 }
151  
152 push @drop, DBIO::Oracle::Diff::ForeignKey->new(
153 action => 'drop', table_name => $table,
154 2 constraint_name => $fk->{constraint_name}, fk_info => $fk,
155 );
156 2 push @new, DBIO::Oracle::Diff::ForeignKey->create_op($table, $stays);
157 }
158 }
159 6 return (\@drop, \@new);
160 }
161  
162  
163 30 1;
164  
165 __END__
« Oracle/Introspect/ForeignKeys.pm ↑ Summary Oracle/Deploy.pm »