from __future__ import annotations from dataclasses import dataclass from sqlalchemy import text from sqlalchemy.ext.asyncio import AsyncSession from .schema import CONTENT_SCHEMA, resolve_content_schema @dataclass(frozen=True, slots=True) class ModuleContentCleanupResult: module_code: str content_count: int search_facet_count: int search_document_count: int module_content_count: int content_attribute_count: int content_item_count: int class ModuleContentCleaner: """ Removes seeded content associated with one module without removing module/template/attribute-definition/search-facet configuration. Shared content is preserved: * the target module's module_contents associations are removed; * content_attributes/content_items are deleted only when the content has no remaining module_contents association. Callers own the transaction boundary. This class never commits. """ def __init__(self, session: AsyncSession, *, schema: str = CONTENT_SCHEMA) -> None: self._session = session self._schema = resolve_content_schema(schema) async def cleanup(self, module_code: str) -> ModuleContentCleanupResult: normalized_code = module_code.strip() if not normalized_code: raise ValueError("module_code must be a non-empty string") module_id = await self._session.scalar( text( f""" SELECT id FROM {self._schema}.modules WHERE code = :module_code """ ), {"module_code": normalized_code}, ) if module_id is None: raise ValueError(f"Module not found: {normalized_code}") await self._session.execute( text( """ CREATE TEMP TABLE IF NOT EXISTS tmp_acupedia_module_cleanup_content ( content_id uuid PRIMARY KEY ) ON COMMIT DROP """ ) ) await self._session.execute( text("TRUNCATE TABLE tmp_acupedia_module_cleanup_content") ) await self._session.execute( text( f""" INSERT INTO tmp_acupedia_module_cleanup_content (content_id) SELECT DISTINCT mc.content_id FROM {self._schema}.module_contents mc WHERE mc.module_id = :module_id ON CONFLICT (content_id) DO NOTHING """ ), {"module_id": module_id}, ) content_count = int( await self._session.scalar( text("SELECT count(*) FROM tmp_acupedia_module_cleanup_content") ) or 0 ) search_facet_result = await self._session.execute( text( f""" DELETE FROM {self._schema}.content_search_facets csf USING tmp_acupedia_module_cleanup_content t WHERE csf.content_id = t.content_id """ ) ) search_document_result = await self._session.execute( text( f""" DELETE FROM {self._schema}.content_search_documents csd USING tmp_acupedia_module_cleanup_content t WHERE csd.content_id = t.content_id """ ) ) module_content_result = await self._session.execute( text(f"DELETE FROM {self._schema}.module_contents WHERE module_id = :module_id"), {"module_id": module_id}, ) attribute_result = await self._session.execute( text( f""" DELETE FROM {self._schema}.content_attributes ca USING tmp_acupedia_module_cleanup_content t WHERE ca.content_id = t.content_id AND NOT EXISTS ( SELECT 1 FROM {self._schema}.module_contents mc WHERE mc.content_id = t.content_id ) """ ) ) content_result = await self._session.execute( text( f""" DELETE FROM {self._schema}.content_items ci USING tmp_acupedia_module_cleanup_content t WHERE ci.id = t.content_id AND NOT EXISTS ( SELECT 1 FROM {self._schema}.module_contents mc WHERE mc.content_id = t.content_id ) """ ) ) return ModuleContentCleanupResult( module_code=normalized_code, content_count=content_count, search_facet_count=max(search_facet_result.rowcount or 0, 0), search_document_count=max(search_document_result.rowcount or 0, 0), module_content_count=max(module_content_result.rowcount or 0, 0), content_attribute_count=max(attribute_result.rowcount or 0, 0), content_item_count=max(content_result.rowcount or 0, 0), )