Fix duplicate CalDAV sync UIDs
Track uncommitted CalendarEvent rows during a CalDAV sync batch so duplicate UIDs update the pending row instead of inserting twice.
This commit is contained in:
@@ -133,6 +133,10 @@ def _sync_blocking(owner: str, url: str, username: str, password: str) -> dict:
|
|||||||
from icalendar import Calendar as iCal
|
from icalendar import Calendar as iCal
|
||||||
|
|
||||||
seen_uids = set()
|
seen_uids = set()
|
||||||
|
# Track events added to the session but not yet committed so
|
||||||
|
# duplicate UIDs within the same batch are updated, not re-inserted
|
||||||
|
# (which would violate the UNIQUE constraint on commit).
|
||||||
|
pending: dict = {}
|
||||||
try:
|
try:
|
||||||
objs = remote_cal.date_search(start=start, end=end, expand=False)
|
objs = remote_cal.date_search(start=start, end=end, expand=False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -182,7 +186,7 @@ def _sync_blocking(owner: str, url: str, username: str, password: str) -> dict:
|
|||||||
else ""
|
else ""
|
||||||
)
|
)
|
||||||
|
|
||||||
existing = db.query(CalendarEvent).filter(
|
existing = pending.get(uid_val) or db.query(CalendarEvent).filter(
|
||||||
CalendarEvent.uid == uid_val,
|
CalendarEvent.uid == uid_val,
|
||||||
).first()
|
).first()
|
||||||
if existing:
|
if existing:
|
||||||
@@ -196,7 +200,7 @@ def _sync_blocking(owner: str, url: str, username: str, password: str) -> dict:
|
|||||||
existing.is_utc = row_is_utc
|
existing.is_utc = row_is_utc
|
||||||
existing.rrule = rrule
|
existing.rrule = rrule
|
||||||
else:
|
else:
|
||||||
db.add(CalendarEvent(
|
new_ev = CalendarEvent(
|
||||||
uid=uid_val,
|
uid=uid_val,
|
||||||
calendar_id=local_cal.id,
|
calendar_id=local_cal.id,
|
||||||
summary=summary,
|
summary=summary,
|
||||||
@@ -207,7 +211,9 @@ def _sync_blocking(owner: str, url: str, username: str, password: str) -> dict:
|
|||||||
all_day=all_day,
|
all_day=all_day,
|
||||||
is_utc=row_is_utc,
|
is_utc=row_is_utc,
|
||||||
rrule=rrule,
|
rrule=rrule,
|
||||||
))
|
)
|
||||||
|
db.add(new_ev)
|
||||||
|
pending[uid_val] = new_ev
|
||||||
result["events"] += 1
|
result["events"] += 1
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user