feat(nextcloud): hard-delete SourceRoot + reliable delete sync
Two related fixes for the Nextcloud library lifecycle.
1. DELETE /api/v1/nextcloud/source-roots/{id} now actually deletes
the SourceRoot, every Folder under it, and every Photo in those
folders (Nextcloud files untouched). Was a soft-deactivate
(is_active=false) that left the rows around forever, so re-adding
the same path resurrected ghosts and prune-missing reported zero.
Returns {deleted_photos, deleted_folders}; the Settings UI toasts
the count and invalidates photos/folders/stats so cached lists
don't show ghosts. photo_tags and heap_photos already cascade via
ON DELETE CASCADE; FolderShare uses a stringly-typed folder_id
with no FK so cleaned up explicitly.
2. The watcher (watch_folders task) was getting killed every five
minutes by the global task_soft_time_limit=300 in app/tasks/celery.py
despite passing soft_time_limit=None on the decorator (None falls
back to the worker default in this Celery version). Override with
soft_time_limit=0, time_limit=0 (= unlimited) so the watch loop
actually stays alive. The 'Soft time limit (300s) exceeded' /
'Worker exited prematurely' lines should stop in worker-watcher
logs.
3. Added discard_missing_photos() in services/cleanup.py — a soft
variant of prune_missing_photos that walks every present source
root, checks os.path.exists for each non-discarded Photo, and
flips is_discarded=true on the missing ones (UPDATE not DELETE).
Wired as discard_missing_photos_beat in tasks/scan.py and
scheduled every 30 min via celery beat. Beat runs in-process on
worker-watcher (--beat flag in compose) — there's only ever one
watcher and we don't need a separate container.
Hard delete remains manual via prune-missing for users who want to
review before committing. The beat catch-up only soft-discards (file
gone -> mule-image trash, restorable).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -30,12 +30,14 @@ from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import delete, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import get_db
|
||||
from app.dependencies import get_current_user
|
||||
from app.models.folders import SourceRoot, Folder
|
||||
from app.models.photos import Photo
|
||||
from app.models.sharing import FolderShare
|
||||
from app.models.user import User
|
||||
from app.services.nextcloud_dav import (
|
||||
NEXTCLOUD_USERS_ROOT,
|
||||
@@ -267,16 +269,25 @@ async def create_nextcloud_source_root(
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/source-roots/{source_root_id}", status_code=204)
|
||||
@router.delete("/source-roots/{source_root_id}")
|
||||
async def delete_nextcloud_source_root(
|
||||
source_root_id: str,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Soft-delete: deactivate the SourceRoot and orphan its photo rows
|
||||
(mark them is_discarded). The actual files in Nextcloud are NOT
|
||||
touched — this is just unwiring the user's view of the library.
|
||||
The user can re-add the same path later to restart scanning."""
|
||||
"""Hard-delete: drop the SourceRoot row and cascade-delete every
|
||||
Folder and Photo underneath it. The actual files in Nextcloud are
|
||||
NOT touched — this is just unwiring the user's library in mule-image.
|
||||
|
||||
Implementation notes:
|
||||
- photo_tags and heap_photos cascade via DB-level ON DELETE CASCADE,
|
||||
so deleting Photo rows is enough to clean those up.
|
||||
- FolderShare uses a stringly-typed folder_id (no FK), so we have
|
||||
to clean those rows by hand for both the SourceRoot itself and
|
||||
every Folder we're about to delete.
|
||||
- Chunked at 500 to mirror prune_missing_photos so postgres doesn't
|
||||
choke on a 21k-photo source root.
|
||||
"""
|
||||
sr = (await db.execute(
|
||||
select(SourceRoot).where(
|
||||
SourceRoot.id == source_root_id,
|
||||
@@ -291,11 +302,49 @@ async def delete_nextcloud_source_root(
|
||||
detail="This endpoint only manages Nextcloud-rooted SourceRoots",
|
||||
)
|
||||
|
||||
# Soft-deactivate. Existing photos stay in the DB but won't appear
|
||||
# in active queries (folders.py filters by is_active=true).
|
||||
sr.is_active = False
|
||||
folder_ids = (await db.execute(
|
||||
select(Folder.id).where(Folder.source_root_id == sr.id)
|
||||
)).scalars().all()
|
||||
|
||||
photo_ids: list[str] = []
|
||||
if folder_ids:
|
||||
photo_ids = (await db.execute(
|
||||
select(Photo.id).where(Photo.folder_id.in_(folder_ids))
|
||||
)).scalars().all()
|
||||
|
||||
CHUNK = 500
|
||||
for i in range(0, len(photo_ids), CHUNK):
|
||||
chunk = photo_ids[i:i + CHUNK]
|
||||
await db.execute(delete(Photo).where(Photo.id.in_(chunk)))
|
||||
|
||||
# FolderShare rows: not a real FK, clean both 'source_root' and
|
||||
# 'folder' typed shares pointing at anything we're tearing down.
|
||||
await db.execute(
|
||||
delete(FolderShare).where(
|
||||
FolderShare.folder_id == sr.id,
|
||||
FolderShare.folder_type == 'source_root',
|
||||
)
|
||||
)
|
||||
if folder_ids:
|
||||
await db.execute(
|
||||
delete(FolderShare).where(
|
||||
FolderShare.folder_id.in_(folder_ids),
|
||||
FolderShare.folder_type == 'folder',
|
||||
)
|
||||
)
|
||||
await db.execute(delete(Folder).where(Folder.id.in_(folder_ids)))
|
||||
|
||||
await db.delete(sr)
|
||||
await db.commit()
|
||||
return None
|
||||
|
||||
logger.info(
|
||||
f"Deleted SourceRoot {sr.id} ({sr.name}): "
|
||||
f"{len(photo_ids)} photos, {len(folder_ids)} folders"
|
||||
)
|
||||
return {
|
||||
"deleted_photos": len(photo_ids),
|
||||
"deleted_folders": len(folder_ids),
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user