Sybase/Diff/ForeignKey.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 14 / 14 Conditioncond 80.0% 24 / 30 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 1 totaltotal 91.8% 146 / 159 CC 24 SCAR 23.0
CC
24
Cov
95%
diff
18.0
_fk_key
4.4
_fk_name
4.1
CRAP 10.0
« Sybase/Storage/ASE/LOBWriter.pm ↑ Summary Sybase/Introspect/Columns.pm »
1 package DBIO::Sybase::Diff::ForeignKey;
2 # ABSTRACT: Diff Sybase ASE foreign keys
3  
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 our $VERSION = '0.900007';
8  
9 32 ▶use DBIO::Diff::Compare qw(changed_fk_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use DBIO::Sybase::Quote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use DBIO::Sybase::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # Stable identity for a per-constraint FK entry: the relationship tuple.
15 # on_update / on_delete are attributes, not identity.
16 23 ▶sub _fk_key {
Subroutine _fk_key: called CC=4 SCAR=14.9
17 23 my ($fk) = @_;
18 return join "\0",
19 23 ▶ join(',', @{ $fk->{from_columns} // [] }),
Condition50%2/4
$fk->{"from_columns"} // []
l!l
230
$fk->{"to_table"} // ""
l!l
230
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC0%0/1
$fk->{"to_table"} // ""
$fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
Truth table50%1/2
A
A= $fk->{"to_table"}
Aresult
11
00
20 ($fk->{to_table} // ''),
21 23 ▶ join(',', @{ $fk->{to_columns} // [] });
Condition50%1/2
$fk->{"to_columns"} // []
l!l
230
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
Aresult
11
00
22 }
23  
24 # Constraint name to emit for an FK entry. Prefers the real, server-assigned
25 # name carried on the entry (present on source/introspected entries, absent on
26 # desired/target entries), so DROP/ALTER target a foreign key by its actual
27 # name even when it was created out-of-band. Falls back to a deterministic name
28 # derived from the relationship tuple when no live name is present (every
29 # CREATE, plus any DROP whose source entry lacks a name).
30 42 ▶sub _fk_name {
Subroutine _fk_name: called CC=4 SCAR=14.1
31 42 my ($table, $fk) = @_;
32 return $fk->{constraint_name}
33 42 ▶ if defined $fk->{constraint_name} && length $fk->{constraint_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $fk->{"constraint_name"} and length $fk->{"constraint_name"} 9 33 42
Condition66%2/3
defined $fk->{"constraint_name"} and length $fk->{"constraint_name"}
!ll&&!rl&&r
3309
MC/DC50%1/2
defined $fk->{"constraint_name"} and length $fk->{"constraint_name"}
defined $fk->{"constraint_name"} length $fk->{"constraint_name"}
Truth table66%2/3
A and B
A= defined $fk->{"constraint_name"}B= length $fk->{"constraint_name"}
ABresult
0X0
100
111
34 33 ▶ return sprintf 'fk_%s_%s', $table, join('_', @{ $fk->{from_columns} // [] });
Condition50%1/2
$fk->{"from_columns"} // []
l!l
330
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Aresult
11
00
35 }
36  
37 66 ▶sub diff {
Subroutine diff: called CC=18 SCAR=28.9
Pod undocumented
38 66 my ($class, $source, $target, $source_tables, $target_tables) = @_;
39 66 ▶ $source //= {};
Condition100%2/2
$source //= {}
l!l
651
MC/DC100%1/1
$source //= {}
$source
Truth table100%2/2
A
A= $source
Aresult
11
00
40 66 ▶ $target //= {};
Condition100%2/2
$target //= {}
l!l
651
MC/DC100%1/1
$target //= {}
$target
Truth table100%2/2
A
A= $target
Aresult
11
00
41 66 my @ops;
42  
43 66 ▶ my $by_table = defined $source_tables && defined $target_tables;
Condition66%2/3
defined $source_tables && defined $target_tables
!ll&&!rl&&r
13053
MC/DC50%1/2
defined $source_tables && defined $target_tables
defined $source_tables defined $target_tables
Truth table66%2/3
A && B
A= defined $source_tablesB= defined $target_tables
ABresult
0X0
100
111
44 66 ▶ my @tables = $by_table ? sort(keys %$target_tables) : sort(keys %$target);
Branch: 1 branch
BranchTrueFalseTotal
$by_table ? : 53 13 66
45  
46 # FK adds (and the create half of an alter) go in the target's tables.
47 66 for my $table (@tables) {
48 77 ▶ if ($by_table && !exists $source_tables->{$table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($by_table and not exists $source_tables->{$table}) 6 71 77
Condition100%3/3
$by_table and not exists $source_tables->{$table}
!ll&&!rl&&r
13586
MC/DC100%2/2
$by_table and not exists $source_tables->{$table}
$by_table not exists $source_tables->{$table}
Truth table100%3/3
A and not(B)
A= $by_tableB= not exists $source_tables->{$table}
ABresult
0X0
100
111
49 # A new table: its CREATE TABLE carries no FK, they are all added here.
50 push @ops, map {
51 1 DBIO::Sybase::Diff::ForeignKey::Create->new(
52 action => 'create', table => $table, fk => $_,
53 )
54 6 ▶ } @{ $target->{$table} // [] };
Condition100%2/2
$target->{$table} // []
l!l
15
MC/DC100%1/1
$target->{$table} // []
$target->{$table}
Truth table100%2/2
A
A= $target->{$table}
Aresult
11
00
55 6 next;
56 }
57 71 ▶ next unless $by_table || exists $source->{$table};
Branch: 1 branch
BranchTrueFalseTotal
unless $by_table or exists $source->{$table} 1 70 71
Condition100%3/3
$by_table or exists $source->{$table}
l!l&&r!l&&!r
58121
MC/DC100%2/2
$by_table or exists $source->{$table}
$by_table exists $source->{$table}
Truth table100%3/3
A or B
A= $by_tableB= exists $source->{$table}
ABresult
1X1
011
000
58  
59 70 ▶ my %s_idx = map { _fk_key($_) => $_ } @{ $source->{$table} // [] };
Condition100%2/2
$source->{$table} // []
l!l
1654
MC/DC100%1/1
$source->{$table} // []
$source->{$table}
Truth table100%2/2
A
A= $source->{$table}
Aresult
11
00
60 70 ▶ my %t_idx = map { _fk_key($_) => $_ } @{ $target->{$table} // [] };
Condition100%2/2
$target->{$table} // []
l!l
1654
MC/DC100%1/1
$target->{$table} // []
$target->{$table}
Truth table100%2/2
A
A= $target->{$table}
Aresult
11
00
61  
62 70 for my $key (sort keys %t_idx) {
63 13 ▶ if (exists $s_idx{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s_idx{$key}) { } 4 9 13
64 # Same relationship, possibly different ON UPDATE/DELETE: drop+create.
65 push @ops,
66 DBIO::Sybase::Diff::ForeignKey::Drop->new(
67 action => 'drop', table => $table, fk => $s_idx{$key},
68 ),
69 DBIO::Sybase::Diff::ForeignKey::Create->new(
70 action => 'create', table => $table, fk => $t_idx{$key},
71 )
72 4 ▶ if changed_fk_fields($s_idx{$key}, $t_idx{$key});
Branch: 1 branch
BranchTrueFalseTotal
if changed_fk_fields($s_idx{$key}, $t_idx{$key}) 2 2 4
73 }
74 else {
75 push @ops, DBIO::Sybase::Diff::ForeignKey::Create->new(
76 9 action => 'create', table => $table, fk => $t_idx{$key},
77 );
78 }
79 }
80  
81 70 for my $key (sort keys %s_idx) {
82 push @ops, DBIO::Sybase::Diff::ForeignKey::Drop->new(
83 action => 'drop', table => $table, fk => $s_idx{$key},
84 10 ▶ ) unless exists $t_idx{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $t_idx{$key} 6 4 10
85 }
86 }
87  
88 66 return @ops;
89 }
90  
91 package DBIO::Sybase::Diff::ForeignKey::Create;
92 32 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 32 __PACKAGE__->mk_diff_accessors(qw(table fk));
95  
96 13 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
97 13 my $self = shift;
98 13 my $fk = $self->fk;
99 13 my $tbl = DBIO::Sybase::Quote::quote_ident($self->table);
100 13 my $name = DBIO::Sybase::Quote::quote_ident(
101 DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $fk));
102 13 my $from = join ', ', map { DBIO::Sybase::Quote::quote_ident($_) } @{ $fk->{from_columns} };
103 13 my $to = join ', ', map { DBIO::Sybase::Quote::quote_ident($_) } @{ $fk->{to_columns} };
104 13 my $ref = DBIO::Sybase::Quote::quote_ident($fk->{to_table});
105  
106 # ASE has no referential actions (Msg 156): refuse, never append ON ..
107 13 my $what = "foreign key '"
108 . DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $fk) . "'";
109 13 DBIO::Sybase::DDL::_check_fk_action($what, $self->table, 'ON UPDATE', $fk->{on_update}, $fk->{from_columns});
110 11 DBIO::Sybase::DDL::_check_fk_action($what, $self->table, 'ON DELETE', $fk->{on_delete}, $fk->{from_columns});
111  
112 9 my $sql = sprintf
113 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)',
114 $tbl, $name, $from, $ref, $to;
115 9 return "$sql;";
116 }
117 2 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
118 2 my $self = shift;
119 2 'ADD FK ' . DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $self->fk)
120 . ' ON ' . $self->table;
121 }
122  
123 package DBIO::Sybase::Diff::ForeignKey::Drop;
124 32 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 32 __PACKAGE__->mk_diff_accessors(qw(table fk));
127  
128 10 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
129 10 my $self = shift;
130 10 my $tbl = DBIO::Sybase::Quote::quote_ident($self->table);
131 10 my $name = DBIO::Sybase::Quote::quote_ident(
132 DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $self->fk));
133 10 return "ALTER TABLE $tbl DROP CONSTRAINT $name;";
134 }
135 3 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
136 3 my $self = shift;
137 3 'DROP FK ' . DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $self->fk)
138 . ' ON ' . $self->table;
139 }
140  
141 32 1;
142  
143 __END__
« Sybase/Storage/ASE/LOBWriter.pm ↑ Summary Sybase/Introspect/Columns.pm »