| 1 | package DBIO::MySQL::Diff::ForeignKey; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff operations for MySQL/MariaDB foreign keys | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use DBIO::Diff::Compare qw(changed_fk_fields); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | __PACKAGE__->mk_diff_accessors(qw( | |||||||||||||||||||||||||||
| 12 | table_name constraint_name fk_info | ||||||||||||||||||||||||||||
| 13 | )); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 6 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | return $class->diff_nested( | ||||||||||||||||||||||||||||
| 21 | $source, $target, | ||||||||||||||||||||||||||||
| 22 | index_by => 'constraint_name', | ||||||||||||||||||||||||||||
| 23 | source_tables => $source_tables, | ||||||||||||||||||||||||||||
| 24 | target_tables => $target_tables, | ||||||||||||||||||||||||||||
| 25 | changed_when => \&changed_fk_fields, | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | on_new => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | my ($table_name, $constraint_name, $fk_info) = @_; | |||||||||||||||||||||||||||
| 28 | 1 | $class->new(action => 'add', table_name => $table_name, | |||||||||||||||||||||||||||
| 29 | constraint_name => $constraint_name, fk_info => $fk_info); | ||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | on_changed => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($table_name, $constraint_name, $old_info, $new_info) = @_; | |||||||||||||||||||||||||||
| 33 | ( | ||||||||||||||||||||||||||||
| 34 | 0 | $class->new(action => 'drop', table_name => $table_name, | |||||||||||||||||||||||||||
| 35 | constraint_name => $constraint_name, fk_info => $old_info), | ||||||||||||||||||||||||||||
| 36 | $class->new(action => 'add', table_name => $table_name, | ||||||||||||||||||||||||||||
| 37 | constraint_name => $constraint_name, fk_info => $new_info), | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | on_gone => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | my ($table_name, $constraint_name, $fk_info) = @_; | |||||||||||||||||||||||||||
| 42 | 1 | $class->new(action => 'drop', table_name => $table_name, | |||||||||||||||||||||||||||
| 43 | constraint_name => $constraint_name, fk_info => $fk_info); | ||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | 6 | ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 51 | 2 | my $tbl = $self->table_name; | |||||||||||||||||||||||||||
| 52 | 2 | my $name = $self->constraint_name; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | if ($self->action eq 'add') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | my $info = $self->fk_info; | |||||||||||||||||||||||||||
| 56 | 1 | my $from = join(', ', map { "`$_`" } @{ $info->{from_columns} }); | |||||||||||||||||||||||||||
| 57 | 1 | my $to = join(', ', map { "`$_`" } @{ $info->{to_columns} }); | |||||||||||||||||||||||||||
| 58 | my $sql = sprintf | ||||||||||||||||||||||||||||
| 59 | 'ALTER TABLE `%s` ADD CONSTRAINT `%s` FOREIGN KEY (%s) REFERENCES `%s`(%s)', | ||||||||||||||||||||||||||||
| 60 | 1 | $tbl, $name, $from, $info->{to_table}, $to; | |||||||||||||||||||||||||||
| 61 | 1 | ▶ | $sql .= " ON UPDATE $info->{on_update}" if $info->{on_update} && $info->{on_update} ne 'NO ACTION'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/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 table33%1/3
A and B
A= $info->{"on_update"}B= $info->{"on_update"} ne "NO ACTION"
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | $sql .= " ON DELETE $info->{on_delete}" if $info->{on_delete} && $info->{on_delete} ne 'NO ACTION'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION"
MC/DC0%0/2
$info->{"on_delete"} and $info->{"on_delete"} ne "NO ACTION"
$info->{"on_delete"}
$info->{"on_delete"} ne "NO ACTION"
Truth table33%1/3
A and B
A= $info->{"on_delete"}B= $info->{"on_delete"} ne "NO ACTION"
| |||||||||||||||||||||||||||||
| 63 | 1 | return "$sql;"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 1 | return sprintf 'ALTER TABLE `%s` DROP FOREIGN KEY `%s`;', $tbl, $name; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $prefix = $self->action eq 'add' ? '+' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | return sprintf ' %sfk: %s on %s', $prefix, $self->constraint_name, $self->table_name; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 4 | 1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =pod | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 NAME | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | DBIO::MySQL::Diff::ForeignKey - Diff operations for MySQL/MariaDB foreign keys | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 VERSION | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | version 0.900001 | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Represents a foreign key diff operation: C<ADD CONSTRAINT> or | ||||||||||||||||||||||||||||
| 94 | C<DROP FOREIGN KEY>. FKs that already exist on a brand-new table are | ||||||||||||||||||||||||||||
| 95 | created inline by L<DBIO::MySQL::Diff::Table> -- this module only | ||||||||||||||||||||||||||||
| 96 | handles FK changes on tables that exist in both source and target. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | FK identity is by C<constraint_name>. A definition change becomes a | ||||||||||||||||||||||||||||
| 99 | drop-then-add pair (MySQL has no C<ALTER FOREIGN KEY>). | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 METHODS | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head2 diff | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | my @ops = DBIO::MySQL::Diff::ForeignKey->diff( | ||||||||||||||||||||||||||||
| 106 | $source_fks, $target_fks, $source_tables, $target_tables, | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 as_sql | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 summary | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 120 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 121 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 124 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||