fix: Terminate sync immediately when project is deleted (#366) · basicmachines-co/basic-memory@729a5a3 · GitHub
Skip to content

Commit 729a5a3

Browse files
phernandezclaude
andauthored
fix: Terminate sync immediately when project is deleted (#366)
Signed-off-by: phernandez <paul@basicmachines.co> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 434cdf2 commit 729a5a3

5 files changed

Lines changed: 143 additions & 1 deletion

File tree

src/basic_memory/repository/entity_repository.py

Lines changed: 14 additions & 1 deletion

src/basic_memory/services/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,18 @@ class DirectoryOperationError(Exception):
2020
"""Raised when directory operations fail"""
2121

2222
pass
23+
24+
25+
class SyncFatalError(Exception):
26+
"""Raised when sync encounters a fatal error that prevents continuation.
27+
28+
Fatal errors include:
29+
- Project deleted during sync (FOREIGN KEY constraint)
30+
- Database corruption
31+
- Critical system failures
32+
33+
When this exception is raised, the entire sync operation should be terminated
34+
immediately rather than attempting to continue with remaining files.
35+
"""
36+
37+
pass

src/basic_memory/sync/sync_service.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from basic_memory.repository import EntityRepository, RelationRepository, ObservationRepository
2323
from basic_memory.repository.search_repository import SearchRepository
2424
from basic_memory.services import EntityService, FileService
25+
from basic_memory.services.exceptions import SyncFatalError
2526
from basic_memory.services.link_resolver import LinkResolver
2627
from basic_memory.services.search_service import SearchService
2728
from basic_memory.services.sync_status_service import sync_status_tracker, SyncStatus
@@ -514,6 +515,13 @@ async def sync_file(
514515
return entity, checksum
515516

516517
except Exception as e:
518+
# Check if this is a fatal error (or caused by one)
519+
# Fatal errors like project deletion should terminate sync immediately
520+
if isinstance(e, SyncFatalError) or isinstance(e.__cause__, SyncFatalError):
521+
logger.error(f"Fatal sync error encountered, terminating sync: path={path}")
522+
raise
523+
524+
# Otherwise treat as recoverable file-level error
517525
error_msg = str(e)
518526
logger.error(f"Failed to sync file: path={path}, error={error_msg}")
519527

tests/repository/test_entity_repository_upsert.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from basic_memory.models.knowledge import Entity
77
from basic_memory.repository.entity_repository import EntityRepository
88
from basic_memory.repository.project_repository import ProjectRepository
9+
from basic_memory.services.exceptions import SyncFatalError
910

1011

1112
@pytest.mark.asyncio
@@ -436,3 +437,32 @@ async def test_upsert_entity_permalink_conflict_within_project_only(session_make
436437
assert result1.project_id == project1.id
437438
assert result2.project_id == project2.id
438439
assert result3.project_id == project1.id
440+
441+
442+
@pytest.mark.asyncio
443+
async def test_upsert_entity_with_invalid_project_id(entity_repository: EntityRepository):
444+
"""Test that upserting with non-existent project_id raises clear error.
445+
446+
This tests the fix for issue #188 where sync fails with FOREIGN KEY constraint
447+
violations when a project is deleted during sync operations.
448+
"""
449+
# Create entity with non-existent project_id
450+
entity = Entity(
451+
title="Test Entity",
452+
entity_type="note",
453+
file_path="test.md",
454+
permalink="test",
455+
project_id=99999, # This project doesn't exist
456+
content_type="text/markdown",
457+
created_at=datetime.now(timezone.utc),
458+
updated_at=datetime.now(timezone.utc),
459+
)
460+
461+
# Should raise SyncFatalError with clear message about missing project
462+
with pytest.raises(SyncFatalError) as exc_info:
463+
await entity_repository.upsert_entity(entity)
464+
465+
error_msg = str(exc_info.value)
466+
assert "project_id=99999 does not exist" in error_msg
467+
assert "project may have been deleted" in error_msg.lower()
468+
assert "sync will be terminated" in error_msg.lower()

tests/sync/test_sync_service.py

Lines changed: 76 additions & 0 deletions

0 commit comments

Comments
 (0)