Files
Void-Homelab/lib/db/migrations/001_core.sql
root 3ca1509935 fix(schema): enforce cross-space FK on tasks.project_id via composite key
Security review flagged that tasks.project_id could reference a project in
a different space. Added composite FK (project_id, space_id) -> projects(id, space_id)
with ON DELETE SET NULL (project_id) so a deleted project leaves the task in
its space with project_id NULL rather than orphaning into a NULL space.

Added two regression tests: cross-space FK rejection + cascade behavior.
2026-05-31 02:14:20 +10:00

51 lines
1.8 KiB
SQL

CREATE TABLE spaces (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
slug text NOT NULL UNIQUE,
name text NOT NULL,
description text,
theme text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE projects (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
space_id uuid NOT NULL REFERENCES spaces(id) ON DELETE CASCADE,
slug text NOT NULL,
name text NOT NULL,
description text,
status text NOT NULL DEFAULT 'active'
CHECK (status IN ('idea','active','paused','done','abandoned')),
started_at timestamptz,
completed_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (space_id, slug),
-- Composite key target for tasks.(project_id, space_id) FK below
UNIQUE (id, space_id)
);
CREATE TABLE tasks (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
space_id uuid NOT NULL REFERENCES spaces(id) ON DELETE CASCADE,
project_id uuid,
-- Cross-space FK enforcement: a task's project (if set) must live in the same space.
FOREIGN KEY (project_id, space_id) REFERENCES projects(id, space_id)
ON DELETE SET NULL (project_id),
title text NOT NULL,
body text,
status text NOT NULL DEFAULT 'todo'
CHECK (status IN ('todo','doing','blocked','done')),
priority int,
due_at timestamptz,
position int,
completed_at timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_projects_space ON projects(space_id);
CREATE INDEX idx_tasks_space ON tasks(space_id);
CREATE INDEX idx_tasks_project ON tasks(project_id);
CREATE INDEX idx_tasks_status ON tasks(status) WHERE status <> 'done';