MSSQL/Diff/ForeignKey.pm

filter: Statementstmt 100% 50 / 50 Branchbran 83.3% 5 / 6 Conditioncond 83.3% 10 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 93.3% 84 / 90 CC 10 SCAR 12.2
CC
10
Cov
96%
as_sql
6.0
diff
5.0
BEGIN
1.0
CRAP 3.4
« MSSQL/Introspect/Tables.pm ↑ Summary MSSQL/SQLMaker.pm »
1 package DBIO::MSSQL::Diff::ForeignKey;
2 # ABSTRACT: Diff operations for MSSQL foreign keys
3  
4 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 38 our $VERSION = '0.900007';
8  
9 38 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 38 ▶use DBIO::MSSQL::DDL qw(_quote_ident _quote_table);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 38 __PACKAGE__->mk_diff_accessors(qw/table_name constraint_name fk_info/);
16  
17  
18 96 ▶sub diff {
Subroutine diff: called CC=5 SCAR=16.1
Pod documented
19 96 my ($class, $source, $target, $source_tables, $target_tables) = @_;
20  
21 # core visits the tables of the target only: give every table the source
22 # has foreign keys for an entry there, empty when the target has none
23 96 ▶ $source = { %{ $source // {} } };
Condition100%2/2
$source // {}
l!l
951
MC/DC100%1/1
$source // {}
$source
Truth table100%2/2
A
A= $source
Aresult
11
00
24 96 ▶ $target = { %{ $target // {} } };
Condition50%1/2
$target // {}
l!l
960
MC/DC0%0/1
$target // {}
$target
Truth table50%1/2
A
A= $target
Aresult
11
00
25 96 ▶ $target->{$_} //= [] for keys %$source;
Condition100%2/2
$target->{$_} //= []
l!l
258
MC/DC100%1/1
$target->{$_} //= []
$target->{$_}
Truth table100%2/2
A
A= $target->{$_}
Aresult
11
00
26  
27 return $class->diff_nested($source, $target,
28 index_by => 'constraint_name',
29 scope => 'all',
30 source_tables => $source_tables,
31 target_tables => $target_tables,
32 20 ▶ changed_when => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 20 scalar changed_fields($_[0], $_[1],
34 scalar => ['to_table', 'on_delete', 'on_update'],
35 dim => ['from_columns', 'to_columns']);
36 },
37 18 ▶ on_new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 18 my ($table, $name, $new) = @_;
39 18 $class->new(action => 'add', table_name => $table,
40 constraint_name => $name, fk_info => $new);
41 },
42 2 ▶ on_changed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 2 my ($table, $name, $old, $new) = @_;
44 2 ($class->new(action => 'drop', table_name => $table,
45 constraint_name => $name, fk_info => $old),
46 $class->new(action => 'add', table_name => $table,
47 constraint_name => $name, fk_info => $new));
48 },
49 12 ▶ on_gone => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 12 my ($table, $name, $old) = @_;
51 12 $class->new(action => 'drop', table_name => $table,
52 constraint_name => $name, fk_info => $old);
53 },
54 96 );
55 }
56  
57  
58 41 ▶sub as_sql {
Subroutine as_sql: called CC=6 SCAR=17.9
Pod documented
59 41 my ($self) = @_;
60 41 my $tbl = _quote_table($self->table_name);
61 41 my $name = _quote_ident($self->constraint_name);
62  
63 41 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 22 19 41
64 22 my $info = $self->fk_info;
65 22 my $from = join(', ', map { _quote_ident($_) } @{ $info->{from_columns} });
66 22 my $to = join(', ', map { _quote_ident($_) } @{ $info->{to_columns} });
67 my $sql = sprintf
68 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)',
69 22 $tbl, $name, $from, _quote_table($info->{to_table}), $to;
70 $sql .= " ON DELETE $info->{on_delete}"
71 22 ▶ if $info->{on_delete} && $info->{on_delete} ne 'NO ACTION';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION" 3 19 22
Condition100%3/3
$info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION"
!ll&&!rl&&r
6133
MC/DC100%2/2
$info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION"
$info->{"on_delete"} $info->{"on_delete"} ne "NO ACTION"
Truth table100%3/3
A and B
A= $info->{"on_delete"}B= $info->{"on_delete"} ne "NO ACTION"
ABresult
0X0
100
111
72 $sql .= " ON UPDATE $info->{on_update}"
73 22 ▶ if $info->{on_update} && $info->{on_update} ne 'NO ACTION';
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"on_update"} and $info->{"on_update"} ne "NO ACTION" 0 22 22
Condition66%2/3
$info->{"on_update"} and $info->{"on_update"} ne "NO ACTION"
!ll&&!rl&&r
6160
MC/DC0%0/2
$info->{"on_update"} and $info->{"on_update"} ne "NO ACTION"
$info->{"on_update"} $info->{"on_update"} ne "NO ACTION"
Truth table66%2/3
A and B
A= $info->{"on_update"}B= $info->{"on_update"} ne "NO ACTION"
ABresult
0X0
100
111
74 22 return "$sql;";
75 }
76 19 return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $tbl, $name;
77 }
78  
79  
80 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
81 2 my ($self) = @_;
82 2 return sprintf ' %sfk: %s on %s',
83 $self->summary_prefix, $self->constraint_name, $self->table_name;
84 }
85  
86 38 1;
87  
88 __END__
« MSSQL/Introspect/Tables.pm ↑ Summary MSSQL/SQLMaker.pm »