https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=42418 --- Comment #121 from Tomás Cohen Arazi (tcohen) <tomascohen@gmail.com> --- Created attachment 209386 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=209386&action=edit Bug 42418: (QA follow-up) Only backfill checkins with a known, existing checkin library The checkins backfill from old_issues had two problems, reported by Pedro on comment #119: 1. It failed with 'Broken FK constraint' when an old_issues row referenced a library that had been deleted, and no checkin was backfilled at all. 2. It stored checkout data as checkin data: - user_id was taken from old_issues.issuer_id, which is the staff member who did the checkout, not the checkin. old_issues doesn't record who processed the return. - library_id fell back to old_issues.branchcode (the checkout library) when checkin_library was empty, which is the case for returns made before bug 33641 (24.11). This patch: - Leaves user_id empty on backfilled checkins. - Only backfills returns with a checkin_library that still exists in branches. The rest are skipped and counted in the update output. library_id is NOT NULL, and ON DELETE CASCADE already removes the checkins of deleted libraries, so skipping them is consistent. - Takes the number of backfilled rows from $dbh->do, as $dbh->rows is not reliable on a database handle and the 'Backfilled N checkin records' message was never printed. Test plan: 1. Check out '39999000003154' to any patron and check it in 2. Reproduce the deleted library problem: $ ktd --shell k$ koha-mysql kohadev <<'SQL' DROP TABLE checkins; UPDATE old_issues SET branchcode='GONE', checkin_library='GONE'; SQL k$ updatedatabase => FAIL: 'ERROR: Broken FK constraint', and checkins is empty 3. Clean up: k$ koha-mysql kohadev -e "UPDATE old_issues SET branchcode='CPL', checkin_library='CPL'" k$ updatedatabase 4. Set RecordStaffUserOnCheckout to 'Record' 5. Create a staff patron 'staffb' (password 'Staffb1234') with the catalogue, circulate and loggedinlibrary permissions 6. As 'koha', check out '39999000003154' to any patron 7. Log in as 'staffb', use 'Set library' to switch to 'Fairview', and check '39999000003154' in 8. Rebuild the table using only the backfill, simulating a return made before bug 33641 (24.11): k$ koha-mysql kohadev <<'SQL' DROP TABLE checkins; UPDATE old_issues SET checkin_library=NULL; SQL k$ updatedatabase k$ koha-mysql kohadev -e "SELECT user_id, library_id FROM checkins WHERE checkout_id=(SELECT MAX(issue_id) FROM old_issues)" => FAIL: user_id is 51 ('koha', who checked it out) and library_id is 'CPL' (where it was checked out) 9. Apply this patch 10. Repeat 2 => SUCCESS: No error. The update reports the skipped old_issues 11. Repeat 3 and 8 => SUCCESS: No checkin is backfilled for that return, and the update reports the skipped old_issues 12. Set checkin_library back, then repeat the DROP TABLE, updatedatabase and SELECT from 8 (without the UPDATE): k$ koha-mysql kohadev -e "UPDATE old_issues SET checkin_library='FFL' WHERE checkin_library IS NULL" => SUCCESS: The backfilled checkin has user_id NULL and library_id 'FFL' 13. Sign off :-D Assisted-by: Opus 5.5 (Anthropic) -- You are receiving this mail because: You are watching all bug changes.