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 <andrew@bywatersolutions.com>
Signed-off-by: Lisette Scheer <lisette@bywatersolutions.com>
Signed-off-by: Thomas Klausner <domm@plix.at>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Tomas Cohen Arazi 2025-02-14 22:35:41 +00:00 committed by Katrin Fischer
parent 5986c6649c
commit 3669cb0eed
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
1 changed files with 70 additions and 0 deletions

View File

@ -30,7 +30,10 @@ B<compare_es_to_db.pl>
=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";
@ -111,4 +141,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;