| 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 // {}
MC/DC100%1/1
$source // {}
$source
Truth table100%2/2
A
A= $source
| |||||||||||||||||||||||||||||
| 24 | 96 | ▶ | $target = { %{ $target // {} } }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target // {}
MC/DC0%0/1
$target // {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||
| 25 | 96 | ▶ | $target->{$_} //= [] for keys %$source; | ||||||||||||||||||||||||||
|
Condition100%2/2
$target->{$_} //= []
MC/DC100%1/1
$target->{$_} //= []
$target->{$_}
Truth table100%2/2
A
A= $target->{$_}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION"
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"
| |||||||||||||||||||||||||||||
| 72 | $sql .= " ON UPDATE $info->{on_update}" | ||||||||||||||||||||||||||||
| 73 | 22 | ▶ | if $info->{on_update} && $info->{on_update} ne 'NO ACTION'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"on_update"} and $info->{"on_update"} ne "NO ACTION"
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"
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||