-- Cohorts table CREATE TABLE IF NOT EXISTS cohorts ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), organization_id UUID NOT NULL, name TEXT NOT NULL, description TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT cohorts_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE ); -- User-Cohort relationship table (M:N) CREATE TABLE IF NOT EXISTS user_cohorts ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), cohort_id UUID NOT NULL, user_id UUID NOT NULL, assigned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT user_cohorts_cohort_id_fkey FOREIGN KEY (cohort_id) REFERENCES cohorts(id) ON DELETE CASCADE, CONSTRAINT user_cohorts_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, CONSTRAINT user_cohorts_unique UNIQUE (cohort_id, user_id) ); -- Indexes for performance CREATE INDEX IF NOT EXISTS idx_cohorts_organization_id ON cohorts(organization_id); CREATE INDEX IF NOT EXISTS idx_user_cohorts_cohort_id ON user_cohorts(cohort_id); CREATE INDEX IF NOT EXISTS idx_user_cohorts_user_id ON user_cohorts(user_id);