| 1 | package DBIO::Informix::Diff::PrimaryKey; | ||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for Informix primary keys | ||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 24 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 24 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | 24 | ▶ | use DBIO::Informix::Util qw(_quote_ident); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||
| 13 | 24 | __PACKAGE__->mk_diff_accessors(qw(table_name constraint_name columns replaced)); | |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||
| 16 | 136 | ▶ | sub _pk_columns { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_pk_columns: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||
| 17 | 136 | my ($cols) = @_; | |||||||||||||||||||||||||||||||||||
| 18 | 29 | return [ map { $_->{column_name} } | |||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | sort { ($a->{pk_position} // 0) <=> ($b->{pk_position} // 0) } | ||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$DBIO::Informix::Diff::PrimaryKey::a->{"pk_position"} // 0
$DBIO::Informix::Diff::PrimaryKey::b->{"pk_position"} // 0
MC/DC0%0/1
$DBIO::Informix::Diff::PrimaryKey::a->{"pk_position"} // 0
$DBIO::Informix::Diff::PrimaryKey::a->{"pk_position"}
MC/DC0%0/1
$DBIO::Informix::Diff::PrimaryKey::b->{"pk_position"} // 0
$DBIO::Informix::Diff::PrimaryKey::b->{"pk_position"}
Truth table50%1/2
A
A= $DBIO::Informix::Diff::PrimaryKey::a->{"pk_position"}
Truth table50%1/2
A
A= $DBIO::Informix::Diff::PrimaryKey::b->{"pk_position"}
| |||||||||||||||||||||||||||||||||||||
| 20 | 136 | ▶ | grep { $_->{is_pk} } @{ $cols // [] } ]; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cols // []
MC/DC0%0/1
$cols // []
$cols
Truth table50%1/2
A
A= $cols
| |||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | 51 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 25 | 51 | my ($class, $source_cols, $target_cols, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||||||||||
| 26 | 51 | ▶ | $_ //= {} for $source_cols, $target_cols, $source_tables, $target_tables; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_ //= {}
MC/DC0%0/1
$_ //= {}
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | 51 | my @ops; | |||||||||||||||||||||||||||||||||||
| 29 | 51 | for my $table (sort keys %$source_tables) { | |||||||||||||||||||||||||||||||||||
| 30 | 81 | ▶ | next unless exists $target_tables->{$table}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 31 | next if ($source_tables->{$table}{kind} // '') eq 'view' | ||||||||||||||||||||||||||||||||||||
| 32 | 72 | ▶ | || ($target_tables->{$table}{kind} // '') eq 'view'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%7/7
($source_tables->{$table}{"kind"} // "") eq "view" or ($target_tables->{$table}{"kind"} // "") eq "view"
$source_tables->{$table}{"kind"} // ""
$target_tables->{$table}{"kind"} // ""
MC/DC50%1/2
($source_tables->{$table}{"kind"} // "") eq "view" or ($target_tables->{$table}{"kind"} // "") eq "view"
$source_tables->{$table}{"kind"}
$target_tables->{$table}{"kind"}
Truth table66%2/3
A or B
A= $source_tables->{$table}{"kind"}B= $target_tables->{$table}{"kind"}
| |||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | 68 | my ($have, $want) = (_pk_columns($source_cols->{$table}), _pk_columns($target_cols->{$table})); | |||||||||||||||||||||||||||||||||||
| 35 | 68 | ▶ | next if join("\0", @$have) eq join("\0", @$want); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | push @ops, $class->new(action => 'drop', table_name => $table, | ||||||||||||||||||||||||||||||||||||
| 38 | 10 | ▶ | constraint_name => $source_tables->{$table}{primary_key_name}, columns => $have, | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 39 | replaced => (@$want ? 1 : 0)) if @$have; | ||||||||||||||||||||||||||||||||||||
| 40 | 10 | ▶ | push @ops, $class->new(action => 'add', table_name => $table, columns => $want) if @$want; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||
| 42 | 51 | return @ops; | |||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | 12 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 47 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||
| 48 | 12 | my $tbl = _quote_ident($self->table_name); | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 12 | ▶ | if ($self->action eq 'drop') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 51 | 6 | my $name = $self->constraint_name; | |||||||||||||||||||||||||||||||||||
| 52 | 6 | ▶ | die sprintf "Informix primary key of table %s cannot be dropped: the model holds no name for it (primary_key_name of the table in the tables section)\n", | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $name and length $name
MC/DC50%1/2
defined $name and length $name
defined $name
length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||||||||||
| 53 | $self->table_name unless defined $name && length $name; | ||||||||||||||||||||||||||||||||||||
| 54 | 5 | return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $tbl, _quote_ident($name); | |||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||
| 57 | return sprintf 'ALTER TABLE %s ADD CONSTRAINT PRIMARY KEY (%s);', $tbl, | ||||||||||||||||||||||||||||||||||||
| 58 | 6 | join(', ', map { _quote_ident($_) } @{ $self->columns }); | |||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||
| 64 | return sprintf '%s primary key: (%s) on %s', $self->summary_prefix, | ||||||||||||||||||||||||||||||||||||
| 65 | 0 | join(', ', @{ $self->columns }), $self->table_name; | |||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | 24 | 1; | |||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | __END__ | ||||||||||||||||||||||||||||||||||||