From 701929b25393f7029a4ad0a670dbe65dc6ecb910 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 30 Mar 2021 15:08:09 +0000 Subject: [PATCH] Bug 26326: (follow-up) Add ability to check for existence of any primary key Before dropping a primary key we need to see if one exists - this updates the installer method to allow searching for existence of key Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Katrin Fischer Bug 26326: (follow-up) Address qa script failures Signed-off-by: Katrin Fischer Signed-off-by: Jonathan Druart --- C4/Installer.pm | 15 +++++++++------ Koha/Import/Record.pm | 4 ++-- installer/data/mysql/atomicupdate/bug_26326.perl | 4 +++- t/db_dependent/Installer.t | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/C4/Installer.pm b/C4/Installer.pm index 65bb578e1a..ff9c2b170f 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -632,12 +632,15 @@ 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 - ); + my $sql = qq| SHOW INDEX FROM $table_name WHERE key_name='PRIMARY' |; + my $exists; + if( $key_name ){ + $sql .= 'AND column_name = ? ' if $key_name; + ($exists) = $dbh->selectrow_array( $sql, undef, $key_name ); + } else { + ($exists) = $dbh->selectrow_array( $sql, undef ); + } + return $exists; } diff --git a/Koha/Import/Record.pm b/Koha/Import/Record.pm index 189d878dd8..efd017152e 100644 --- a/Koha/Import/Record.pm +++ b/Koha/Import/Record.pm @@ -29,12 +29,12 @@ Koha::Import::Record - Koha Import Record Object class =head1 API -=head2 Class methods - =head2 Internal methods =head3 _type +Returns name of corresponding DBIC resultset + =cut sub _type { diff --git a/installer/data/mysql/atomicupdate/bug_26326.perl b/installer/data/mysql/atomicupdate/bug_26326.perl index b6a9c380fb..000aedc9f7 100644 --- a/installer/data/mysql/atomicupdate/bug_26326.perl +++ b/installer/data/mysql/atomicupdate/bug_26326.perl @@ -4,7 +4,9 @@ if( CheckVersion( $DBversion ) ) { primary_key_exists('import_record_matches','import_record_id') && primary_key_exists('import_record_matches','candidate_match_id') ){ - $dbh->do( "ALTER TABLE import_record_matches DROP PRIMARY KEY" ); + if( primary_key_exists('import_record_matches' ) ){ + $dbh->do( "ALTER TABLE import_record_matches DROP PRIMARY KEY" ); + } $dbh->do( "ALTER TABLE import_record_matches ADD PRIMARY KEY (import_record_id,candidate_match_id)" ); } diff --git a/t/db_dependent/Installer.t b/t/db_dependent/Installer.t index 15e2985af8..2e9c0e26c8 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; @@ -73,7 +73,7 @@ ok( ! foreign_key_exists( 'borrowers', 'xxx' ), 'FK xxxx does not exist' ); ok( unique_key_exists( 'items', 'itembarcodeidx' ), 'UNIQUE KEY itembarcodeidx exists' ); ok( ! unique_key_exists( 'borrowers', 'xxx' ), 'UNIQUE KEY xxxx does not exist' ); - +ok( primary_key_exists( 'borrowers' ), 'Borrowers does have a primary key on some column'); 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.39.5