-- v1.1.8 F1 — drop the plaintext realtime_signing_key column.
--
-- v1.1.7 added at-rest encryption (migration 0025) and a startup task
-- that backfilled encryption over the plaintext column. Once every
-- existing row has an encrypted counterpart, the plaintext column is
-- pure dead weight (and a footgun: anyone with DB-read access can
-- still read the signing key for any app that lived through the
-- migration).
--
-- The guard refuses to drop if any row still has a plaintext value
-- without an encrypted counterpart. This makes the migration safe to
-- replay and forces operators who skipped v1.1.7 to apply it first:
-- the v1.1.7 startup task is the only thing that knows how to
-- encrypt the existing plaintext rows.
--
-- Operators upgrading from v1.1.6 or earlier MUST apply v1.1.7
-- first (and let its startup encryption sweep complete) before
-- applying this migration. The CHANGELOG and HANDBACK make this
-- mandatory; the guard below is the belt-and-suspenders.

DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM app_secrets
        WHERE realtime_signing_key IS NOT NULL
          AND realtime_signing_key_encrypted IS NULL
    ) THEN
        RAISE EXCEPTION
            'v1.1.8 migration 0032 refused: unencrypted realtime_signing_key rows still present. Apply v1.1.7 first and ensure its startup encryption sweep has completed.';
    END IF;
END$$;

ALTER TABLE app_secrets DROP COLUMN IF EXISTS realtime_signing_key;
