From 39d2e80f896c4d115e796a9698c541b872e537b7 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 31 Aug 2020 16:43:53 +0000 Subject: [PATCH] Bug 26325: Add primary_key_exists check to C4::Installer To test: 1 - prove -v t/db_dependent/Installer.t Signed-off-by: Brandon J Signed-off-by: Katrin Fischer --- C4/Installer.pm | 14 +++++++++++++- t/db_dependent/Installer.t | 6 +++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/C4/Installer.pm b/C4/Installer.pm index 3624d13226..55bcf27335 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -30,7 +30,7 @@ use vars qw(@ISA @EXPORT); BEGIN { require Exporter; @ISA = qw( Exporter ); - push @EXPORT, qw( foreign_key_exists index_exists column_exists TableExists); + push @EXPORT, qw( primary_key_exists foreign_key_exists index_exists column_exists TableExists); }; =head1 NAME @@ -610,6 +610,18 @@ sub get_file_path_from_name { } +sub primary_key_exists { + my ( $table_name, $key_name ) = @_; + my $dbh = C4::Context->dbh; + my ($exists) = $dbh->selectrow_array( + qq| + SHOW INDEX FROM $table_name + WHERE key_name = 'PRIMARY' AND column_name = ? + |, undef, $key_name + ); + return $exists; +} + sub foreign_key_exists { my ( $table_name, $constraint_name ) = @_; my $dbh = C4::Context->dbh; diff --git a/t/db_dependent/Installer.t b/t/db_dependent/Installer.t index 7ba654d62f..be3bc350a9 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 => 16; +use Test::More tests => 18; use Koha::Database; BEGIN { @@ -66,3 +66,7 @@ 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' ); + + +ok( primary_key_exists( 'borrowers', 'borrowernumber'), 'Borrowers has primary key on borrowernumber'); +ok( ! primary_key_exists( 'borrowers', 'email'), 'Borrowers does not have a primary key on email'); -- 2.11.0