| 1 | package DBIO::DB2::Diff::ForeignKey; | ||||||||||
| 2 | # ABSTRACT: Diff operations for DB2 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::SQL::Util qw(_quote_ident); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use DBIO::Diff::Compare qw(changed_fk_fields); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use DBIO::DB2::DDL qw(_fk_constraint_clause); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | 4 | __PACKAGE__->mk_diff_accessors(qw/table_name constraint_name fk_info/); | |||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | 12 | ▶ | sub diff { | ||||||||
|
Subroutine
diff: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 18 | 12 | my ($class, $source, $target, $source_tables, $target_tables) = @_; | |||||||||
| 19 | |||||||||||
| 20 | return $class->diff_nested($source, $target, | ||||||||||
| 21 | index_by => 'constraint_name', | ||||||||||
| 22 | scope => 'both', | ||||||||||
| 23 | source_tables => $source_tables, | ||||||||||
| 24 | target_tables => $target_tables, | ||||||||||
| 25 | 2 | ▶ | changed_when => sub { scalar changed_fk_fields($_[0], $_[1]) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 3 | ▶ | on_new => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 3 | my ($table, $name, $new) = @_; | |||||||||
| 28 | 3 | $class->new(action => 'add', table_name => $table, | |||||||||
| 29 | constraint_name => $name, fk_info => $new); | ||||||||||
| 30 | }, | ||||||||||
| 31 | 1 | ▶ | on_changed => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | my ($table, $name, $old, $new) = @_; | |||||||||
| 33 | 1 | ($class->new(action => 'drop', table_name => $table, | |||||||||
| 34 | constraint_name => $name, fk_info => $old), | ||||||||||
| 35 | $class->new(action => 'add', table_name => $table, | ||||||||||
| 36 | constraint_name => $name, fk_info => $new)); | ||||||||||
| 37 | }, | ||||||||||
| 38 | 1 | ▶ | on_gone => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 1 | my ($table, $name, $old) = @_; | |||||||||
| 40 | 1 | $class->new(action => 'drop', table_name => $table, | |||||||||
| 41 | constraint_name => $name, fk_info => $old); | ||||||||||
| 42 | }, | ||||||||||
| 43 | 12 | ); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | 8 | ▶ | sub as_sql { | ||||||||
|
Subroutine
as_sql: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 48 | 8 | my ($self) = @_; | |||||||||
| 49 | 8 | my $tbl = _quote_ident($self->table_name); | |||||||||
| 50 | 8 | my $name = _quote_ident($self->constraint_name); | |||||||||
| 51 | |||||||||||
| 52 | 8 | ▶ | if ($self->action eq 'add') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | # Same FK clause body as the inline create path -- single source of truth | ||||||||||
| 54 | # in DBIO::DB2::DDL (ADR 0005), here wrapped in ALTER TABLE ... ADD. | ||||||||||
| 55 | 6 | my $clause = _fk_constraint_clause($self->fk_info); | |||||||||
| 56 | 6 | return sprintf 'ALTER TABLE %s ADD %s;', $tbl, $clause; | |||||||||
| 57 | } | ||||||||||
| 58 | 2 | return sprintf 'ALTER TABLE %s DROP FOREIGN KEY %s;', $tbl, $name; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub summary { | ||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||
| 64 | 0 | return sprintf ' %sfk: %s on %s', | |||||||||
| 65 | $self->summary_prefix, $self->constraint_name, $self->table_name; | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 4 | 1; | |||||||||
| 69 | |||||||||||
| 70 | __END__ | ||||||||||
| 71 | |||||||||||
| 72 | =pod | ||||||||||
| 73 | |||||||||||
| 74 | =encoding UTF-8 | ||||||||||
| 75 | |||||||||||
| 76 | =head1 NAME | ||||||||||
| 77 | |||||||||||
| 78 | DBIO::DB2::Diff::ForeignKey - Diff operations for DB2 foreign keys | ||||||||||
| 79 | |||||||||||
| 80 | =head1 VERSION | ||||||||||
| 81 | |||||||||||
| 82 | version 0.900001 | ||||||||||
| 83 | |||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||
| 85 | |||||||||||
| 86 | Represents a foreign key diff operation: C<ADD CONSTRAINT> or | ||||||||||
| 87 | C<DROP FOREIGN KEY>. FKs on a brand-new table are created inline by | ||||||||||
| 88 | L<DBIO::DB2::Diff::Table>; this module handles FK changes on tables that | ||||||||||
| 89 | exist in both source and target. Built on L<DBIO::Diff::Op>. | ||||||||||
| 90 | |||||||||||
| 91 | FK identity is by C<constraint_name>. DB2 has no C<ALTER> for a foreign key | ||||||||||
| 92 | definition, so a change becomes a drop-then-add pair (drop first). The | ||||||||||
| 93 | deterministic FK name emitted by L<DBIO::DB2::DDL> round-trips through | ||||||||||
| 94 | introspection, so source and target carry the same stable name and the | ||||||||||
| 95 | name-based match does not phantom-diff (see ADR 0005). The C<DROP> uses the | ||||||||||
| 96 | real server name carried in the model. | ||||||||||
| 97 | |||||||||||
| 98 | =head1 METHODS | ||||||||||
| 99 | |||||||||||
| 100 | =head2 diff | ||||||||||
| 101 | |||||||||||
| 102 | my @ops = DBIO::DB2::Diff::ForeignKey->diff( | ||||||||||
| 103 | $source_fks, $target_fks, $source_tables, $target_tables, | ||||||||||
| 104 | ); | ||||||||||
| 105 | |||||||||||
| 106 | =head2 as_sql | ||||||||||
| 107 | |||||||||||
| 108 | =head2 summary | ||||||||||
| 109 | |||||||||||
| 110 | =head1 AUTHOR | ||||||||||
| 111 | |||||||||||
| 112 | DBIO & DBIx::Class Authors | ||||||||||
| 113 | |||||||||||
| 114 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 115 | |||||||||||
| 116 | Copyright (C) 2026 DBIO Authors | ||||||||||
| 117 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||
| 118 | Based on DBIx::Class, heavily modified. | ||||||||||
| 119 | |||||||||||
| 120 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 121 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||