mirror of
https://github.com/myronblair/kino-app
synced 2026-07-27 21:18:43 -05:00
Code review fix: Library Cleanup left orphaned rows on episode delete
Fixing a missing-file or duplicate-import episode only deleted the episode itself, leaving its episode_progress/subtitle rows behind for the next scan to catch as separate orphaned_subtitles/dangling findings. Consolidated movie/episode/track deletion (with all their dependent rows) into one _delete_content() helper, used consistently by missing_files, orphaned_episodes, and duplicate_paths.
This commit is contained in:
+21
-11
@@ -105,22 +105,33 @@ async def scan(db, videos_dir: Path, hls_dir: Path) -> Dict[str, List[Dict]]:
|
|||||||
return findings
|
return findings
|
||||||
|
|
||||||
|
|
||||||
|
async def _delete_content(db, content_type: str, item_id: str) -> None:
|
||||||
|
"""Deletes a movie/episode/track and every row that references it, so a Fix never
|
||||||
|
leaves orphans behind for the next scan to catch on a second pass."""
|
||||||
|
if content_type == "movie":
|
||||||
|
await db.movies.delete_one({"id": item_id})
|
||||||
|
await db.watchlist.delete_many({"movie_id": item_id})
|
||||||
|
await db.progress.delete_many({"movie_id": item_id})
|
||||||
|
await db.subtitles.delete_many({"movie_id": item_id})
|
||||||
|
elif content_type == "episode":
|
||||||
|
await db.episodes.delete_one({"id": item_id})
|
||||||
|
await db.episode_progress.delete_many({"episode_id": item_id})
|
||||||
|
await db.subtitles.delete_many({"episode_id": item_id})
|
||||||
|
elif content_type == "track":
|
||||||
|
await db.tracks.delete_one({"id": item_id})
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unknown content_type: {content_type}")
|
||||||
|
|
||||||
|
|
||||||
async def fix_one(db, videos_dir: Path, hls_dir: Path, category: str, item: Dict) -> None:
|
async def fix_one(db, videos_dir: Path, hls_dir: Path, category: str, item: Dict) -> None:
|
||||||
content_type = item.get("content_type")
|
content_type = item.get("content_type")
|
||||||
item_id = item.get("id")
|
item_id = item.get("id")
|
||||||
|
|
||||||
if category == "missing_files":
|
if category == "missing_files":
|
||||||
coll = {"movie": db.movies, "episode": db.episodes, "track": db.tracks}[content_type]
|
await _delete_content(db, content_type, item_id)
|
||||||
await coll.delete_one({"id": item_id})
|
|
||||||
if content_type == "movie":
|
|
||||||
await db.watchlist.delete_many({"movie_id": item_id})
|
|
||||||
await db.progress.delete_many({"movie_id": item_id})
|
|
||||||
await db.subtitles.delete_many({"movie_id": item_id})
|
|
||||||
|
|
||||||
elif category == "orphaned_episodes":
|
elif category == "orphaned_episodes":
|
||||||
await db.episodes.delete_one({"id": item_id})
|
await _delete_content(db, "episode", item_id)
|
||||||
await db.episode_progress.delete_many({"episode_id": item_id})
|
|
||||||
await db.subtitles.delete_many({"episode_id": item_id})
|
|
||||||
|
|
||||||
elif category == "orphaned_hls":
|
elif category == "orphaned_hls":
|
||||||
shutil.rmtree(hls_dir / item_id, ignore_errors=True)
|
shutil.rmtree(hls_dir / item_id, ignore_errors=True)
|
||||||
@@ -132,8 +143,7 @@ async def fix_one(db, videos_dir: Path, hls_dir: Path, category: str, item: Dict
|
|||||||
}})
|
}})
|
||||||
|
|
||||||
elif category == "duplicate_paths":
|
elif category == "duplicate_paths":
|
||||||
coll = {"movie": db.movies, "episode": db.episodes, "track": db.tracks}[content_type]
|
await _delete_content(db, content_type, item_id)
|
||||||
await coll.delete_one({"id": item_id})
|
|
||||||
|
|
||||||
elif category == "orphaned_subtitles":
|
elif category == "orphaned_subtitles":
|
||||||
sub = await db.subtitles.find_one({"id": item_id}, {"_id": 0})
|
sub = await db.subtitles.find_one({"id": item_id}, {"_id": 0})
|
||||||
|
|||||||
Reference in New Issue
Block a user