MSSQL/Diff/PrimaryKey.pm

filter: Statementstmt 97.5% 78 / 80 Branchbran 91.6% 22 / 24 Conditioncond 78.5% 33 / 42 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 86.2% 157 / 182 CC 30 SCAR 26.9
CC
30
Cov
91%
diff
23.1
_key_columns
4.2
as_sql
4.0
CRAP 14.8
« MSSQL/Introspect/Views.pm ↑ Summary MSSQL/Util.pm »
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
l!l
20
$DBIO::MSSQL::Diff::PrimaryKey::b->{'pk_position'} || 0
l!l
20
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'}
Aresult
11
00
Truth table50%1/2
A
A= $DBIO::MSSQL::Diff::PrimaryKey::b->{'pk_position'}
Aresult
11
00
23 180 ▶ grep { $_->{is_pk} } @{ $cols // [] };
Condition50%1/2
$cols // []
l!l
1800
MC/DC0%0/1
$cols // []
$cols
Truth table50%1/2
A
A= $cols
Aresult
11
00
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 //= {}
l!l
910
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
30 91 ▶ $target //= {};
Condition50%1/2
$target //= {}
l!l
910
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
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
BranchTrueFalseTotal
if $is_view{$table} or not $source->{$table} 29 90 119
Condition100%3/3
$is_view{$table} or not $source->{$table}
l!l&&r!l&&!r
101990
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}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless @want or @have 37 53 90
Condition100%3/3
@want or @have
l!l&&r!l&&!r
49437
MC/DC100%2/2
@want or @have
@want @have
Truth table100%3/3
A or B
A= @wantB= @have
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (@have and @want) 46 7 53
Condition100%3/3
@have and @want
!ll&&!rl&&r
3446
MC/DC100%2/2
@have and @want
@have @want
Truth table100%3/3
A and B
A= @haveB= @want
ABresult
0X0
100
111
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
l!l&&r!l&&!r
6337
defined $have_cl && defined $want_cl && !$have_cl != !$want_cl
!ll&&!rl&&r
3523
defined $have_cl and defined $want_cl
!ll&&!rl&&r
3505
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
ABCDresult
1XXX1
01111
00XX0
010X0
01100
53 }
54 53 ▶ if (!$changed && @have) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $changed and @have) 37 16 53
Condition66%2/3
not $changed and @have
!ll&&!rl&&r
16037
MC/DC50%1/2
not $changed and @have
not $changed @have
Truth table66%2/3
not(A) and B
A= not $changedB= @have
ABresult
0X0
100
111
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
!ll&&!rl&&r
1121
$_->table_name eq $table and $key{$_->column_name}
!ll&&!rl&&r
653
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
ABCresult
0XX0
10X0
1100
1111
58 37 ▶ } @{ $column_ops // [] };
Condition50%1/2
$column_ops // []
l!l
370
MC/DC0%0/1
$column_ops // []
$column_ops
Truth table50%1/2
A
A= $column_ops
Aresult
11
00
59 }
60 53 ▶ next unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 36 17 53
61  
62 17 ▶ if (@have) {
Branch: 1 branch
BranchTrueFalseTotal
if (@have) 14 3 17
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
BranchTrueFalseTotal
unless defined $name and length $name 1 13 14
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
1013
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless @want 4 12 16
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
BranchTrueFalseTotal
if $self->action eq "drop" 11 10 21
87 if $self->action eq 'drop';
88 10 ▶ my $cl = !defined $self->clustered ? ''
Branch: 2 branches
BranchTrueFalseTotal
!defined($self->clustered) ? : 7 3 10
$self->clustered ? : 3 0 3
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
BranchTrueFalseTotal
if $self->dropped_for_good 1 0 1
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__
« MSSQL/Introspect/Views.pm ↑ Summary MSSQL/Util.pm »