From 7b5d5af0445aa8f21f929c2c23fe5de86f6028c3 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 1 Nov 2021 14:08:42 +0000 Subject: [PATCH] Bug 29390: Allow to check on FK existence with field name 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 Signed-off-by: David Nind --- 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 7e248a17aa..5545908d01 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -648,10 +648,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 2e9c0e26c8..c8fc627aea 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 => 22; +use Test::More tests => 23; 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.30.2