| 1 | package DBIO::Oracle::Diff::Unique; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for the unique constraints of Oracle tables | ||||||||||||||||||||||||||||||||||||||
| 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::Op'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | 30 | ▶ | use DBIO::Oracle::Identifier (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | 30 | __PACKAGE__->mk_diff_accessors(qw(table_name constraint_name columns)); | |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||
| 17 | 118 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=17 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 18 | 118 | my ($class, $source, $target, $source_tables, $target_tables, $target_columns) = @_; | |||||||||||||||||||||||||||||||||||||
| 19 | 118 | ▶ | $source ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source ||= {}
MC/DC0%0/1
$source ||= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||
| 20 | 118 | ▶ | $target ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target ||= {}
MC/DC0%0/1
$target ||= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||
| 21 | 118 | ▶ | $source_tables ||= $source; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$source_tables ||= $source
MC/DC0%0/2
$source_tables ||= $source
$source_tables
$source
Truth table33%1/3
A ||= B
A= $source_tablesB= $source
| |||||||||||||||||||||||||||||||||||||||
| 22 | 118 | ▶ | $target_tables ||= $target; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$target_tables ||= $target
MC/DC0%0/2
$target_tables ||= $target
$target_tables
$target
Truth table33%1/3
A ||= B
A= $target_tablesB= $target
| |||||||||||||||||||||||||||||||||||||||
| 23 | 118 | ▶ | $target_columns ||= {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target_columns ||= {}
MC/DC0%0/1
$target_columns ||= {}
$target_columns
Truth table50%1/2
A
A= $target_columns
| |||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 118 | ▶ | my $sig = sub { join "\x1f", @{ $_[0][1] // [] } }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Condition50%1/2
$_[0][1] // []
MC/DC0%0/1
$_[0][1] // []
$_[0][1]
Truth table50%1/2
A
A= $_[0][1]
| |||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 118 | my @ops; | |||||||||||||||||||||||||||||||||||||
| 28 | 118 | my %walk = map { $_ => 1 } keys %$source, keys %$target; | |||||||||||||||||||||||||||||||||||||
| 29 | 118 | for my $table (sort keys %walk) { | |||||||||||||||||||||||||||||||||||||
| 30 | 38 | ▶ | next unless exists $source_tables->{$table} && exists $target_tables->{$table}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $source_tables->{$table} and exists $target_tables->{$table}
MC/DC100%2/2
exists $source_tables->{$table} and exists $target_tables->{$table}
exists $source_tables->{$table}
exists $target_tables->{$table}
Truth table100%3/3
A and B
A= exists $source_tables->{$table}B= exists $target_tables->{$table}
| |||||||||||||||||||||||||||||||||||||||
| 31 | 29 | ▶ | my @src = @{ $source->{$table} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$source->{$table} // []
MC/DC100%1/1
$source->{$table} // []
$source->{$table}
Truth table100%2/2
A
A= $source->{$table}
| |||||||||||||||||||||||||||||||||||||||
| 32 | 29 | ▶ | my @tgt = @{ $target->{$table} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$target->{$table} // []
MC/DC100%1/1
$target->{$table} // []
$target->{$table}
Truth table100%2/2
A
A= $target->{$table}
| |||||||||||||||||||||||||||||||||||||||
| 33 | 29 | my %have = map { $sig->($_) => 1 } @src; | |||||||||||||||||||||||||||||||||||||
| 34 | 29 | my %want = map { $sig->($_) => 1 } @tgt; | |||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | # a constraint over a column the target drops goes with the column | ||||||||||||||||||||||||||||||||||||||
| 37 | my $kept = $target_columns->{$table} | ||||||||||||||||||||||||||||||||||||||
| 38 | 29 | ▶ | ? { map { lc($_->{column_name}) => 1 } @{ $target_columns->{$table} } } : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | 29 | for my $uc (grep { !$want{ $sig->($_) } } @src) { | |||||||||||||||||||||||||||||||||||||
| 41 | 9 | ▶ | next if $kept && grep { !$kept->{ lc $_ } } @{ $uc->[1] // [] }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$kept and grep {not $$kept{lc $_};} @{$uc->[1] // [];}
$uc->[1] // []
MC/DC50%1/2
$kept and grep {not $$kept{lc $_};} @{$uc->[1] // [];}
$kept
grep {not $$kept{lc $_};} @{$uc->[1] // [];}
MC/DC0%0/1
$uc->[1] // []
$uc->[1]
Truth table66%2/3
A and B
A= $keptB= grep {not $$kept{lc $_};} @{$uc->[1] // [];}
Truth table50%1/2
A
A= $uc->[1]
| |||||||||||||||||||||||||||||||||||||||
| 42 | 8 | push @ops, $class->new( | |||||||||||||||||||||||||||||||||||||
| 43 | action => 'drop', table_name => $table, | ||||||||||||||||||||||||||||||||||||||
| 44 | constraint_name => $uc->[0], columns => $uc->[1], | ||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||
| 47 | 29 | for my $uc (grep { !$have{ $sig->($_) } } @tgt) { | |||||||||||||||||||||||||||||||||||||
| 48 | 6 | push @ops, $class->new( | |||||||||||||||||||||||||||||||||||||
| 49 | action => 'create', table_name => $table, | ||||||||||||||||||||||||||||||||||||||
| 50 | constraint_name => $uc->[0], columns => $uc->[1], | ||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||
| 54 | 118 | return @ops; | |||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | 11 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 59 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 60 | 11 | my $q = \&DBIO::Oracle::Identifier::quote; | |||||||||||||||||||||||||||||||||||||
| 61 | 11 | my $tbl = DBIO::Oracle::Identifier::quote_table($self->table_name); | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | 11 | ▶ | return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $tbl, $q->($self->constraint_name) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 64 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s);', | ||||||||||||||||||||||||||||||||||||||
| 67 | 5 | $tbl, $q->($self->constraint_name), join(', ', map { $q->($_) } @{ $self->columns }); | |||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 73 | 0 | return sprintf ' %sunique constraint: %s on %s', | |||||||||||||||||||||||||||||||||||||
| 74 | $self->summary_prefix, $self->constraint_name, $self->table_name; | ||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | 30 | 1; | |||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||||||||||||