Phase 1-3 of decoupling: - path_setup.py adds project root to sys.path - Events-owned models in events/models/ (calendars with all related models) - All imports updated: shared.infrastructure, shared.db, shared.browser, etc. - Calendar uses container_type/container_id instead of post_id FK - CalendarEntryPost uses content_type/content_id (generic content refs) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
26 lines
900 B
Python
26 lines
900 B
Python
from sqlalchemy import select, update
|
|
from models.calendars import CalendarEntry
|
|
|
|
from sqlalchemy import func
|
|
|
|
async def adopt_session_entries_for_user(session, user_id: int, session_id: str | None) -> None:
|
|
if not session_id:
|
|
return
|
|
# (Optional) Mark any existing entries for this user as deleted to avoid duplicates
|
|
await session.execute(
|
|
update(CalendarEntry)
|
|
.where(CalendarEntry.deleted_at.is_(None), CalendarEntry.user_id == user_id)
|
|
.values(deleted_at=func.now())
|
|
)
|
|
# Reassign anonymous entries to the user
|
|
result = await session.execute(
|
|
select(CalendarEntry).where(
|
|
CalendarEntry.deleted_at.is_(None),
|
|
CalendarEntry.session_id == session_id
|
|
)
|
|
)
|
|
anon_entries = result.scalars().all()
|
|
for entry in anon_entries:
|
|
entry.user_id = user_id
|
|
# No commit here; caller will commit
|