nexus/migrations/20260101000013_create_reports.sql
2026-01-26 11:58:04 -05:00

47 lines
1.6 KiB
SQL

-- Migration 013: Create reports tables
-- Report: aggregates completed services/projects for a team member on a date
-- Used to calculate labor share for payment
-- Team member work report
CREATE TABLE reports (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
team_profile_id UUID NOT NULL REFERENCES team_profiles(id) ON DELETE CASCADE,
date DATE NOT NULL,
-- One report per team member per date
UNIQUE (team_profile_id, date)
);
-- M2M: Report to services
CREATE TABLE report_services (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
report_id UUID NOT NULL REFERENCES reports(id) ON DELETE CASCADE,
service_id UUID NOT NULL REFERENCES services(id) ON DELETE CASCADE,
UNIQUE (report_id, service_id)
);
-- M2M: Report to projects
CREATE TABLE report_projects (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
report_id UUID NOT NULL REFERENCES reports(id) ON DELETE CASCADE,
project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
UNIQUE (report_id, project_id)
);
-- Indexes
CREATE INDEX idx_reports_team_profile ON reports(team_profile_id);
CREATE INDEX idx_reports_date ON reports(date);
CREATE INDEX idx_report_services_report ON report_services(report_id);
CREATE INDEX idx_report_services_service ON report_services(service_id);
CREATE INDEX idx_report_projects_report ON report_projects(report_id);
CREATE INDEX idx_report_projects_project ON report_projects(project_id);