Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
perf(health): delete replaced health rows in one statement (#4226)
Raghav Chamadiya41e1cce8b20e
diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/health/findings.py b/packages/core/src/repowise/core/persistence/crud/analysis/health/findings.pyindex 7309ad79a..59b599871 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/health/findings.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/health/findings.py@@ -8,7 +8,7 @@ from collections.abc import Iterable, Sequencefrom datetime import datetimefrom typing import Any-from sqlalchemy import func, or_, select+from sqlalchemy import delete, func, or_, selectfrom sqlalchemy.ext.asyncio import AsyncSessionfrom .....analysis.finding_registry import excluded_types@@ -116,6 +116,11 @@ async def _fold_into_triaged(kept.updated_at = now+async def _delete_open(session: AsyncSession, scope: list[Any]) -> None:+ """Delete the open rows in *scope* in one statement; triaged rows stay."""+ await session.execute(delete(HealthFinding).where(*scope, HealthFinding.status == "open"))++async def _insert_keeping_triage(session: AsyncSession,repository_id: str,@@ -160,13 +165,7 @@ async def save_health_findings(dataclasses or plain dicts."""scope = [HealthFinding.repository_id == repository_id]- existing = await session.execute(- select(HealthFinding).where(*scope, HealthFinding.status == "open")- )- for row in existing.scalars().all():- await session.delete(row)- await session.flush()-+ await _delete_open(session, scope)await _insert_keeping_triage(session, repository_id, findings, scope)@@ -202,13 +201,7 @@ async def replace_governance_findings(HealthFinding.repository_id == repository_id,HealthFinding.biomarker_type.in_(list(GOVERNANCE_BIOMARKERS)),]- existing = await session.execute(- select(HealthFinding).where(*scope, HealthFinding.status == "open")- )- for row in existing.scalars().all():- await session.delete(row)- await session.flush()-+ await _delete_open(session, scope)await _insert_keeping_triage(session, repository_id, findings, scope)@@ -233,12 +226,7 @@ async def upsert_health_findings(]if dimension is not None:predicates.append(HealthFinding.dimension == dimension)- existing = await session.execute(- select(HealthFinding).where(*predicates, HealthFinding.status == "open")- )- for row in existing.scalars().all():- await session.delete(row)- await session.flush()+ await _delete_open(session, predicates)allowed = set(file_paths)scoped = [diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/health/metrics.py b/packages/core/src/repowise/core/persistence/crud/analysis/health/metrics.pyindex 68f5a925f..5b5db5066 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/health/metrics.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/health/metrics.py@@ -5,7 +5,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKING, Any-from sqlalchemy import select+from sqlalchemy import delete, selectfrom sqlalchemy.ext.asyncio import AsyncSessionif TYPE_CHECKING:@@ -21,7 +21,7 @@ from .....analysis.health.scope import scores_languagefrom .....analysis.health.scoring import SCORE_FIELDSfrom .....test_paths import is_test_related_pathfrom ....models import GraphNode, HealthFileMetric, HealthFinding, _new_uuid-from ..._shared import _BATCH_SIZE+from ..._shared import _BATCH_SIZE, _batch_delete_infrom .exclude import _filter_excluded_paths, health_exclude_specfrom .findings import get_deduction_by_path@@ -74,12 +74,9 @@ async def save_health_metrics(so ``Repository.head_commit`` cannot answer "how old is this score". Omitted→ NULL, which reads as "not recorded" rather than "current"."""- existing = await session.execute(- select(HealthFileMetric).where(HealthFileMetric.repository_id == repository_id)+ await session.execute(+ delete(HealthFileMetric).where(HealthFileMetric.repository_id == repository_id))- for row in existing.scalars().all():- await session.delete(row)- await session.flush()for i in range(0, len(metrics), _BATCH_SIZE):batch = metrics[i : i + _BATCH_SIZE]@@ -210,19 +207,10 @@ async def prune_unscored_health_rows(session: AsyncSession, repository_id: str)if not stale:return 0removed = 0- for i in range(0, len(stale), _BATCH_SIZE):- batch = stale[i : i + _BATCH_SIZE]- for model in (HealthFileMetric, HealthFinding):- rows = await session.execute(- select(model).where(- model.repository_id == repository_id,- model.file_path.in_(batch),- )- )- for row in rows.scalars().all():- await session.delete(row)- removed += 1- await session.flush()+ for model in (HealthFileMetric, HealthFinding):+ removed += await _batch_delete_in(+ session, model, model.file_path, stale, prefilter=(model.repository_id == repository_id,)+ )return removed