Create user_roles Supabase table schema
epic-role-based-access-control-data-infrastructure-task-001 — Design and implement the user_roles database table in Supabase, including columns for user_id, org_unit_id, role_name (peer_mentor, coordinator, org_admin), and org_id. Add appropriate indexes, foreign key constraints, and a get_my_roles RPC function that returns the calling user's role assignments scoped to their organization units.
Acceptance Criteria
Technical Requirements
Implementation Notes
Write the schema as a numbered SQL migration file (e.g., 20260001_create_user_roles.sql) so it can be applied idempotently. Use IF NOT EXISTS on CREATE TABLE and CREATE INDEX to allow re-runs. The get_my_roles function should use RETURNS TABLE(...) for typed return shape rather than RETURNS SETOF for easier Dart deserialization. Set SECURITY DEFINER and explicit search_path on the function.
Do not add RLS policies in this migration — those are handled in task-002. Keep the org_unit_id nullable to support org-level roles that are not scoped to a specific unit.
Testing Requirements
Verify migration with direct Supabase SQL queries in the dashboard. Confirm CHECK constraint rejects invalid role_name values (e.g., 'superuser'). Confirm foreign key cascade by inserting a test user, creating a role assignment, then deleting the user and verifying the role row is removed. Test get_my_roles() RPC by calling it as two different auth users and confirming each only receives their own rows.
Test that is_active = false rows are excluded from results.
The get_my_roles RPC call adds a network round-trip immediately after login, potentially increasing the time before the home screen renders. If Supabase RPC is slow or the roles table lacks proper indexing, users with multiple org affiliations could experience noticeable delays.
Mitigation & Contingency
Mitigation: Index user_roles on user_id and org_unit_id. Use JWT claim extraction as the primary fast path; fall back to the RPC only when claims are absent or stale. Set a 3-second timeout with a fallback to cached roles.
Contingency: If RPC latency exceeds acceptable thresholds in production, pre-fetch and embed roles into the session JWT at login time via a Supabase Auth hook, eliminating the post-login RPC entirely.
Users who belong to multiple organizations (e.g., a coordinator in one NHF chapter who is also a peer mentor in another) may have conflicting role assignments. The repository layer must correctly scope roles to the active organization context set during the organization selection step, or it could return roles from the wrong org.
Mitigation & Contingency
Mitigation: Always filter role queries by the active org_unit_id stored in the tenant session. Write integration tests that simulate multi-org users and verify only the correct org's roles are returned.
Contingency: If org-scoping logic is found to be incorrect during QA, add an explicit org_unit_id parameter to get_my_roles RPC and require the client to always pass the active org context, making the scoping explicit rather than inferred.