fix(profiles): commit the whole profile delete cascade once

delete_generations_by_profile (and the version sweep under it) committed on
their own, so a failure between the generation sweep and the profile row
left the profile listed with its history already gone. Both helpers take a
commit flag now, and delete_profile passes commit=False so the single
db.commit() at its end publishes the entire cascade. Other callers keep the
default and are unchanged.
This commit is contained in:
jamiepine
2026-10-04 00:19:41 +00:00
committed by capy-ai-staging[bot]
parent 012c957f4d
commit f0f3802b79
3 changed files with 19 additions and 11 deletions
+13 -8
View File
@@ -18,7 +18,7 @@ from .. import config
logger = logging.getLogger(__name__)
def _delete_generation_children(generation_id: str, db: Session) -> None:
def _delete_generation_children(generation_id: str, db: Session, commit: bool = True) -> None:
"""Remove the rows that reference a generation, plus any version audio files.
Story items and versions both point at the generation by a non-null FK.
@@ -28,7 +28,7 @@ def _delete_generation_children(generation_id: str, db: Session) -> None:
from . import versions as versions_mod
db.query(DBStoryItem).filter_by(generation_id=generation_id).delete()
versions_mod.delete_versions_for_generation(generation_id, db)
versions_mod.delete_versions_for_generation(generation_id, db, commit=commit)
def _get_versions_for_generations(generation_ids: list[str], db: Session) -> dict:
@@ -349,14 +349,18 @@ async def delete_failed_generations(db: Session) -> int:
async def delete_generations_by_profile(
profile_id: str,
db: Session,
commit: bool = True,
) -> int:
"""
Delete all generations for a profile.
Args:
profile_id: Profile ID
db: Database session
commit: Commit at the end. Pass False when the caller owns the
transaction (e.g. deleting the profile itself) so the whole
cascade lands in one commit.
Returns:
Number of generations deleted
"""
@@ -365,7 +369,7 @@ async def delete_generations_by_profile(
count = 0
for generation in generations:
# Delete associated version files and rows first
_delete_generation_children(generation.id, db)
_delete_generation_children(generation.id, db, commit=commit)
# Delete audio file
audio_path = config.resolve_storage_path(generation.audio_path)
@@ -380,9 +384,10 @@ async def delete_generations_by_profile(
# Delete from database
db.delete(generation)
count += 1
db.commit()
if commit:
db.commit()
return count