| 1 | package DBIO::MSSQL::Diff::PrimaryKey; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MSSQL primary keys | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 36 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 36 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 36 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 36 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 36 | ▶ | use DBIO::MSSQL::DDL qw(_quote_ident _quote_table _constraint_name); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 36 | ▶ | use DBIO::MSSQL::Diff::Table (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 36 | __PACKAGE__->mk_diff_accessors(qw/table_name constraint_name columns dropped_for_good clustered/); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # [ column names in key order ], from the column list of a table | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 180 | ▶ | sub _key_columns { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_key_columns: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 180 | my ($cols) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 101 | return map { $_->{column_name} } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | sort { ($a->{pk_position} || 0) <=> ($b->{pk_position} || 0) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$DBIO::MSSQL::Diff::PrimaryKey::a->{'pk_position'} || 0
$DBIO::MSSQL::Diff::PrimaryKey::b->{'pk_position'} || 0
MC/DC0%0/1
$DBIO::MSSQL::Diff::PrimaryKey::a->{'pk_position'} || 0
$DBIO::MSSQL::Diff::PrimaryKey::a->{'pk_position'}
MC/DC0%0/1
$DBIO::MSSQL::Diff::PrimaryKey::b->{'pk_position'} || 0
$DBIO::MSSQL::Diff::PrimaryKey::b->{'pk_position'}
Truth table50%1/2
A
A= $DBIO::MSSQL::Diff::PrimaryKey::a->{'pk_position'}
Truth table50%1/2
A
A= $DBIO::MSSQL::Diff::PrimaryKey::b->{'pk_position'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 180 | ▶ | grep { $_->{is_pk} } @{ $cols // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$cols // []
MC/DC0%0/1
$cols // []
$cols
Truth table50%1/2
A
A= $cols
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 91 | ▶ | sub diff { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
diff: called CC=23 SCAR=31.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 91 | my ($class, $source, $target, $source_tables, $target_tables, $column_ops) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 91 | ▶ | $source //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 91 | ▶ | $target //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 91 | my %is_view = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | DBIO::MSSQL::Diff::Table->view_names($source_tables, $target_tables); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 91 | my @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 91 | for my $table (sort keys %$target) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 119 | ▶ | next if $is_view{$table} || !$source->{$table}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_view{$table} or not $source->{$table}
MC/DC100%2/2
$is_view{$table} or not $source->{$table}
$is_view{$table}
not $source->{$table}
Truth table100%3/3
A or not(B)
A= $is_view{$table}B= not $source->{$table}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 90 | my @want = _key_columns($target->{$table}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 90 | my @have = _key_columns($source->{$table}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 90 | ▶ | next unless @want || @have; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@want or @have
MC/DC100%2/2
@want or @have
@want
@have
Truth table100%3/3
A or B
A= @wantB= @have
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 53 | my $changed = join("\0", @have) ne join("\0", @want); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # CLUSTERED or NONCLUSTERED: compared when both models know it. The classes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # create the key clustered (install_ddl), so a live key that is not is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # replaced -- the same way as a key with other columns. The server cannot | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # change it in place, and ADD PRIMARY KEY without the word makes the key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # NONCLUSTERED, silently, while a clustered index stands (measured). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 53 | my ($have_cl, $want_cl); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 53 | ▶ | if (@have && @want) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@have and @want
MC/DC100%2/2
@have and @want
@have
@want
Truth table100%3/3
A and B
A= @haveB= @want
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 46 | ($have_cl) = map { $_->{pk_clustered} } grep { $_->{is_pk} } @{ $source->{$table} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 46 | ($want_cl) = map { $_->{pk_clustered} } grep { $_->{is_pk} } @{ $target->{$table} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 46 | ▶ | $changed ||= defined $have_cl && defined $want_cl && !$have_cl != !$want_cl; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition88%8/9
$changed ||= defined $have_cl && defined $want_cl && !$have_cl != !$want_cl
defined $have_cl && defined $want_cl && !$have_cl != !$want_cl
defined $have_cl and defined $want_cl
MC/DC0%0/4
$changed ||= defined $have_cl && defined $want_cl && !$have_cl != !$want_cl
$changed
defined $have_cl
defined $want_cl
!$have_cl != !$want_cl
Truth table0%0/5
A ||= B and C && not(D)
A= $changedB= defined $have_clC= defined $want_clD= !$have_cl != !$want_cl
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 53 | ▶ | if (!$changed && @have) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $changed and @have
MC/DC50%1/2
not $changed and @have
not $changed
@have
Truth table66%2/3
not(A) and B
A= not $changedB= @have
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 37 | my %key = map { $_ => 1 } @have; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | $changed = grep { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 14 | ▶ | $_->table_name eq $table && $key{ $_->column_name } && $_->unbinds_dependents | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$_->table_name eq $table && $key{$_->column_name} && $_->unbinds_dependents
$_->table_name eq $table and $key{$_->column_name}
MC/DC100%3/3
$_->table_name eq $table && $key{$_->column_name} && $_->unbinds_dependents
$_->table_name eq $table
$key{$_->column_name}
$_->unbinds_dependents
Truth table100%4/4
A and B && C
A= $_->table_name eq $tableB= $key{$_->column_name}C= $_->unbinds_dependents
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 37 | ▶ | } @{ $column_ops // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$column_ops // []
MC/DC0%0/1
$column_ops // []
$column_ops
Truth table50%1/2
A
A= $column_ops
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 53 | ▶ | next unless $changed; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 17 | ▶ | if (@have) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 14 | my ($live) = grep { $_->{is_pk} } @{ $source->{$table} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 14 | my $name = $live->{pk_constraint_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 14 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||||||||||||||||||||||
|
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | "DBIO::MSSQL: the primary key of table '%s' (%s) cannot be dropped: " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | . "Transact-SQL drops a key by constraint name and the live column " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | . "info carries no pk_constraint_name", $table, join(', ', @have)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | unless defined $name && length $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 13 | push @ops, $class->new(action => 'drop', table_name => $table, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | constraint_name => $name, columns => [ @have ], dropped_for_good => !@want); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 16 | ▶ | next unless @want; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 12 | push @ops, $class->new(action => 'create', table_name => $table, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | constraint_name => _constraint_name('PK', $table), columns => [ @want ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | clustered => $want_cl); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 90 | return @ops; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 21 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 21 | my $tbl = _quote_table($self->table_name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 21 | my $name = _quote_ident($self->constraint_name); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 21 | ▶ | return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $tbl, $name | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | if $self->action eq 'drop'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | ▶ | my $cl = !defined $self->clustered ? '' | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | : $self->clustered ? 'CLUSTERED ' : 'NONCLUSTERED '; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY %s(%s);', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 10 | $tbl, $name, $cl, join ', ', map { _quote_ident($_) } @{ $self->columns }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub summary { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summary: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | return sprintf ' %sPRIMARY KEY DROPPED: %s (%s) -- the classes declare no primary key for it', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | $self->summary_prefix, $self->table_name, join ', ', @{ $self->columns } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | if $self->dropped_for_good; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | return sprintf ' %sprimary key: %s (%s)', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $self->summary_prefix, $self->table_name, join ', ', @{ $self->columns }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 36 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||