CREATE TABLE IF NOT EXISTS qna_version_table (
  id                BIGSERIAL   PRIMARY   KEY,
  qna_id            BIGINT      REFERENCES   qna_table         (id) NOT NULL,
  external_id       TEXT        NOT NULL,
  task_id           BIGINT      REFERENCES   task_table        (id) NOT NULL,

  state_id          BIGINT      REFERENCES   qna_state_table   (id) NOT NULL,

  data              TEXT        NOT NULL,

  run_id            VARCHAR,

  start_ts          BIGINT,

  version           BIGINT      NOT NULL,

  deleted           BOOLEAN     NOT NULL,

  UNIQUE (task_id, external_id, version) -- Note this automatically adds an index on task_id, external_id, version
);

ALTER TABLE qna_table ADD COLUMN IF NOT EXISTS deleted BOOLEAN NOT NULL DEFAULT FALSE;

CREATE INDEX IF NOT EXISTS ix_qna_task_id_deleted ON qna_table (task_id, deleted);
