-- 022_carddav_shares.sql — user-connected CardDAV address books. -- -- 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 CardDAV endpoint (any RFC 6352 service; Settings -> CardDAV) -- materializes each address book as its own Collection whose members are -- bookmarks -- one per contact, carrying the card's DAV .vcf URL. -- CardDavAddressbook maps book-href to Collection; CardDavItem is the -- (address book, card path) ledger with the exact WebDavItem/CalDavEvent -- semantics: bookmarkId ON DELETE SET NULL leaves a tombstone so a -- user-deleted contact bookmark is never resurrected, and a card removed on -- the server detaches membership only. -- -- Credentials follow the house discipline (username clear, app password -- encrypted via internal/crypto, masked, write-only). Production dials pin -- to 443 like the existing Contacts CardDAV sync (review M-3/CR-01: Basic -- auth credentials never travel to arbitrary ports), carry the credential in -- the Authorization header only, and refuse redirects. Mutations carry the -- row version (409 on stale); engine bookkeeping never bumps it. CREATE TABLE IF NOT EXISTS "CardDavConnection" ( 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 "CardDavConnection_ownerId_idx" ON "CardDavConnection"("ownerId"); CREATE TABLE IF NOT EXISTS "CardDavAddressbook" ( id text PRIMARY KEY, "connectionId" text NOT NULL REFERENCES "CardDavConnection"(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 "CardDavAddressbook_connectionId_idx" ON "CardDavAddressbook"("connectionId"); CREATE TABLE IF NOT EXISTS "CardDavItem" ( id text PRIMARY KEY, "addressbookId" text NOT NULL REFERENCES "CardDavAddressbook"(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 '', "createdAt" timestamptz NOT NULL DEFAULT now(), "updatedAt" timestamptz NOT NULL DEFAULT now(), UNIQUE ("addressbookId", path) ); CREATE INDEX IF NOT EXISTS "CardDavItem_addressbookId_idx" ON "CardDavItem"("addressbookId");