Create peer_mentor_status_history audit table
epic-peer-mentor-pause-foundation-task-002 — Create Supabase migration for the peer_mentor_status_history table capturing mentor_id, previous_status, new_status, changed_by, changed_at, and reason columns. Add foreign key constraints to peer_mentors and users tables. Create index on mentor_id and changed_at for efficient history queries.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Reuse the mentor_status enum type defined in task-001 for both previous_status and new_status columns. The job_run_id column links audit records back to automated job executions (from CertificationExpiryJob) — this is critical for correlating bulk auto-pause events. The ON DELETE CASCADE on mentor_id is a pragmatic choice for a GDPR right-to-erasure scenario where a mentor's entire record is removed; however, in practice soft-deletes are preferred — document this in the column comment.
Ensure the migration references the enum type after it has been created (i.e. this migration must declare its dependency on task-001's migration timestamp in the file naming convention).
Testing Requirements
Post-migration smoke tests: (1) INSERT a test history record with all required fields and assert it is retrievable, (2) INSERT a record with changed_by=NULL (system action) and assert it succeeds, (3) DELETE the parent peer_mentor row and assert the history rows are also deleted (CASCADE), (4) DELETE the changed_by user and assert the history row's changed_by becomes NULL (SET NULL), (5) run EXPLAIN on SELECT * FROM peer_mentor_status_history WHERE mentor_id = $1 ORDER BY changed_at DESC and assert the composite index is used. Include these as a migration smoke-test script.
Supabase RLS policies for status reads and writes must correctly distinguish between a mentor editing their own status and a coordinator editing another mentor's status within the same chapter. Incorrect policies could allow cross-chapter data leakage or silently block legitimate status updates, causing hard-to-diagnose runtime failures.
Mitigation & Contingency
Mitigation: Write RLS policies with explicit role checks (auth.uid() = mentor_id OR chapter_coordinator_check()) and verify with integration tests that cover same-chapter coordinator access, cross-chapter denial, and self-access. Review policies with a second developer before merging.
Contingency: If policy errors surface after merge, temporarily widen policy to coordinator role globally while a targeted fix is authored; use Supabase audit logs to trace any unauthorised access during the interim.
CoordinatorNotificationService must correctly resolve which coordinator(s) are responsible for a given mentor's chapter. If the chapter-coordinator mapping is incomplete or a mentor belongs to multiple chapters (as with NHF multi-chapter memberships), the service could fail to notify or duplicate notifications to the wrong coordinators.
Mitigation & Contingency
Mitigation: Use the existing chapter membership data model and query all active coordinator roles for each of the mentor's chapters. Add a de-duplication step before dispatch. Write integration tests with fixtures covering single-chapter, multi-chapter, and no-coordinator edge cases.
Contingency: If resolution logic proves too complex at this stage, fall back to notifying all coordinators in the organisation until a proper chapter-scoped resolver can be delivered in a follow-up task.
Adding new columns to peer_mentors in production could conflict with existing application code that does SELECT * queries if new non-nullable columns without defaults are introduced, causing unexpected failures in unrelated screens.
Mitigation & Contingency
Mitigation: Make all new columns nullable or provide safe defaults. Use additive migration strategy with no column renames or drops. Run migration against a staging copy of production data before applying to live.
Contingency: Prepare a rollback migration script that drops only the new columns; coordinate with the team to deploy the rollback and hotfix immediately if production issues are detected.