-- Theme B (P1-14): add Heartbeat.errorClass
-- =========================================================
-- This project uses `prisma db push` rather than Prisma's migrate
-- workflow, so changes to schema.prisma are applied by an operator
-- running `npx prisma db push` against the target database. This file
-- documents the SQL the operator should run AHEAD of time on
-- production-class environments where db-push's auto-generated DDL
-- needs to be reviewed.
--
-- It also serves as the reversible-rollback record per CLAUDE.md
-- "every schema change ships with tested down".
--
-- Apply order:
--   1. Run UP block below (or `prisma db push`)
--   2. Deploy the new application code
--   3. Optionally run BACKFILL to mark legacy rows
--
-- Rollback (DOWN):
--   1. Deploy the previous application code (drops references to errorClass)
--   2. Run DOWN block below

-- ============================================================
-- UP
-- ============================================================

ALTER TABLE `Heartbeat`
  ADD COLUMN `errorClass` VARCHAR(32) NULL;

-- ============================================================
-- BACKFILL (optional, but recommended for clean rule-matching)
-- ============================================================
-- Set errorClass='UNKNOWN' on existing down rows so notification
-- rules with an errorClass condition don't silently miss them.

UPDATE `Heartbeat`
   SET `errorClass` = 'UNKNOWN'
 WHERE `status` = 'down'
   AND `errorClass` IS NULL;

-- ============================================================
-- DOWN
-- ============================================================
-- Reverse the UP. Safe to run at any time — column is nullable
-- and only read by application code (no FK constraints).

ALTER TABLE `Heartbeat`
  DROP COLUMN `errorClass`;
