| 1 | package DBIO::Sybase::Diff::ForeignKey; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Diff Sybase ASE foreign keys | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use DBIO::Diff::Compare qw(changed_fk_fields); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DBIO::SQL::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Stable identity for a per-constraint FK entry: the relationship tuple. | ||||||||||||||||||||||||||||
| 12 | # on_update / on_delete are attributes, not identity. | ||||||||||||||||||||||||||||
| 13 | 14 | ▶ | sub _fk_key { | ||||||||||||||||||||||||||
|
Subroutine
_fk_key: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 14 | 14 | my ($fk) = @_; | |||||||||||||||||||||||||||
| 15 | return join "\0", | ||||||||||||||||||||||||||||
| 16 | 14 | ▶ | join(',', @{ $fk->{from_columns} // [] }), | ||||||||||||||||||||||||||
|
Condition50%2/4
$fk->{"from_columns"} // []
$fk->{"to_table"} // ""
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
MC/DC0%0/1
$fk->{"to_table"} // ""
$fk->{"to_table"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
Truth table50%1/2
A
A= $fk->{"to_table"}
| |||||||||||||||||||||||||||||
| 17 | ($fk->{to_table} // ''), | ||||||||||||||||||||||||||||
| 18 | 14 | ▶ | join(',', @{ $fk->{to_columns} // [] }); | ||||||||||||||||||||||||||
|
Condition50%1/2
$fk->{"to_columns"} // []
MC/DC0%0/1
$fk->{"to_columns"} // []
$fk->{"to_columns"}
Truth table50%1/2
A
A= $fk->{"to_columns"}
| |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Constraint name to emit for an FK entry. Prefers the real, server-assigned | ||||||||||||||||||||||||||||
| 22 | # name carried on the entry (present on source/introspected entries, absent on | ||||||||||||||||||||||||||||
| 23 | # desired/target entries), so DROP/ALTER target a foreign key by its actual | ||||||||||||||||||||||||||||
| 24 | # name even when it was created out-of-band. Falls back to a deterministic name | ||||||||||||||||||||||||||||
| 25 | # derived from the relationship tuple when no live name is present (every | ||||||||||||||||||||||||||||
| 26 | # CREATE, plus any DROP whose source entry lacks a name). | ||||||||||||||||||||||||||||
| 27 | 19 | ▶ | sub _fk_name { | ||||||||||||||||||||||||||
|
Subroutine
_fk_name: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 28 | 19 | my ($table, $fk) = @_; | |||||||||||||||||||||||||||
| 29 | return $fk->{constraint_name} | ||||||||||||||||||||||||||||
| 30 | 19 | ▶ | if defined $fk->{constraint_name} && length $fk->{constraint_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fk->{"constraint_name"} and length $fk->{"constraint_name"}
MC/DC50%1/2
defined $fk->{"constraint_name"} and length $fk->{"constraint_name"}
defined $fk->{"constraint_name"}
length $fk->{"constraint_name"}
Truth table66%2/3
A and B
A= defined $fk->{"constraint_name"}B= length $fk->{"constraint_name"}
| |||||||||||||||||||||||||||||
| 31 | 16 | ▶ | return sprintf 'fk_%s_%s', $table, join('_', @{ $fk->{from_columns} // [] }); | ||||||||||||||||||||||||||
|
Condition50%1/2
$fk->{"from_columns"} // []
MC/DC0%0/1
$fk->{"from_columns"} // []
$fk->{"from_columns"}
Truth table50%1/2
A
A= $fk->{"from_columns"}
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 11 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=12 SCAR=25.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | 11 | my ($class, $source, $target) = @_; | |||||||||||||||||||||||||||
| 36 | 11 | ▶ | $source //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$source //= {}
MC/DC0%0/1
$source //= {}
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||
| 37 | 11 | ▶ | $target //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target //= {}
MC/DC0%0/1
$target //= {}
$target
Truth table50%1/2
A
A= $target
| |||||||||||||||||||||||||||||
| 38 | 11 | my @ops; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # FK adds (and the create half of an alter) go in the target's tables. | ||||||||||||||||||||||||||||
| 41 | 11 | for my $table (sort keys %$target) { | |||||||||||||||||||||||||||
| 42 | # Tables present only in the target are created (with their FKs) elsewhere. | ||||||||||||||||||||||||||||
| 43 | 11 | ▶ | next unless exists $source->{$table}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 10 | ▶ | my %s_idx = map { _fk_key($_) => $_ } @{ $source->{$table} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$source->{$table} // []
MC/DC0%0/1
$source->{$table} // []
$source->{$table}
Truth table50%1/2
A
A= $source->{$table}
| |||||||||||||||||||||||||||||
| 46 | 10 | ▶ | my %t_idx = map { _fk_key($_) => $_ } @{ $target->{$table} // [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target->{$table} // []
MC/DC0%0/1
$target->{$table} // []
$target->{$table}
Truth table50%1/2
A
A= $target->{$table}
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 10 | for my $key (sort keys %t_idx) { | |||||||||||||||||||||||||||
| 49 | 7 | ▶ | if (exists $s_idx{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | # Same relationship, possibly different ON UPDATE/DELETE: drop+create. | ||||||||||||||||||||||||||||
| 51 | push @ops, | ||||||||||||||||||||||||||||
| 52 | DBIO::Sybase::Diff::ForeignKey::Drop->new( | ||||||||||||||||||||||||||||
| 53 | action => 'drop', table => $table, fk => $s_idx{$key}, | ||||||||||||||||||||||||||||
| 54 | ), | ||||||||||||||||||||||||||||
| 55 | DBIO::Sybase::Diff::ForeignKey::Create->new( | ||||||||||||||||||||||||||||
| 56 | action => 'create', table => $table, fk => $t_idx{$key}, | ||||||||||||||||||||||||||||
| 57 | ) | ||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if changed_fk_fields($s_idx{$key}, $t_idx{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | push @ops, DBIO::Sybase::Diff::ForeignKey::Create->new( | ||||||||||||||||||||||||||||
| 62 | 4 | action => 'create', table => $table, fk => $t_idx{$key}, | |||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 10 | for my $key (sort keys %s_idx) { | |||||||||||||||||||||||||||
| 68 | push @ops, DBIO::Sybase::Diff::ForeignKey::Drop->new( | ||||||||||||||||||||||||||||
| 69 | action => 'drop', table => $table, fk => $s_idx{$key}, | ||||||||||||||||||||||||||||
| 70 | 7 | ▶ | ) unless exists $t_idx{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 11 | return @ops; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | package DBIO::Sybase::Diff::ForeignKey::Create; | ||||||||||||||||||||||||||||
| 78 | 1 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | __PACKAGE__->mk_diff_accessors(qw(table fk)); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 8 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 83 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 8 | my $fk = $self->fk; | |||||||||||||||||||||||||||
| 85 | 8 | my $tbl = DBIO::SQL::Util::_quote_ident($self->table); | |||||||||||||||||||||||||||
| 86 | 8 | my $name = DBIO::SQL::Util::_quote_ident( | |||||||||||||||||||||||||||
| 87 | DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $fk)); | ||||||||||||||||||||||||||||
| 88 | 8 | my $from = join ', ', map { DBIO::SQL::Util::_quote_ident($_) } @{ $fk->{from_columns} }; | |||||||||||||||||||||||||||
| 89 | 8 | my $to = join ', ', map { DBIO::SQL::Util::_quote_ident($_) } @{ $fk->{to_columns} }; | |||||||||||||||||||||||||||
| 90 | 8 | my $ref = DBIO::SQL::Util::_quote_ident($fk->{to_table}); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 8 | my $sql = sprintf | |||||||||||||||||||||||||||
| 93 | 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)', | ||||||||||||||||||||||||||||
| 94 | $tbl, $name, $from, $ref, $to; | ||||||||||||||||||||||||||||
| 95 | $sql .= " ON UPDATE $fk->{on_update}" | ||||||||||||||||||||||||||||
| 96 | 8 | ▶ | if $fk->{on_update} && uc($fk->{on_update}) ne 'NO ACTION'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fk->{"on_update"} and uc $fk->{"on_update"} ne "NO ACTION"
MC/DC100%2/2
$fk->{"on_update"} and uc $fk->{"on_update"} ne "NO ACTION"
$fk->{"on_update"}
uc $fk->{"on_update"} ne "NO ACTION"
Truth table100%3/3
A and B
A= $fk->{"on_update"}B= uc $fk->{"on_update"} ne "NO ACTION"
| |||||||||||||||||||||||||||||
| 97 | $sql .= " ON DELETE $fk->{on_delete}" | ||||||||||||||||||||||||||||
| 98 | 8 | ▶ | if $fk->{on_delete} && uc($fk->{on_delete}) ne 'NO ACTION'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fk->{"on_delete"} and uc $fk->{"on_delete"} ne "NO ACTION"
MC/DC100%2/2
$fk->{"on_delete"} and uc $fk->{"on_delete"} ne "NO ACTION"
$fk->{"on_delete"}
uc $fk->{"on_delete"} ne "NO ACTION"
Truth table100%3/3
A and B
A= $fk->{"on_delete"}B= uc $fk->{"on_delete"} ne "NO ACTION"
| |||||||||||||||||||||||||||||
| 99 | 8 | return $sql; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 2 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | 2 | 'ADD FK ' . DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $self->fk) | |||||||||||||||||||||||||||
| 104 | . ' ON ' . $self->table; | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | package DBIO::Sybase::Diff::ForeignKey::Drop; | ||||||||||||||||||||||||||||
| 108 | 1 | ▶ | use base 'DBIO::Diff::Op'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | __PACKAGE__->mk_diff_accessors(qw(table fk)); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 6 | ▶ | sub as_sql { | ||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 6 | my $tbl = DBIO::SQL::Util::_quote_ident($self->table); | |||||||||||||||||||||||||||
| 115 | 6 | my $name = DBIO::SQL::Util::_quote_ident( | |||||||||||||||||||||||||||
| 116 | DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $self->fk)); | ||||||||||||||||||||||||||||
| 117 | 6 | return "ALTER TABLE $tbl DROP CONSTRAINT $name"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 3 | ▶ | sub summary { | ||||||||||||||||||||||||||
|
Subroutine
summary: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 120 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | 3 | 'DROP FK ' . DBIO::Sybase::Diff::ForeignKey::_fk_name($self->table, $self->fk) | |||||||||||||||||||||||||||
| 122 | . ' ON ' . $self->table; | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =pod | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 NAME | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | DBIO::Sybase::Diff::ForeignKey - Diff Sybase ASE foreign keys | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 VERSION | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | version 0.900001 | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Compares two foreign-key sets and generates FK-level diff operations for | ||||||||||||||||||||||||||||
| 144 | tables that exist on both sides. FKs on a brand-new table are the table's | ||||||||||||||||||||||||||||
| 145 | own concern; FKs on a dropped table vanish with it. This module only | ||||||||||||||||||||||||||||
| 146 | reconciles FK changes on tables retained across the diff. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | The introspected per-constraint FK shape (produced by | ||||||||||||||||||||||||||||
| 149 | L<DBIO::Sybase::Introspect/_group_fks_by_constraint>) is | ||||||||||||||||||||||||||||
| 150 | C<{ constraint_name, from_columns, to_table, to_columns, on_update, | ||||||||||||||||||||||||||||
| 151 | on_delete }>. FK identity here is the relationship tuple | ||||||||||||||||||||||||||||
| 152 | C<< from_columns -> to_table(to_columns) >> (B<not> the name, since the | ||||||||||||||||||||||||||||
| 153 | desired/target model built from a schema definition has no live name); the | ||||||||||||||||||||||||||||
| 154 | alterable attributes are C<on_update> / C<on_delete>. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | C<DROP> (and the drop-half of an alter) prefers the B<real, server-assigned> | ||||||||||||||||||||||||||||
| 157 | C<constraint_name> carried on the source (live/introspected) entry, so a | ||||||||||||||||||||||||||||
| 158 | foreign key created out-of-band under any name is dropped by its actual name. | ||||||||||||||||||||||||||||
| 159 | C<CREATE> (and the create-half of an alter) targets the desired model, which | ||||||||||||||||||||||||||||
| 160 | has no live name, so it falls back to a B<deterministic generated> name | ||||||||||||||||||||||||||||
| 161 | derived from the relationship tuple (C<fk_E<lt>tableE<gt>_E<lt>colsE<gt>>, | ||||||||||||||||||||||||||||
| 162 | the same approach the DDL emitter uses for unnamed unique indexes). When the | ||||||||||||||||||||||||||||
| 163 | source entry also lacks a name, C<DROP> falls back to the same generated name. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Sybase ASE cannot alter an FK constraint in place, so an attribute change | ||||||||||||||||||||||||||||
| 166 | becomes a drop-then-add pair. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 175 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 176 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 179 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =cut | ||||||||||||||||||||||||||||