-- Google Calendar integration -- Store Google Calendar connection per user CREATE TABLE IF NOT EXISTS google_calendar_connections ( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE UNIQUE, access_token TEXT NOT NULL, refresh_token TEXT NOT NULL, token_expires_at TIMESTAMPTZ NOT NULL, calendar_id TEXT, -- specific calendar to sync with created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() ); -- Link local events to Google Calendar events ALTER TABLE calendar_events ADD COLUMN IF NOT EXISTS google_event_id TEXT; ALTER TABLE calendar_events ADD COLUMN IF NOT EXISTS synced_at TIMESTAMPTZ; -- Index CREATE INDEX IF NOT EXISTS idx_google_calendar_user ON google_calendar_connections(user_id); CREATE INDEX IF NOT EXISTS idx_calendar_events_google ON calendar_events(google_event_id) WHERE google_event_id IS NOT NULL; -- RLS ALTER TABLE google_calendar_connections ENABLE ROW LEVEL SECURITY; -- Users can only see/manage their own connections CREATE POLICY "Users can view own Google connection" ON google_calendar_connections FOR SELECT USING (auth.uid() = user_id); CREATE POLICY "Users can manage own Google connection" ON google_calendar_connections FOR ALL USING (auth.uid() = user_id);