fix(caldav): don't prune locally-created events on sync (#2706)
The CalDAV pull prunes events in the synced calendar+window whose UID the server didn't just return, to propagate upstream deletions. But CalendarEvent had no field distinguishing a server-pulled row from a locally-created one, so the prune also deleted events that were never on the server: events created by the agent / email triage (which never write back to the server) and UI events whose best-effort write-back failed. Result: silent, unrecoverable loss of the user's appointments (hard db.delete, no soft-delete). Add an 'origin' column to calendar_events (lightweight idempotent migration, mirroring _migrate_add_calendar_is_utc), set origin='caldav' on rows the sync inserts/updates, and gate the prune on origin == 'caldav'. Locally-created events carry origin NULL and are never pruned. On the first sync after the migration nothing is pruned (all rows NULL until re-marked), erring toward keeping data. Fixes #2704
This commit is contained in:
@@ -1485,6 +1485,10 @@ class CalendarEvent(TimestampMixin, Base):
|
||||
importance = Column(String, default="normal") # low | normal | high | critical
|
||||
event_type = Column(String, nullable=True) # work | personal | health | travel | meal | social | admin | other
|
||||
last_pinged = Column(DateTime, nullable=True) # last time the assistant pinged about this event
|
||||
# "caldav" = pulled from a CalDAV server (so the sync may prune it when it
|
||||
# vanishes upstream). NULL/local = created locally (agent, email triage, or
|
||||
# a UI event whose write-back failed) and must NOT be pruned by the sync.
|
||||
origin = Column(String, nullable=True, index=True)
|
||||
|
||||
calendar = relationship("CalendarCal", back_populates="events")
|
||||
|
||||
@@ -1617,6 +1621,7 @@ def init_db():
|
||||
_migrate_seed_email_account()
|
||||
_migrate_add_calendar_metadata()
|
||||
_migrate_add_calendar_is_utc()
|
||||
_migrate_add_calendar_origin()
|
||||
_migrate_encrypt_email_passwords()
|
||||
_migrate_encrypt_signatures()
|
||||
_migrate_encrypt_endpoint_keys()
|
||||
@@ -1759,6 +1764,28 @@ def _migrate_add_calendar_is_utc():
|
||||
logging.getLogger(__name__).warning(f"is_utc migration failed: {e}")
|
||||
|
||||
|
||||
def _migrate_add_calendar_origin():
|
||||
"""Add `origin` to calendar_events so the CalDAV sync can tell server-pulled
|
||||
rows (prunable when they vanish upstream) from locally-created ones (agent /
|
||||
email triage / failed write-back), which must never be pruned. Idempotent."""
|
||||
import sqlite3
|
||||
db_path = DATABASE_URL.replace("sqlite:///", "")
|
||||
if not os.path.exists(db_path):
|
||||
return
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.execute("PRAGMA table_info(calendar_events)")
|
||||
columns = [row[1] for row in cursor.fetchall()]
|
||||
if columns and "origin" not in columns:
|
||||
conn.execute("ALTER TABLE calendar_events ADD COLUMN origin TEXT")
|
||||
conn.execute("CREATE INDEX IF NOT EXISTS ix_calendar_events_origin ON calendar_events(origin)")
|
||||
conn.commit()
|
||||
logging.getLogger(__name__).info("Migrated: added 'origin' column to calendar_events")
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
logging.getLogger(__name__).warning(f"calendar_events.origin migration failed: {e}")
|
||||
|
||||
|
||||
def _migrate_add_calendar_metadata():
|
||||
"""Add importance/event_type/last_pinged columns to calendar_events table."""
|
||||
import sqlite3
|
||||
|
||||
Reference in New Issue
Block a user