diff --git a/modules/docs/__init__.py b/modules/docs/__init__.py index 723b0eaf..1f6d8afe 100644 --- a/modules/docs/__init__.py +++ b/modules/docs/__init__.py @@ -27,14 +27,22 @@ async def doc_modeler(progress_callback: Callable[[int], None] = None): total_processed = 0 while True: with get_db_session(commit=True) as session: - file_groups = session.query(FileGroup, Doc).filter( + file_groups = session.query(FileGroup, Doc) \ + .outerjoin(Doc, Doc.file_group_id == FileGroup.id) \ + .filter( or_( *[FileGroup.mimetype == mt for mt in DOC_MIMETYPES], *[FileGroup.primary_path.like(f'%{suffix}') for suffix in COMIC_BOOK_SUFFIXES], ), - FileGroup.indexed == False, - ).outerjoin(Doc, Doc.file_group_id == FileGroup.id) \ - .limit(DOC_PROCESSING_LIMIT) + # Model any doc that has no Doc row yet (even if `apply_indexers` already set + # `indexed=True` before this modeler ran), or any doc explicitly flagged for + # re-indexing. Gating solely on `indexed == False` left already-indexed docs + # permanently unmodeled and invisible in /api/docs. + or_( + Doc.id.is_(None), + FileGroup.indexed == False, + ), + ).limit(DOC_PROCESSING_LIMIT) file_groups: List[Tuple[FileGroup, Doc]] = list(file_groups) processed = 0 diff --git a/modules/docs/test/test_doc_modeler.py b/modules/docs/test/test_doc_modeler.py index b302a5c9..8a129df1 100644 --- a/modules/docs/test/test_doc_modeler.py +++ b/modules/docs/test/test_doc_modeler.py @@ -116,6 +116,32 @@ async def test_doc_do_model_sets_model(test_session, example_pdf): assert file_group.indexed is True +@pytest.mark.asyncio +async def test_doc_modeler_models_indexed_filegroup_without_doc(async_client, test_session, test_directory, + example_pdf): + """A doc-mimetype FileGroup that was already indexed (indexed=True) but never modeled (no Doc + row) must still be picked up by the doc_modeler. + + Regression: apply_indexers marks every FileGroup indexed=True. If a doc file gets indexed + before it is modeled (e.g. the modeler errored, or the file was imported already-indexed), the + old `indexed == False` gate excluded it forever -- leaving thousands of docs invisible in + /api/docs even though they exist on disk. + """ + # Simulate the stuck production state: the FileGroup has been swept by apply_indexers + # (indexed=True) but has no Doc row and no model. + file_group = FileGroup.from_paths(test_session, example_pdf) + file_group.indexed = True + file_group.model = None + test_session.commit() + assert test_session.query(Doc).count() == 0, 'Precondition: no Doc row exists yet' + + await doc_modeler() + + doc: Doc = test_session.query(Doc).one() + assert doc.file_group_id == file_group.id + assert doc.file_group.model == 'doc' + + @pytest.mark.asyncio async def test_doc_modeler_processes_more_than_batch_limit(async_client, test_session, test_directory): """Doc modeler processes more than one batch of files (limit=10)."""