Files
HealthLog/prisma/migrations/0479_retry_failed_local_pdf_index/migration.sql
T
Marc-André Bombeck 1ea14e9100 fix(documents): index once more the PDFs a failed local read left out
While two pdf.js versions broke each other, a local text read after a
page render failed and was recorded as decrypt-error, and a scanned PDF
that had to be rasterized for the provider was recorded as raster-failed.
Neither has a content index, and on an instance without an AI provider
nothing offers to index it again.

Migration 0479 clears the attempt time of exactly those PDFs and keeps
their reason, a state no index attempt leaves behind. A new boot
discovery, document-index-retry, enqueues the upload's auto-index job for
each such row, so it runs the same provider-first, local-fallback decision
under the same consent rules; a deferred import stays local because its
row says so. The attempt writes its time back, so every document drops out
after one try, and one that really cannot be read records its reason again
and stays settled. An account with the documents module off keeps its
marks until the module is on.

An integration test seeds both kinds of damage, runs each migration's
statement and only what a boot runs on its own, and sees a preview and an
index appear, while a blank page, a broken ciphertext and a file that is
not a PDF settle after one retry and are not found by the next boot.

Refs #1277
2026-10-24 19:50:20 +02:00

33 lines
1.6 KiB
SQL

-- Queue one more content-index attempt for every PDF whose last one failed in
-- a way a pdf.js clash produces.
--
-- Until this release the server could load two pdf.js versions. Once a page
-- render had claimed the in-process pdf.js worker, every later local text read
-- failed until a restart, which the index records as `decrypt-error`; the
-- other way round, a scanned PDF the picked provider could only read as images
-- failed to rasterize and was recorded as `raster-failed`. Neither document has
-- a content index, so neither is found by search, and on an instance without a
-- configured AI provider nothing offers to index it again.
--
-- The marker is the state no index attempt ever leaves behind: a failure
-- reason with no attempt time (the recorder always writes both). The boot
-- discovery `document-index-retry` enqueues the auto-index job for exactly
-- those rows, through the same provider-first, local-fallback decision and the
-- same consent rules as an upload. The attempt writes its time back, so the
-- row drops out whatever the outcome: a document that really cannot be read
-- records its reason again and stays settled.
--
-- Only PDFs without an index and with a recorded attempt are touched; running
-- this again queues those that failed once more for one more attempt.
UPDATE "inbound_documents" AS d
SET "last_index_attempt_at" = NULL
WHERE d."deleted_at" IS NULL
AND d."mime_type" = 'application/pdf'
AND d."last_index_outcome" IN ('decrypt-error', 'raster-failed')
AND d."last_index_attempt_at" IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM "document_content_index" AS c
WHERE c."document_id" = d."id"
);