-- 021_caldav_calendars.sql — user-connected CalDAV calendars. -- -- Additive and idempotent; schema.sql carries the identical tables, so this -- is a structural no-op on a fresh database (db-converge proves both paths -- land the same). -- -- A connected CalDAV calendar (any RFC 4791 service; Settings → CalDAV) -- materializes as a Collection whose members are bookmarks — one per VEVENT -- inside the rolling sync window, carrying the event's DAV .ics URL. -- CalDavCalendar is the server-calendar↔Collection map; CalDavEvent is the -- (calendar, event-path)↔bookmark ledger — the exact WebDavItem semantics: -- bookmarkId ON DELETE SET NULL leaves a tombstone so a user-deleted event -- bookmark is never resurrected, and removal on the server (or scrolling out -- of the window) detaches membership only. -- -- Credentials follow the house discipline: username in the clear, app -- password encrypted at rest via internal/crypto, masked in every response, -- write-only. Dials go through internal/netguard — user-supplied hosts get -- no trusted-range exemption — Basic auth in a header only, redirects -- refused. Mutations carry the row version (409 on stale); engine bookkeeping -- (collection links, lastSyncedAt, lastError) never bumps version. CREATE TABLE IF NOT EXISTS "CalDavConnection" ( id text PRIMARY KEY, "ownerId" text NOT NULL REFERENCES "User"(id) ON DELETE CASCADE, name text NOT NULL, url text NOT NULL, username text NOT NULL, secret text NOT NULL, enabled boolean NOT NULL DEFAULT true, "lastSyncedAt" timestamptz, "lastError" text, version integer NOT NULL DEFAULT 1, "createdAt" timestamptz NOT NULL DEFAULT now(), "updatedAt" timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS "CalDavConnection_ownerId_idx" ON "CalDavConnection"("ownerId"); CREATE TABLE IF NOT EXISTS "CalDavCalendar" ( id text PRIMARY KEY, "connectionId" text NOT NULL REFERENCES "CalDavConnection"(id) ON DELETE CASCADE, href text NOT NULL, "displayName" text NOT NULL DEFAULT '', "collectionId" text REFERENCES "Collection"(id) ON DELETE SET NULL, "createdAt" timestamptz NOT NULL DEFAULT now(), "updatedAt" timestamptz NOT NULL DEFAULT now(), UNIQUE ("connectionId", href) ); CREATE INDEX IF NOT EXISTS "CalDavCalendar_connectionId_idx" ON "CalDavCalendar"("connectionId"); CREATE TABLE IF NOT EXISTS "CalDavEvent" ( id text PRIMARY KEY, "calendarId" text NOT NULL REFERENCES "CalDavCalendar"(id) ON DELETE CASCADE, path text NOT NULL, "bookmarkId" text REFERENCES "Bookmark"(id) ON DELETE SET NULL, etag text NOT NULL DEFAULT '', uid text NOT NULL DEFAULT '', "startAt" timestamptz, "createdAt" timestamptz NOT NULL DEFAULT now(), "updatedAt" timestamptz NOT NULL DEFAULT now(), UNIQUE ("calendarId", path) ); CREATE INDEX IF NOT EXISTS "CalDavEvent_calendarId_idx" ON "CalDavEvent"("calendarId");