From 1e8ccc0fc6efa46e2d84ae1a3cbf6972bb491045 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 14 Feb 2025 22:35:41 +0000 Subject: [PATCH] Bug 36365: Add a way for compare_es_to_db.pl to fix problems This patch adds a new option switch `--fix` (or `-f`) to make the script try to fix the problems it found. Bonus: `--help` option switch added. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ perl -MKoha::SearchEngine -MKoha::SearchEngine::Indexer -e 'my $i = Koha::SearchEngine::Indexer->new({index => $Koha::SearchEngine::BIBLIOS_INDEX}); $i->delete_index([1]);' => SUCCESS: Bibliographic record #deleted from ES index 3. Run: k$ koha-mysql kohadev > DELETE FROM biblio WHERE biblionumber=2 OR biblionumber=3; => SUCCESS: Bibliographic records 2 and 3 deleted directly from the database, no reindex triggered by Koha. 4. Run: k$ perl misc/maintenance/compare_es_to_db.pl => SUCCESS: The results from (2) and (3) are explained. [1] 5. Run: k$ perl misc/maintenance/compare_es_to_db.pl --fix => SUCCESS: It says it is gonna fix things 6. Repeat 4 => SUCCESS: The problems we generated are no longer reported! They've been fixed! 7. Sign off :-D [1] Note KTD ships some broken records on purpose so devs need to deal with a not-perfect DB everyday, and catch misses in the code. Signed-off-by: Andrew Fuerste Henry --- misc/maintenance/compare_es_to_db.pl | 70 ++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/misc/maintenance/compare_es_to_db.pl b/misc/maintenance/compare_es_to_db.pl index 394449b743..7d9c52acd6 100755 --- a/misc/maintenance/compare_es_to_db.pl +++ b/misc/maintenance/compare_es_to_db.pl @@ -30,7 +30,10 @@ B =cut use Modern::Perl; + use Array::Utils qw( array_minus ); +use Getopt::Long qw( GetOptions ); +use Try::Tiny qw( catch try ); use C4::Context; @@ -39,6 +42,33 @@ use Koha::Biblios; use Koha::Items; use Koha::SearchEngine::Elasticsearch; +my $help; +my $fix; + +GetOptions( + 'h|help' => \$help, + 'f|fix' => \$fix, +); + +my $usage = <<'ENDUSAGE'; + +This script finds differences between the records on the Koha database +and the Elasticsearch index. + +The `--fix` option switch can be passed to try fixing them. + +This script has the following parameters : + + -f|--fix Try to fix errors + -h|--help Print this message + +ENDUSAGE + +if ($help) { + print $usage; + exit; +} + foreach my $index ( ( 'biblios', 'authorities' ) ) { print "=================\n"; print "Checking $index\n"; @@ -110,4 +140,44 @@ foreach my $index ( ( 'biblios', 'authorities' ) ) { print " Enter this command to view record: curl $es_base/data/$problem?pretty=true\n"; } } + + if ( $fix && ( @koha_problems || @es_problems ) ) { + + print "=================\n"; + print "Trying to fix problems:\n\n"; + + my $indexer; + my $server; + if ( $index eq 'biblios' ) { + $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); + $server = 'biblioserver'; + } else { + $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } ); + $server = 'authorityserver'; + } + + if (@koha_problems) { + + print "=================\n"; + print "Fixing missing records in the index ($index):\n\n"; + + foreach my $id (@koha_problems) { + try { + $indexer->update_index( [$id] ); + } catch { + print STDERR "ERROR: record #$id failed: $_\n\n"; + }; + } + } + + if (@es_problems) { + + print "=================\n"; + print "Deleting non-existent records from the index ($index)...\n"; + + $indexer->delete_index( \@es_problems ); + } + } } + +1; -- 2.39.5