MSSQL/Diff/ForeignKey.pm

filter: Statementstmt 100% 44 / 44 Branchbran 83.3% 5 / 6 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 92.0% 69 / 75 CC 6 SCAR 10.2
CC
6
Cov
95%
as_sql
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.8
« MSSQL/Result.pm ↑ Summary MSSQL/Introspect.pm »
1 package DBIO::MSSQL::Diff::ForeignKey;
2 # ABSTRACT: Diff operations for MSSQL foreign keys
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base 'DBIO::Diff::Op';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use DBIO::SQL::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DBIO::Diff::Compare qw(changed_fields);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 3 __PACKAGE__->mk_diff_accessors(qw/table_name constraint_name fk_info/);
14  
15  
16 9 ▶sub diff {
Subroutine diff: called CC=1 SCAR=0.0
Pod documented
17 9 my ($class, $source, $target, $source_tables, $target_tables) = @_;
18  
19 return $class->diff_nested($source, $target,
20 index_by => 'constraint_name',
21 scope => 'both',
22 source_tables => $source_tables,
23 target_tables => $target_tables,
24 1 ▶ changed_when => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 scalar changed_fields($_[0], $_[1],
26 scalar => ['to_table', 'on_delete', 'on_update'],
27 dim => ['from_columns', 'to_columns']);
28 },
29 2 ▶ on_new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 my ($table, $name, $new) = @_;
31 2 $class->new(action => 'add', table_name => $table,
32 constraint_name => $name, fk_info => $new);
33 },
34 1 ▶ on_changed => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 1 my ($table, $name, $old, $new) = @_;
36 1 ($class->new(action => 'drop', table_name => $table,
37 constraint_name => $name, fk_info => $old),
38 $class->new(action => 'add', table_name => $table,
39 constraint_name => $name, fk_info => $new));
40 },
41 1 ▶ on_gone => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 1 my ($table, $name, $old) = @_;
43 1 $class->new(action => 'drop', table_name => $table,
44 constraint_name => $name, fk_info => $old);
45 },
46 9 );
47 }
48  
49  
50 4 ▶sub as_sql {
Subroutine as_sql: called CC=6 SCAR=18.0
Pod documented
51 4 my ($self) = @_;
52 4 my $tbl = _quote_ident($self->table_name);
53 4 my $name = _quote_ident($self->constraint_name);
54  
55 4 ▶ if ($self->action eq 'add') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->action eq "add") 3 1 4
56 3 my $info = $self->fk_info;
57 3 my $from = join(', ', map { _quote_ident($_) } @{ $info->{from_columns} });
58 3 my $to = join(', ', map { _quote_ident($_) } @{ $info->{to_columns} });
59 my $sql = sprintf
60 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)',
61 3 $tbl, $name, $from, _quote_ident($info->{to_table}), $to;
62 $sql .= " ON DELETE $info->{on_delete}"
63 3 ▶ 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" 1 2 3
Condition66%2/3
$info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION"
!ll&&!rl&&r
201
MC/DC50%1/2
$info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION"
$info->{"on_delete"} $info->{"on_delete"} ne "NO ACTION"
Truth table66%2/3
A and B
A= $info->{"on_delete"}B= $info->{"on_delete"} ne "NO ACTION"
ABresult
0X0
100
111
64 $sql .= " ON UPDATE $info->{on_update}"
65 3 ▶ 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 3 3
Condition66%2/3
$info->{"on_update"} and $info->{"on_update"} ne "NO ACTION"
!ll&&!rl&&r
210
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
66 3 return "$sql;";
67 }
68 1 return sprintf 'ALTER TABLE %s DROP CONSTRAINT %s;', $tbl, $name;
69 }
70  
71  
72 1 ▶sub summary {
Subroutine summary: called CC=1 SCAR=0.0
Pod documented
73 1 my ($self) = @_;
74 1 return sprintf ' %sfk: %s on %s',
75 $self->summary_prefix, $self->constraint_name, $self->table_name;
76 }
77  
78 3 1;
79  
80 __END__
81  
82 =pod
83  
84 =encoding UTF-8
85  
86 =head1 NAME
87  
88 DBIO::MSSQL::Diff::ForeignKey - Diff operations for MSSQL foreign keys
89  
90 =head1 VERSION
91  
92 version 0.900001
93  
94 =head1 DESCRIPTION
95  
96 Represents a foreign key diff operation: C<ADD CONSTRAINT> or
97 C<DROP CONSTRAINT>. FKs on a brand-new table are created inline by
98 L<DBIO::MSSQL::Diff::Table>; this module handles FK changes on tables that
99 exist in both source and target. Built on L<DBIO::Diff::Op>.
100  
101 FK identity is by C<constraint_name>. MSSQL has no C<ALTER> for a foreign
102 key, so a definition change becomes a drop-then-add pair. Local and remote
103 column lists are order-significant, so they are compared as C<dim> fields.
104  
105 =head1 METHODS
106  
107 =head2 diff
108  
109 my @ops = DBIO::MSSQL::Diff::ForeignKey->diff(
110 $source_fks, $target_fks, $source_tables, $target_tables,
111 );
112  
113 =head2 as_sql
114  
115 =head2 summary
116  
117 =head1 AUTHOR
118  
119 DBIO & DBIx::Class Authors
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 Copyright (C) 2026 DBIO Authors
124 Portions Copyright (C) 2005-2025 DBIx::Class Authors
125 Based on DBIx::Class, heavily modified.
126  
127 This is free software; you can redistribute it and/or modify it under
128 the same terms as the Perl 5 programming language system itself.
129  
130 =cut
« MSSQL/Result.pm ↑ Summary MSSQL/Introspect.pm »