| 1 | package DBIO::MySQL::Diff::ForeignKey; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MySQL/MariaDB foreign keys | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 57 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 57 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 57 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 57 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 57 | ▶ | use DBIO::Diff::Compare qw(changed_fk_fields); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 57 | ▶ | use DBIO::Exception (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 57 | ▶ | use DBIO::MySQL::Identifier qw(quote_ident quote_table_in); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 57 | ▶ | use DBIO::MySQL::DDL (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 57 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||
| 17 | table_name constraint_name fk_info database replaced | ||||||||||||||||||||||||||||
| 18 | )); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 176 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=10 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 176 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||
| 24 | 176 | ▶ | $source //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||
| 25 | 176 | ▶ | $target //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # diff_nested walks the tables that are keys of the target it is given, and a | ||||||||||||||||||||||||||||
| 28 | # table without a foreign key is not one: neither the introspection nor | ||||||||||||||||||||||||||||
| 29 | # target_from_compiled gives it an entry. A table whose last foreign key is | ||||||||||||||||||||||||||||
| 30 | # gone would so never be looked at, and its DROP FOREIGN KEY never written | ||||||||||||||||||||||||||||
| 31 | # (karr k86). So every table that had foreign keys gets an entry here, an | ||||||||||||||||||||||||||||
| 32 | # empty one unless the target has its own. Which of them is walked is still | ||||||||||||||||||||||||||||
| 33 | # decided by the tables of the two models (scope 'both'): not a table the | ||||||||||||||||||||||||||||
| 34 | # diff creates, and not one it drops. | ||||||||||||||||||||||||||||
| 35 | 176 | my %target = ((map { $_ => [] } keys %$source), %$target); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my @ops = $class->diff_nested( | ||||||||||||||||||||||||||||
| 38 | $source, \%target, | ||||||||||||||||||||||||||||
| 39 | index_by => 'constraint_name', | ||||||||||||||||||||||||||||
| 40 | source_tables => $source_tables, | ||||||||||||||||||||||||||||
| 41 | target_tables => $target_tables // $target, | ||||||||||||||||||||||||||||
| 42 | changed_when => \&changed_fk_fields, | ||||||||||||||||||||||||||||
| 43 | 5 | ▶ | on_new => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 5 | my ($table_name, $constraint_name, $fk_info) = @_; | |||||||||||||||||||||||||||
| 45 | 5 | $class->new(action => 'add', table_name => $table_name, | |||||||||||||||||||||||||||
| 46 | constraint_name => $constraint_name, fk_info => $fk_info); | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | 2 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 2 | my ($table_name, $constraint_name, $old_info, $new_info) = @_; | |||||||||||||||||||||||||||
| 50 | ( | ||||||||||||||||||||||||||||
| 51 | 2 | $class->new(action => 'drop', table_name => $table_name, replaced => 1, | |||||||||||||||||||||||||||
| 52 | constraint_name => $constraint_name, fk_info => $old_info), | ||||||||||||||||||||||||||||
| 53 | $class->new(action => 'add', table_name => $table_name, | ||||||||||||||||||||||||||||
| 54 | constraint_name => $constraint_name, fk_info => $new_info), | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||
| 57 | 11 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 11 | my ($table_name, $constraint_name, $fk_info) = @_; | |||||||||||||||||||||||||||
| 59 | 11 | $class->new(action => 'drop', table_name => $table_name, | |||||||||||||||||||||||||||
| 60 | constraint_name => $constraint_name, fk_info => $fk_info); | ||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | 176 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$target_tables // $target
MC/DC0%0/2
$target_tables // $target
$target_tables
$target
Truth table33%1/3
A // B
A= $target_tablesB= $target
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # A table the diff drops, and a foreign key of it into another table the | ||||||||||||||||||||||||||||
| 65 | # diff drops: the server refuses the DROP TABLE of the referenced one while | ||||||||||||||||||||||||||||
| 66 | # the constraint is there (3730 on MySQL, 1451 on MariaDB), and which of the | ||||||||||||||||||||||||||||
| 67 | # two goes first is a matter of their names. The constraint is dropped | ||||||||||||||||||||||||||||
| 68 | # first, with the others (karr k94). A foreign key into a table that stays, | ||||||||||||||||||||||||||||
| 69 | # or into itself, goes with the DROP TABLE as ever. | ||||||||||||||||||||||||||||
| 70 | 176 | ▶ | if ($target_tables) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 11 | my %dropped = map { $_ => 1 } | |||||||||||||||||||||||||||
| 72 | 176 | ▶ | grep { !exists $target_tables->{$_} } keys %{ $source_tables // {} }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$source_tables // {}
MC/DC0%0/1
$source_tables // {}
$source_tables
Truth table50%1/2
A
A= $source_tables
| |||||||||||||||||||||||||||||
| 73 | 176 | for my $table_name (sort grep { $dropped{$_} } keys %$source) { | |||||||||||||||||||||||||||
| 74 | 2 | for my $fk_info (@{ $source->{$table_name} }) { | |||||||||||||||||||||||||||
| 75 | 2 | ▶ | next unless $dropped{ $fk_info->{to_table} } && $fk_info->{to_table} ne $table_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dropped{$fk_info->{"to_table"}} and $fk_info->{"to_table"} ne $table_name
MC/DC50%1/2
$dropped{$fk_info->{"to_table"}} and $fk_info->{"to_table"} ne $table_name
$dropped{$fk_info->{"to_table"}}
$fk_info->{"to_table"} ne $table_name
Truth table66%2/3
A and B
A= $dropped{$fk_info->{"to_table"}}B= $fk_info->{"to_table"} ne $table_name
| |||||||||||||||||||||||||||||
| 76 | push @ops, $class->new(action => 'drop', table_name => $table_name, | ||||||||||||||||||||||||||||
| 77 | 1 | constraint_name => $fk_info->{constraint_name}, fk_info => $fk_info); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 176 | return @ops; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 56 | ▶ | sub refuse_set_default { | ||||||||||||||||||||||||||
|
Subroutine
refuse_set_default: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 56 | my ($table, $constraint, $info) = @_; | |||||||||||||||||||||||||||
| 88 | 56 | for my $attr (qw(on_update on_delete)) { | |||||||||||||||||||||||||||
| 89 | 108 | my $value = $info->{$attr}; | |||||||||||||||||||||||||||
| 90 | 108 | ▶ | next unless defined $value && $value =~ /\A\s*SET[\s_]+DEFAULT\s*\z/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value =~ /\A\s*SET[\s_]+DEFAULT\s*\z/i
MC/DC100%2/2
defined $value and $value =~ /\A\s*SET[\s_]+DEFAULT\s*\z/i
defined $value
$value =~ /\A\s*SET[\s_]+DEFAULT\s*\z/i
Truth table100%3/3
A and B
A= defined $valueB= $value =~ /\A\s*SET[\s_]+DEFAULT\s*\z/i
| |||||||||||||||||||||||||||||
| 91 | 8 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | "foreign key of %2\$s, constraint %1\$s, has %3\$s %4\$s: MySQL and MariaDB accept SET DEFAULT but never run it " | ||||||||||||||||||||||||||||
| 93 | . "(the server acts as RESTRICT), so it is refused", | ||||||||||||||||||||||||||||
| 94 | defined $constraint ? "'$constraint'" : '(unnamed)', $table, $attr, $value); | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 42 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 42 | my ($self) = @_; | |||||||||||||||||||||||||||
| 101 | 42 | my $tbl = quote_table_in($self->database, $self->table_name); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 42 | ▶ | if ($self->action eq 'add') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | # A foreign key the introspection gave no name (a hand made model) gets the | ||||||||||||||||||||||||||||
| 105 | # name install_ddl would give it, as in a CREATE TABLE (karr k106). | ||||||||||||||||||||||||||||
| 106 | 25 | my $info = $self->fk_info; | |||||||||||||||||||||||||||
| 107 | my $name = quote_ident($self->constraint_name | ||||||||||||||||||||||||||||
| 108 | 25 | ▶ | // DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{ $info->{from_columns} })); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->constraint_name // DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$info{"from_columns"};})
MC/DC0%0/2
$self->constraint_name // DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$info{"from_columns"};})
$self->constraint_name
DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$info{"from_columns"};})
Truth table66%2/3
A // B
A= $self->constraint_nameB= DBIO::MySQL::DDL::foreign_key_name($self->table_name, @{$$info{"from_columns"};})
| |||||||||||||||||||||||||||||
| 109 | 25 | refuse_set_default($self->table_name, $self->constraint_name, $info); | |||||||||||||||||||||||||||
| 110 | 21 | my $from = join(', ', map { quote_ident($_) } @{ $info->{from_columns} }); | |||||||||||||||||||||||||||
| 111 | 21 | my $to = join(', ', map { quote_ident($_) } @{ $info->{to_columns} }); | |||||||||||||||||||||||||||
| 112 | my $sql = sprintf | ||||||||||||||||||||||||||||
| 113 | 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)', | ||||||||||||||||||||||||||||
| 114 | 21 | $tbl, $name, $from, quote_ident($info->{to_table}), $to; | |||||||||||||||||||||||||||
| 115 | 21 | ▶ | $sql .= " ON UPDATE $info->{on_update}" if $info->{on_update} && $info->{on_update} ne 'NO ACTION'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$info->{"on_update"} and $info->{"on_update"} ne "NO ACTION"
MC/DC100%2/2
$info->{"on_update"} and $info->{"on_update"} ne "NO ACTION"
$info->{"on_update"}
$info->{"on_update"} ne "NO ACTION"
Truth table100%3/3
A and B
A= $info->{"on_update"}B= $info->{"on_update"} ne "NO ACTION"
| |||||||||||||||||||||||||||||
| 116 | 21 | ▶ | $sql .= " ON DELETE $info->{on_delete}" 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"
| |||||||||||||||||||||||||||||
| 117 | 21 | return "$sql;"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 17 | return sprintf 'ALTER TABLE %s DROP FOREIGN KEY %s;', $tbl, quote_ident($self->constraint_name); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $prefix = $self->action eq 'add' ? '+' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | return sprintf ' %sfk: %s on %s', $prefix, $self->constraint_name, $self->table_name; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 57 | 1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||