| 1 | package DBIO::DB2::Diff::UniqueConstraint; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for DB2 unique constraints | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 45 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 45 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 45 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 45 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 45 | ▶ | use DBIO::DB2::DDL qw(_unique_constraint_clause _db2_quote_ident _db2_quote_table); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 45 | __PACKAGE__->mk_diff_accessors(qw/table_name constraint_name columns/); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 144 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 144 | my ($class, $source, $target, $source_tables, $target_tables, $source_columns, $target_columns) = @_; | |||||||||||||||||||||||||||
| 19 | 144 | ▶ | $source //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$source //= {}
MC/DC100%1/1
$source //= {}
$source
Truth table100%2/2
A
A= $source
| |||||||||||||||||||||||||||||
| 20 | 144 | ▶ | $target //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target //= {}
MC/DC100%1/1
$target //= {}
$target
Truth table100%2/2
A
A= $target
| |||||||||||||||||||||||||||||
| 21 | 144 | ▶ | $source_tables //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$source_tables //= {}
MC/DC0%0/1
$source_tables //= {}
$source_tables
Truth table50%1/2
A
A= $source_tables
| |||||||||||||||||||||||||||||
| 22 | 144 | ▶ | $target_tables //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target_tables //= {}
MC/DC0%0/1
$target_tables //= {}
$target_tables
Truth table50%1/2
A
A= $target_tables
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # diff_nested walks the tables of the target section: give every table the | ||||||||||||||||||||||||||||
| 25 | # models share an entry on both sides, so a table whose last constraint left | ||||||||||||||||||||||||||||
| 26 | # the schema still gets its drop. | ||||||||||||||||||||||||||||
| 27 | # Db2 drops a UNIQUE constraint together with a column it covers (DROP COLUMN), so | ||||||||||||||||||||||||||||
| 28 | # ALTER TABLE ... DROP UNIQUE behind it finds nothing (SQL0204N, karr k57): no | ||||||||||||||||||||||||||||
| 29 | # drop is written for a constraint over a column this pass drops. | ||||||||||||||||||||||||||||
| 30 | 6 | ▶ | my $dropped = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 31 | 6 | my ($table, $cols) = @_; | |||||||||||||||||||||||||||
| 32 | 6 | ▶ | return 0 unless $source_columns && $source_columns->{$table}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$source_columns and $source_columns->{$table}
MC/DC50%1/2
$source_columns and $source_columns->{$table}
$source_columns
$source_columns->{$table}
Truth table66%2/3
A and B
A= $source_columnsB= $source_columns->{$table}
| |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | my %kept = map { $_->{column_name} => 1 } @{ ($target_columns // {})->{$table} // [] }; | ||||||||||||||||||||||||||
|
Condition50%2/4
+($target_columns // {})->{$table} // []
$target_columns // {}
MC/DC0%0/1
+($target_columns // {})->{$table} // []
$target_columns
Truth table50%1/2
A
A= $target_columns
| |||||||||||||||||||||||||||||
| 34 | 1 | my %gone = map { $_->{column_name} => 1 } | |||||||||||||||||||||||||||
| 35 | 3 | grep { !$kept{ $_->{column_name} } } @{ $source_columns->{$table} }; | |||||||||||||||||||||||||||
| 36 | 3 | ▶ | return (grep { $gone{$_} } @$cols) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 144 | }; | |||||||||||||||||||||||||||
| 38 | 144 | my (%src, %tgt); | |||||||||||||||||||||||||||
| 39 | 144 | for my $table (grep { exists $target_tables->{$_} } keys %$source_tables) { | |||||||||||||||||||||||||||
| 40 | 160 | $src{$table} = _by_name($source->{$table}); | |||||||||||||||||||||||||||
| 41 | 160 | $tgt{$table} = _by_name($target->{$table}); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | return $class->diff_nested(\%src, \%tgt, | ||||||||||||||||||||||||||||
| 45 | scope => 'both', | ||||||||||||||||||||||||||||
| 46 | source_tables => $source_tables, | ||||||||||||||||||||||||||||
| 47 | target_tables => $target_tables, | ||||||||||||||||||||||||||||
| 48 | 4 | ▶ | changed_when => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 4 | join("\0", @{ $_[0]{columns} }) ne join("\0", @{ $_[1]{columns} }); | |||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||
| 51 | 3 | ▶ | on_new => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 3 | my ($table, $name, $new) = @_; | |||||||||||||||||||||||||||
| 53 | $class->new(action => 'add', table_name => $table, | ||||||||||||||||||||||||||||
| 54 | 3 | constraint_name => $name, columns => $new->{columns}); | |||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | 1 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 57 | 1 | my ($table, $name, $old, $new) = @_; | |||||||||||||||||||||||||||
| 58 | ($dropped->($table, $old->{columns}) ? () : | ||||||||||||||||||||||||||||
| 59 | $class->new(action => 'drop', table_name => $table, | ||||||||||||||||||||||||||||
| 60 | constraint_name => $name, columns => $old->{columns}), | ||||||||||||||||||||||||||||
| 61 | $class->new(action => 'add', table_name => $table, | ||||||||||||||||||||||||||||
| 62 | 1 | ▶ | constraint_name => $name, columns => $new->{columns})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||
| 64 | 5 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 5 | my ($table, $name, $old) = @_; | |||||||||||||||||||||||||||
| 66 | 5 | ▶ | return if $dropped->($table, $old->{columns}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | $class->new(action => 'drop', table_name => $table, | ||||||||||||||||||||||||||||
| 68 | 4 | constraint_name => $name, columns => $old->{columns}); | |||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||
| 70 | 144 | ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 320 | ▶ | sub _by_name { | ||||||||||||||||||||||||||
|
Subroutine
_by_name: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 74 | 320 | my ($pairs) = @_; | |||||||||||||||||||||||||||
| 75 | 320 | ▶ | return { map { $_->[0] => { constraint_name => $_->[0], columns => $_->[1] } } @{ $pairs || [] } }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$pairs || []
MC/DC100%1/1
$pairs || []
$pairs
Truth table100%2/2
A
A= $pairs
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 6 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 81 | 6 | my $tbl = _db2_quote_table($self->table_name); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 6 | ▶ | if ($self->action eq 'add') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 3 | return sprintf 'ALTER TABLE %s ADD %s;', $tbl, | |||||||||||||||||||||||||||
| 85 | _unique_constraint_clause($self->constraint_name, $self->columns); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 3 | return sprintf 'ALTER TABLE %s DROP UNIQUE %s;', $tbl, | |||||||||||||||||||||||||||
| 88 | _db2_quote_ident($self->constraint_name); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 94 | 0 | return sprintf ' %sunique: %s on %s', | |||||||||||||||||||||||||||
| 95 | $self->summary_prefix, $self->constraint_name, $self->table_name; | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 45 | 1; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||