From 94b2d5405c40c31a4c1d60136a1a64ba10529ea8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joonas=20Kylm=C3=A4l=C3=A4?= Date: Fri, 31 Aug 2018 15:32:20 +0300 Subject: [PATCH] Bug 17885: Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings throws DBD::mysql Duplicate entry exceptions When executing Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings() against populated search engine mappings tables, one gets this exception: DBD::mysql::st execute failed: Duplicate entry '388-73' for key 'PRIMARY' [for Statement \"INSERT INTO `search_marc_to_field` ( `facet`, `search_field_id`, `search_marc_map_id`, `sort`, `suggestible`) VALUES ( ?, ?, ?, ?, ? )\" with ParamValues: 0='', 1='73', 2='388', 3=undef, 4=''] at /usr/share/perl5/DBIx/Class/Storage/DBI.pm line 1834.\nDBIx::Class::Storage::DBI::_dbh_execute(): Duplicate entry '388-73' for key 'PRIMARY' at /home/koha/Koha/Koha/SearchField.pm line 38" This patch fixes this, by first deleting all entries, then adding the default ones. If a subroutine says reset_elasticsearch_mappings(), I expect it to reset the elasticsearch mappings without errors. When writing tests it is better to call that one subroutine reset_elasticsearch_mappings instead of the bunch of code in admin/searchengine/elasticsearch/mappings.pl?op=reset_confirmed Also this promotes code reuse. TEST PLAN: a. perl -e 'use Koha::SearchEngine::Elasticsearch; Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings' b. Run misc/devel/populate_db.pl on an already populated DB and you eventually face this problem. After this patch, step a. works. This patch is adapted from the original patch made by Olli-Antti Kivilahti . Signed-off-by: Johanna Raisa Signed-off-by: Jonathan Druart Signed-off-by: Martin Renvoize Signed-off-by: Joy Nelson (cherry picked from commit 0c68b3a171c07025a0bb9e48245444a44b30d196) Signed-off-by: Lucas Gass --- Koha/SearchEngine/Elasticsearch.pm | 3 +++ admin/searchengine/elasticsearch/mappings.pl | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index ffa3cbafcb..d9fd093cd7 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -282,6 +282,9 @@ sub reset_elasticsearch_mappings { $mappings_yaml ||= C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml'; my $indexes = LoadFile( $mappings_yaml ); + Koha::SearchMarcMaps->delete; + Koha::SearchFields->delete; + while ( my ( $index_name, $fields ) = each %$indexes ) { while ( my ( $field_name, $data ) = each %$fields ) { my %sf_params = map { $_ => $data->{$_} } grep { exists $data->{$_} } qw/ type label weight facet_order /; diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl index f91399aa9d..62fdb8bd29 100755 --- a/admin/searchengine/elasticsearch/mappings.pl +++ b/admin/searchengine/elasticsearch/mappings.pl @@ -143,8 +143,6 @@ if ( $op eq 'edit' ) { } } elsif( $op eq 'reset_confirmed' ) { - Koha::SearchMarcMaps->delete; - Koha::SearchFields->delete; Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; push @messages, { type => 'message', code => 'success_on_reset' }; } -- 2.39.5