-- ===================================================================== -- project_db 마이그레이션 006 — 멘션(@태그) -- ===================================================================== -- 멱등(idempotent). DROP/TRUNCATE 없음. -- -- 실행: -- docker exec -i postgres-db psql -U postgres -d project_db \ -- < scripts/sql/project_db_006_mentions.sql -- -- 설명란(HTML) 또는 댓글(평문)에서 @로 멤버를 지정하면 여기에 1행이 쌓인다. -- 수신함(/project/inbox)의 "나를 태그한 업무" 목록이 이 테이블을 읽는다. -- ===================================================================== \set ON_ERROR_STOP on \connect project_db CREATE TABLE IF NOT EXISTS task_mentions ( id BIGSERIAL PRIMARY KEY, task_id BIGINT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE, comment_id BIGINT REFERENCES task_comments(id) ON DELETE CASCADE, mentioned_email TEXT NOT NULL, actor_email TEXT NOT NULL DEFAULT '', source TEXT NOT NULL DEFAULT 'description', -- description | comment is_read BOOLEAN NOT NULL DEFAULT FALSE, created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS idx_task_mentions_email ON task_mentions (mentioned_email, is_read, created_at DESC); CREATE INDEX IF NOT EXISTS idx_task_mentions_task ON task_mentions (task_id); -- 같은 곳(설명란 = comment_id NULL, 또는 같은 댓글)에서 같은 사람을 여러 번 -- 저장하지 않는다. comment_id 가 NULL 이면 UNIQUE 제약이 안 걸리므로 -- COALESCE 표현식 인덱스를 쓴다(NULL = 설명란 멘션). CREATE UNIQUE INDEX IF NOT EXISTS uq_task_mentions_once ON task_mentions (task_id, COALESCE(comment_id, 0), mentioned_email); GRANT SELECT, INSERT, UPDATE, DELETE ON task_mentions TO project_app; GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO project_app; SELECT 'project_db 006 mentions ready' AS status;