-- ============================================================
-- 31_agent_ords_backoffice_ux_metadata.sql
-- Upgrade existing backoffice metadata for the screen explanations.
-- Run as ADMIN before deploying this application version.
-- ============================================================
WHENEVER SQLERROR EXIT SQL.SQLCODE
SET ECHO ON
SET FEEDBACK ON
SET DEFINE OFF

BEGIN
  EXECUTE IMMEDIATE 'ALTER TABLE cb_protected_object ADD (description VARCHAR2(200))';
EXCEPTION
  WHEN OTHERS THEN
    IF SQLCODE != -1430 THEN
      RAISE;
    END IF;
END;
/

BEGIN
  EXECUTE IMMEDIATE q'!
    CREATE TABLE cb_vpd_policy_note (
      object_owner VARCHAR2(128) NOT NULL,
      object_name  VARCHAR2(128) NOT NULL,
      policy_name  VARCHAR2(128) NOT NULL,
      description  VARCHAR2(500) NOT NULL,
      updated_at   TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
      CONSTRAINT cb_vpd_policy_note_pk PRIMARY KEY (object_owner, object_name, policy_name)
    )!';
EXCEPTION
  WHEN OTHERS THEN
    IF SQLCODE != -955 THEN
      RAISE;
    END IF;
END;
/

BEGIN
  EXECUTE IMMEDIATE q'!
    CREATE TABLE cb_vpd_filter_note (
      function_owner VARCHAR2(128) NOT NULL,
      function_name  VARCHAR2(128) NOT NULL,
      description    VARCHAR2(500) NOT NULL,
      updated_at     TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
      CONSTRAINT cb_vpd_filter_note_pk PRIMARY KEY (function_owner, function_name)
    )!';
EXCEPTION
  WHEN OTHERS THEN
    IF SQLCODE != -955 THEN
      RAISE;
    END IF;
END;
/

COMMIT;
PROMPT === Backoffice UX metadata upgrade complete ===
EXIT;
