-- v1.1.7: encrypt the realtime signing key at rest (two-phase).
--
-- Phase 1 (this migration + the v1.1.7 startup task):
--   * add NULL-able encrypted columns,
--   * drop the NOT NULL on the plaintext column so newly-generated keys
--     can be stored encrypted-only,
--   * the application startup task `migrate_plaintext_keys` encrypts each
--     existing plaintext key into the new columns (plaintext is LEFT in
--     place during the compat window for rollback safety).
--
-- The `RealtimeAuthorityImpl` read path prefers the encrypted columns and
-- falls back to plaintext, so SSE keeps working throughout.
--
-- Phase 2 (v1.1.8): once all rows are migrated, a follow-up migration
-- drops the plaintext `realtime_signing_key` column.

ALTER TABLE app_secrets
    ADD COLUMN realtime_signing_key_encrypted BYTEA,
    ADD COLUMN realtime_signing_key_nonce     BYTEA;

-- New keys (post-v1.1.7) are stored encrypted-only, so the plaintext
-- column must accept NULL.
ALTER TABLE app_secrets
    ALTER COLUMN realtime_signing_key DROP NOT NULL;
