From 8ad0e0ce2de938652739392c6618a87377240b75 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Fri, 13 Jan 2017 12:34:14 +0200 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&i_know_what_i_am_doing=1 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. --- Koha/SearchEngine/Elasticsearch.pm | 7 +++++++ admin/searchengine/elasticsearch/mappings.pl | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm index 03cc5c3..7bae87e 100644 --- a/Koha/SearchEngine/Elasticsearch.pm +++ b/Koha/SearchEngine/Elasticsearch.pm @@ -275,10 +275,17 @@ sub _elasticsearch_mapping_for_default { }; } +=head2 reset_elasticsearch_mappings + +Deletes all search mappings and adds the default search mappings + +=cut + sub reset_elasticsearch_mappings { my $mappings_yaml = C4::Context->config('intranetdir') . '/admin/searchengine/elasticsearch/mappings.yaml'; my $indexes = LoadFile( $mappings_yaml ); + Koha::SearchMarcMaps->search->delete; while ( my ( $index_name, $fields ) = each %$indexes ) { while ( my ( $field_name, $data ) = each %$fields ) { my $field_type = $data->{type}; diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl index 7d63002..6d4b303 100755 --- a/admin/searchengine/elasticsearch/mappings.pl +++ b/admin/searchengine/elasticsearch/mappings.pl @@ -101,7 +101,6 @@ elsif( $op eq 'reset' ) { # TODO Move this feature to the interface my $sure = $input->param('i_know_what_i_am_doing'); if ( $sure ) { - Koha::SearchMarcMaps->search->delete; Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; } } -- 2.7.4