From 66f421529c054eac9895f4093829d2951d6e0ac6 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 1 Nov 2021 14:08:42 +0000 Subject: [PATCH] Bug 29336: Allow to check on FK existence with field name Content-Type: text/plain; charset=utf-8 Sometimes easier (or even safer) to check for FK on specific field name rather than constraint name (which may be created automatically). Test plan: Run t/db_dependent/Installer.t Signed-off-by: Marcel de Rooy --- C4/Installer.pm | 5 +++-- t/db_dependent/Installer.t | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/C4/Installer.pm b/C4/Installer.pm index 65bb578e1a..f972940028 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -642,10 +642,11 @@ sub primary_key_exists { } sub foreign_key_exists { - my ( $table_name, $constraint_name ) = @_; + my ( $table_name, $constraint_name, $field_name ) = @_; my $dbh = C4::Context->dbh; my (undef, $infos) = $dbh->selectrow_array(qq|SHOW CREATE TABLE $table_name|); - return $infos =~ m|CONSTRAINT `$constraint_name` FOREIGN KEY|; + return $infos =~ m|CONSTRAINT `$constraint_name` FOREIGN KEY| if $constraint_name; + return $infos =~ m|FOREIGN KEY \(`$field_name`\)| if $field_name; } sub unique_key_exists { diff --git a/t/db_dependent/Installer.t b/t/db_dependent/Installer.t index 15e2985af8..2a1421de1c 100755 --- a/t/db_dependent/Installer.t +++ b/t/db_dependent/Installer.t @@ -22,7 +22,7 @@ # Add more tests here!!! use Modern::Perl; -use Test::More tests => 21; +use Test::More tests => 22; use File::Temp qw(tempfile); use utf8; @@ -69,6 +69,8 @@ ok( ! index_exists( 'borrowers', 'xxx'), 'Constraint xxx does not exist' ); ok( foreign_key_exists( 'borrowers', 'borrowers_ibfk_1' ), 'FK borrowers_ibfk_1 exists' ); ok( ! foreign_key_exists( 'borrowers', 'xxx' ), 'FK xxxx does not exist' ); +# test new third parameter: +ok( foreign_key_exists( 'borrowers', undef, 'categorycode' ), 'FK on categorycode exists' ); ok( unique_key_exists( 'items', 'itembarcodeidx' ), 'UNIQUE KEY itembarcodeidx exists' ); ok( ! unique_key_exists( 'borrowers', 'xxx' ), 'UNIQUE KEY xxxx does not exist' ); -- 2.20.1