Define Supabase schema for way-forward items
epic-structured-post-session-report-foundation-task-002 — Create the Supabase database migration for the way_forward_items table including columns for item_id, report_id (foreign key to post_session_reports), assigned_to, description, due_date, status, created_at. Add appropriate RLS policies for coordinator and peer mentor access. Ensure referential integrity with the reports table.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Use Supabase CLI migration workflow: `supabase migration new way_forward_items`. The coordinator org-scope RLS policy requires a join — use a subquery against the organisation_members table (or equivalent) to identify the coordinator's org_id and restrict access accordingly. Avoid using security definer functions unless necessary; prefer straightforward RLS joins for auditability. The status CHECK constraint values should be agreed with the product team before migration is applied to avoid a later schema change.
Ensure the migration references the exact table name used in task-001 for post_session_reports — do not assume the name; verify from the prior migration file.
Testing Requirements
Write SQL-based integration tests using Supabase's local dev environment (supabase start). Test each RLS policy: (1) coordinator can SELECT/INSERT/UPDATE items in own org, (2) coordinator cannot access items in another org, (3) peer mentor can SELECT own items, (4) peer mentor cannot SELECT items not assigned to them or not on their report, (5) peer mentor cannot DELETE any item, (6) cascade delete removes items when parent report is deleted. Verify status CHECK constraint rejects invalid values. Verify NOT NULL constraints reject missing description and report_id.
Run migration rollback and confirm clean teardown.
Supabase RLS policies for multi-org report access may be more complex than anticipated — coordinators need cross-peer-mentor access within their org but not across orgs, and draft reports should be invisible to coordinators until submitted. Misconfigured RLS could expose sensitive health data or block legitimate access.
Mitigation & Contingency
Mitigation: Define and test RLS policies in isolation before writing repository code. Create a dedicated SQL migration file with policy definitions and an automated integration test suite that verifies each role's access boundaries using real Supabase auth tokens.
Contingency: If RLS proves too complex to express declaratively, implement application-level access control in the repository layer with explicit org and role checks, and add a security audit task before the feature goes to production.
The org field config JSON stored in Supabase may lack a stable, versioned schema contract. If different organisations have drifted to different field-definition formats, org-field-config-loader will fail silently or crash, breaking form rendering for those orgs.
Mitigation & Contingency
Mitigation: Define a canonical JSON Schema for field config and validate all existing org configs against it before implementation begins. Store a schema version field in every config record and handle version migrations explicitly in the loader.
Contingency: If existing configs are too heterogeneous, implement a config normalisation pass in org-field-config-loader that coerces known variants to the canonical format, logging warnings for fields that cannot be normalised so operations can fix them in the admin console.
TTL-based schema cache invalidation may cause peer mentors to use stale field definitions for up to the TTL window after an admin updates the org config, potentially collecting data against outdated field structures.
Mitigation & Contingency
Mitigation: Set a conservative TTL (e.g. 15 minutes) and expose a manual cache-bust mechanism triggered on app foreground-resume. Document the maximum staleness window in the admin console so org admins know to plan config changes outside active reporting windows.
Contingency: If stale schema causes a data quality incident, add a Supabase Realtime subscription to the org config table that invalidates the cache immediately on any config update.