-- Audit Low finding: preserving forensic history.
--
-- The `execution_logs.script_id` foreign key was created with ON DELETE
-- CASCADE in 0001_init. Deleting a script then wipes every log row that
-- ever referenced it — including the rows that captured the failure
-- that motivated the delete. Switch to ON DELETE SET NULL so the
-- forensic history survives and operators can still look up "what did
-- this dead script do before we removed it" by id.

ALTER TABLE execution_logs
    DROP CONSTRAINT IF EXISTS execution_logs_script_id_fkey;

ALTER TABLE execution_logs
    ALTER COLUMN script_id DROP NOT NULL;

ALTER TABLE execution_logs
    ADD CONSTRAINT execution_logs_script_id_fkey
    FOREIGN KEY (script_id) REFERENCES scripts(id) ON DELETE SET NULL;
