From c61ca5fe75402ff8d66bbb458fa3daff9d099329 Mon Sep 17 00:00:00 2001 From: Nicola Date: Wed, 6 Nov 2024 18:07:11 +0100 Subject: [PATCH] anonymization: index after db.session.commit * move the record indexing part after the db.session.commit, to avoid indexing when there is a db rollback --- invenio_app_ils/patrons/anonymization.py | 44 +++++++++++++++++++----- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/invenio_app_ils/patrons/anonymization.py b/invenio_app_ils/patrons/anonymization.py index e687aa823..846c19b72 100644 --- a/invenio_app_ils/patrons/anonymization.py +++ b/invenio_app_ils/patrons/anonymization.py @@ -106,9 +106,32 @@ def anonymize_patron_data(patron_pid, force=False): cls = current_app.config["ILS_PATRON_ANONYMOUS_CLASS"] anonymous_patron_fields = cls().dumps_loader() + Loan = current_circulation.loan_record_cls + BorrowingRequest = current_ils_ill.borrowing_request_record_cls + DocumentRequest = current_app_ils.document_request_record_cls + Order = current_ils_acq.order_record_cls + + anonymized_records = { + Loan._pid_type: { + "indexer": current_circulation.loan_indexer(), + "records": [], + }, + BorrowingRequest._pid_type: { + "indexer": current_ils_ill.borrowing_request_indexer_cls(), + "records": [], + }, + DocumentRequest._pid_type: { + "indexer": current_app_ils.document_request_indexer, + "records": [], + }, + Order._pid_type: { + "indexer": current_ils_acq.order_indexer, + "records": [], + }, + } + patron_loans = get_loans_by_patron_pid(patron_pid).scan() - Loan = current_circulation.loan_record_cls indices = 0 for hit in patron_loans: loan = Loan.get_record_by_pid(hit.pid) @@ -131,7 +154,7 @@ def anonymize_patron_data(patron_pid, force=False): loan["patron_pid"] = anonymous_patron_fields["pid"] loan["patron"] = anonymous_patron_fields loan.commit() - current_circulation.loan_indexer().index(loan) + anonymized_records[Loan._pid_type]["records"].append(loan) indices += 1 BorrowingRequestsSearch = current_ils_ill.borrowing_request_search_cls @@ -139,14 +162,13 @@ def anonymize_patron_data(patron_pid, force=False): BorrowingRequestsSearch().search_by_patron_pid(patron_pid).scan() ) - BorrowingRequest = current_ils_ill.borrowing_request_record_cls - indexer = current_ils_ill.borrowing_request_indexer_cls() + for hit in patron_borrowing_requests: borrowing_request = BorrowingRequest.get_record_by_pid(hit.pid) borrowing_request["patron"] = anonymous_patron_fields borrowing_request["patron_pid"] = anonymous_patron_fields["pid"] borrowing_request.commit() - indexer.index(borrowing_request) + anonymized_records[BorrowingRequest._pid_type]["records"].append(borrowing_request) indices += 1 DocumentRequestSearch = current_app_ils.document_request_search_cls @@ -154,7 +176,6 @@ def anonymize_patron_data(patron_pid, force=False): DocumentRequestSearch().search_by_patron_pid(patron_pid).scan() ) - DocumentRequest = current_app_ils.document_request_record_cls for hit in patron_document_requests: document_request = DocumentRequest.get_record_by_pid(hit.pid) if document_request["state"] == "PENDING": @@ -163,19 +184,18 @@ def anonymize_patron_data(patron_pid, force=False): document_request["patron"] = anonymous_patron_fields document_request["patron_pid"] = anonymous_patron_fields["pid"] document_request.commit() - current_app_ils.document_request_indexer.index(document_request) + anonymized_records[DocumentRequest._pid_type]["records"].append(document_request) indices += 1 patron_acquisitions = OrderSearch().search_by_patron_pid(patron_pid).scan() - Order = current_ils_acq.order_record_cls for hit in patron_acquisitions: acquisition = Order.get_record_by_pid(hit.pid) for line in acquisition["order_lines"]: if line.get("patron_pid") == patron_pid: line["patron_pid"] = anonymous_patron_fields["pid"] acquisition.commit() - current_ils_acq.order_indexer.index(acquisition) + anonymized_records[Order._pid_type]["records"].append(acquisition) indices += 1 # delete rows from db @@ -185,6 +205,12 @@ def anonymize_patron_data(patron_pid, force=False): notifications = anonymize_patron_in_notification_logs(patron_pid) db.session.commit() + + # index all after committing to DB, to ensure that no errors occurred. + for indexer, records in anonymized_records.values(): + for record in records: + indexer.index(record) + if patron: try: patron_indexer = current_app_ils.patron_indexer