DROP INDEX IF EXISTS conversations_requirement_idx;
ALTER TABLE conversations DROP COLUMN IF EXISTS requirement_id;

-- 还原 0011 的 requirement_prototypes；仅回迁 phase='prototyping' 行，
-- planning/auditing 产物在回滚时丢弃（旧 schema 无处安放）。
CREATE TABLE requirement_prototypes (
  id             UUID PRIMARY KEY,
  requirement_id UUID NOT NULL REFERENCES requirements(id) ON DELETE CASCADE,
  version        INT  NOT NULL,
  content        TEXT NOT NULL,
  note           TEXT NOT NULL DEFAULT '',
  created_by     UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
  created_at     TIMESTAMPTZ NOT NULL DEFAULT now(),
  UNIQUE (requirement_id, version)
);
CREATE INDEX idx_requirement_prototypes_req ON requirement_prototypes(requirement_id);
INSERT INTO requirement_prototypes (id, requirement_id, version, content, note, created_by, created_at)
SELECT id, requirement_id, version, content, note, created_by, created_at
FROM requirement_artifacts WHERE phase = 'prototyping';
DROP TABLE requirement_artifacts;
