-- TuneVault schema — Oracle Autonomous DB 26ai
-- Generated from the LIVE Neon schema (107 tables, 1300 columns).
-- Identifiers are UNQUOTED => Oracle uppercase. db/oracle-client.js must
-- lowercase result keys so app code keeps using row.user_id.
-- Run order: schema.sql -> constraints.sql -> indexes.sql

CREATE TABLE MIGRATIONS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  NAME                               VARCHAR2(255 CHAR) NOT NULL,
  APPLIED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP,
  CONSTRAINT MIGRATIONS_PKEY PRIMARY KEY (ID),
  CONSTRAINT MIGRATIONS_NAME_KEY UNIQUE (NAME)
);

CREATE TABLE ACTIVITY_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  COMPANY_ID                         VARCHAR2(100 CHAR),
  USER_ID                            NUMBER(10),
  USER_EMAIL                         VARCHAR2(255 CHAR),
  USER_NAME                          VARCHAR2(255 CHAR),
  USER_ROLE                          VARCHAR2(50 CHAR),
  ACTION_TYPE                        VARCHAR2(50 CHAR) NOT NULL,
  DETAIL                             JSON DEFAULT '{}' NOT NULL,
  CONNECTION_ID                      NUMBER(10),
  CONNECTION_NAME                    VARCHAR2(255 CHAR),
  RESULT                             VARCHAR2(20 CHAR) DEFAULT 'success' NOT NULL,
  DURATION_MS                        NUMBER(10),
  IP_ADDRESS                         VARCHAR2(64 CHAR),
  USER_AGENT                         VARCHAR2(500 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  COMPANY_DOMAIN                     VARCHAR2(255 CHAR),
  CONSTRAINT ACTIVITY_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE ADDM_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  CREATED_BY                         NUMBER(10) NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  TASK_NAME                          VARCHAR2(4000 CHAR),
  TASK_ID                            NUMBER(10),
  DB_ID                              VARCHAR2(4000 CHAR),
  CONTAINER                          VARCHAR2(4000 CHAR),
  BEGIN_SNAP_ID                      NUMBER(10),
  END_SNAP_ID                        NUMBER(10),
  BEGIN_SNAP_TIME                    TIMESTAMP(6) WITH TIME ZONE,
  END_SNAP_TIME                      TIMESTAMP(6) WITH TIME ZONE,
  DB_TIME_SECONDS                    NUMBER,
  AVG_ACTIVE_SESSIONS                NUMBER,
  FINDINGS                           JSON DEFAULT '[]' NOT NULL,
  RAW_REPORT_TEXT                    CLOB,
  AI_COMMENTARY                      CLOB,
  IS_IDLE                            BOOLEAN DEFAULT FALSE NOT NULL,
  RUN_ERROR                          VARCHAR2(4000 CHAR),
  SNAPSHOT_MS                        NUMBER(10),
  ANALYSIS_MS                        NUMBER(10),
  TOTAL_MS                           NUMBER(10),
  CONSTRAINT ADDM_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE ADMIN_EXPORTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  EXPORT_TYPE                        VARCHAR2(4000 CHAR) NOT NULL,
  FORMAT                             VARCHAR2(4000 CHAR) DEFAULT 'custom' NOT NULL,
  INITIATED_BY                       VARCHAR2(4000 CHAR) NOT NULL,
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  BYTE_COUNT                         NUMBER(19),
  DURATION_MS                        NUMBER(10),
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'started' NOT NULL,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  CONSTRAINT ADMIN_EXPORTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT ADMIN_EXPORTS_EXPORT_TYPE_CHECK CHECK (EXPORT_TYPE IN ('database', 'assets')),
  CONSTRAINT ADMIN_EXPORTS_STATUS_CHECK CHECK (STATUS IN ('started', 'completed', 'failed'))
);

CREATE TABLE ADVISOR_FINDINGS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  FETCHED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  ADDM_TASKS                         JSON DEFAULT '[]' NOT NULL,
  FINDINGS                           JSON DEFAULT '[]' NOT NULL,
  RECOMMENDATIONS                    JSON DEFAULT '[]' NOT NULL,
  SQL_TUNING_TASKS                   JSON DEFAULT '[]' NOT NULL,
  AI_SUMMARY                         CLOB,
  LICENSED                           BOOLEAN DEFAULT TRUE NOT NULL,
  NOT_LICENSED_REASON                VARCHAR2(4000 CHAR),
  FETCH_ERROR                        VARCHAR2(4000 CHAR),
  CONSTRAINT ADVISOR_FINDINGS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE AGENT_CHANNEL_STATE (
  AGENT_ID                           NUMBER(10) NOT NULL,
  LAST_POLL_AT                       TIMESTAMP(6) WITH TIME ZONE,
  LAST_HEARTBEAT_AT                  TIMESTAMP(6) WITH TIME ZONE,
  PENDING_COMMAND_ID                 VARCHAR2(4000 CHAR),
  PENDING_COMMAND_PAYLOAD            JSON,
  PENDING_COMMAND_ENQUEUED_AT        TIMESTAMP(6) WITH TIME ZONE,
  CLAIM_HOLDER                       VARCHAR2(4000 CHAR),
  CLAIM_EXPIRES_AT                   TIMESTAMP(6) WITH TIME ZONE,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT AGENT_CHANNEL_STATE_PKEY PRIMARY KEY (AGENT_ID)
);

CREATE TABLE AGENT_COMMAND_QUEUE (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  AGENT_ID                           NUMBER(10) NOT NULL,
  COMMAND_TYPE                       VARCHAR2(4000 CHAR) DEFAULT 'proxy_request' NOT NULL,
  PAYLOAD                            JSON DEFAULT '{}' NOT NULL,
  ENQUEUED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CLAIMED_AT                         TIMESTAMP(6) WITH TIME ZONE,
  CLAIM_HOLDER                       VARCHAR2(4000 CHAR),
  CLAIM_EXPIRES_AT                   TIMESTAMP(6) WITH TIME ZONE,
  COMPLETED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'pending' NOT NULL,
  RESULT                             JSON,
  CONSTRAINT AGENT_COMMAND_QUEUE_PKEY PRIMARY KEY (ID),
  CONSTRAINT AGENT_COMMAND_QUEUE_STATUS_CHECK CHECK (STATUS IN ('pending', 'claimed', 'completed', 'failed'))
);

CREATE TABLE AGENT_COMMAND_RESULTS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10),
  COMMAND                            VARCHAR2(128 CHAR) NOT NULL,
  OUTPUT                             VARCHAR2(4000 CHAR),
  EXIT_CODE                          NUMBER(10),
  STATUS                             VARCHAR2(16 CHAR) DEFAULT 'pending' NOT NULL,
  REQUESTED_BY                       NUMBER(10),
  REQUESTED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  COMPLETED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  CONSTRAINT AGENT_COMMAND_RESULTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE AGENT_CRASH_ALERTS_SENT (
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  SENT_AT                            TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  RECIPIENT                          VARCHAR2(4000 CHAR),
  AGENT_HEALTH                       VARCHAR2(32 CHAR),
  CONSTRAINT AGENT_CRASH_ALERTS_SENT_PKEY PRIMARY KEY (CONNECTION_ID)
);

CREATE TABLE AGENT_DIAGNOSE_RUNS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  AGENT_VERSION                      VARCHAR2(32 CHAR),
  HOST                               VARCHAR2(4000 CHAR),
  DETECTED_SIDS                      JSON,
  LISTENER_SERVICES                  JSON,
  CHOSEN_SERVICE                     VARCHAR2(4000 CHAR),
  PROBES                             JSON DEFAULT '[]' NOT NULL,
  ROUNDTRIP_MS                       NUMBER(10),
  OVERALL_STATUS                     VARCHAR2(16 CHAR) DEFAULT 'pass' NOT NULL,
  PASS_COUNT                         NUMBER(5) DEFAULT 0,
  FAIL_COUNT                         NUMBER(5) DEFAULT 0,
  SKIP_COUNT                         NUMBER(5) DEFAULT 0,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT AGENT_DIAGNOSE_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE AGENT_INSTALL_FAILURES (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10),
  HOST                               VARCHAR2(4000 CHAR),
  ERROR_CLASS                        VARCHAR2(64 CHAR),
  JOURNALCTL_TAIL                    CLOB,
  INSTALL_LOG_TAIL                   CLOB,
  INSTALLER_VERSION                  VARCHAR2(16 CHAR),
  OS_INFO                            VARCHAR2(4000 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  IP_ADDRESS                         VARCHAR2(45 CHAR),
  USER_AGENT                         VARCHAR2(4000 CHAR),
  RESOLVED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  CONSTRAINT AGENT_INSTALL_FAILURES_PKEY PRIMARY KEY (ID)
);

CREATE TABLE AGENT_REG_TOKENS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TOKEN                              VARCHAR2(4000 CHAR) NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  USED                               BOOLEAN DEFAULT FALSE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  EXPIRES_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP + INTERVAL '30' MINUTE NOT NULL,
  CONSTRAINT AGENT_REG_TOKENS_PKEY PRIMARY KEY (ID),
  CONSTRAINT AGENT_REG_TOKENS_TOKEN_KEY UNIQUE (TOKEN)
);

CREATE TABLE AGENT_RESTART_EVENTS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  REASON_CODE                        VARCHAR2(4000 CHAR) NOT NULL,
  LAST_STAGE_REACHED                 VARCHAR2(4000 CHAR),
  LAST_ERROR                         VARCHAR2(4000 CHAR),
  UPTIME_SECONDS                     NUMBER(10),
  RESTART_SEQUENCE_ID                VARCHAR2(4000 CHAR),
  RECORDED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT AGENT_RESTART_EVENTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE AGENT_TUNNELS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  TUNNEL_UUID                        VARCHAR2(4000 CHAR),
  TUNNEL_NAME                        VARCHAR2(4000 CHAR),
  DNS_HOSTNAME                       VARCHAR2(4000 CHAR),
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'pending' NOT NULL,
  OS_INFO                            VARCHAR2(4000 CHAR),
  ORACLE_HOME                        VARCHAR2(4000 CHAR),
  ORACLE_SIDS                        JSON,
  PROVISIONED_AT                     TIMESTAMP(6) WITH TIME ZONE,
  CONFIRMED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  LAST_HEARTBEAT                     TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  AGENT_VERSION                      VARCHAR2(4000 CHAR),
  PDB_SERVICES                       JSON DEFAULT '[]' NOT NULL,
  AGENT_STATUS                       VARCHAR2(32 CHAR) DEFAULT 'starting',
  LAST_ORACLE_ERROR                  JSON,
  ORACLE_RETRY_COUNT                 NUMBER(10) DEFAULT 0,
  UPTIME_SECONDS                     NUMBER(10),
  ORACLE_STATUS_AT                   TIMESTAMP(6) WITH TIME ZONE,
  ORACLE_MODE                        VARCHAR2(8 CHAR) DEFAULT 'thin',
  INSTANT_CLIENT_PATH                VARCHAR2(4000 CHAR),
  VERIFIER_WORKAROUND_ACTIVE         BOOLEAN DEFAULT FALSE,
  UNINSTALLED_AT                     TIMESTAMP(6) WITH TIME ZONE,
  CONSTRAINT AGENT_TUNNELS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE AGENT_UPGRADE_AUDIT (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  FROM_VERSION                       VARCHAR2(4000 CHAR),
  TO_VERSION                         VARCHAR2(4000 CHAR) NOT NULL,
  TRIGGERED_BY                       VARCHAR2(4000 CHAR) DEFAULT 'auto-stale-policy' NOT NULL,
  TRIGGERED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  COMPLETED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'queued' NOT NULL,
  ERROR                              VARCHAR2(4000 CHAR),
  VERIFICATION_PAYLOAD               JSON,
  VERIFICATION_STATUS                VARCHAR2(4000 CHAR),
  VERIFIED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  CONSTRAINT AGENT_UPGRADE_AUDIT_PKEY PRIMARY KEY (ID),
  CONSTRAINT AGENT_UPGRADE_AUDIT_STATUS_CHECK CHECK (STATUS IN ('queued', 'in_progress', 'completed', 'failed'))
);

CREATE TABLE AGENTS_LOG_BUFFER (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  LINES                              JSON DEFAULT '[]' NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT AGENTS_LOG_BUFFER_PKEY PRIMARY KEY (ID),
  CONSTRAINT AGENTS_LOG_BUFFER_CONNECTION_ID_KEY UNIQUE (CONNECTION_ID)
);

CREATE TABLE ALERT_EVENTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  POLICY_ID                          NUMBER(10) NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  CHECK_RUN_ID                       NUMBER(10),
  TRIGGERED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  RESOLVED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  ACKNOWLEDGED_AT                    TIMESTAMP(6) WITH TIME ZONE,
  ACKNOWLEDGED_BY                    NUMBER(10),
  CURRENT_VALUE                      VARCHAR2(4000 CHAR),
  SEVERITY                           VARCHAR2(4000 CHAR) DEFAULT 'warning' NOT NULL,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'triggered' NOT NULL,
  ESCALATION_STEP                    NUMBER(10) DEFAULT 0 NOT NULL,
  NEXT_ESCALATION_AT                 TIMESTAMP(6) WITH TIME ZONE,
  NOTIFICATIONS_SENT                 JSON DEFAULT '[]' NOT NULL,
  CONSTRAINT ALERT_EVENTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT ALERT_EVENTS_STATUS_CHECK CHECK (STATUS IN ('triggered', 'acknowledged', 'escalated', 'resolved'))
);

CREATE TABLE ALERT_POLICIES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  NAME                               VARCHAR2(4000 CHAR) NOT NULL,
  CHECK_TYPE                         VARCHAR2(4000 CHAR) NOT NULL,
  CONNECTION_ID                      NUMBER(10),
  CONDITIONS                         JSON DEFAULT '[]' NOT NULL,
  SUSTAINED_MINUTES                  NUMBER(10),
  NOTIFICATION_CHANNELS              JSON DEFAULT '[]' NOT NULL,
  ESCALATION_CHAIN                   JSON DEFAULT '[]' NOT NULL,
  IS_ACTIVE                          BOOLEAN DEFAULT TRUE NOT NULL,
  IS_DEFAULT                         BOOLEAN DEFAULT FALSE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT ALERT_POLICIES_PKEY PRIMARY KEY (ID)
);

CREATE TABLE ANALYSIS_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  HEALTH_CHECK_ID                    NUMBER(10) NOT NULL,
  T0_REQUEST_RECEIVED                NUMBER(19),
  T1_METRICS_WRITTEN                 NUMBER(19),
  T2_PROMPT_ASSEMBLED                NUMBER(19),
  T3_GPT_CALL_START                  NUMBER(19),
  T4_GPT_CALL_RETURNED               NUMBER(19),
  T5_RESPONSE_PARSED                 NUMBER(19),
  T6_PERSISTED                       NUMBER(19),
  MODEL                              VARCHAR2(64 CHAR),
  PROMPT_TOKENS                      NUMBER(10),
  COMPLETION_TOKENS                  NUMBER(10),
  TOTAL_TOKENS                       NUMBER(10),
  FINISH_REASON                      VARCHAR2(32 CHAR),
  DUR_COLLECT_MS                     NUMBER(10),
  DUR_PROMPT_MS                      NUMBER(10),
  DUR_GPT_MS                         NUMBER(10),
  DUR_PARSE_MS                       NUMBER(10),
  DUR_PERSIST_MS                     NUMBER(10),
  DUR_TOTAL_MS                       NUMBER(10),
  OUTCOME                            VARCHAR2(32 CHAR),
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  RETRY_LOG                          JSON DEFAULT '[]',
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT ANALYSIS_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE ANALYTICS_EVENTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  EVENT_NAME                         VARCHAR2(100 CHAR) NOT NULL,
  PLAN                               VARCHAR2(50 CHAR),
  CHECK_ID                           NUMBER(10),
  OCCURRED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  USER_ID                            NUMBER(10),
  SESSION_ID                         VARCHAR2(64 CHAR),
  PAGE_PATH                          VARCHAR2(500 CHAR),
  REFERRER                           VARCHAR2(500 CHAR),
  PROPERTIES                         JSON,
  IP                                 VARCHAR2(45 CHAR),
  USER_AGENT                         VARCHAR2(4000 CHAR),
  IP_HASH                            VARCHAR2(64 CHAR),
  CONSTRAINT ANALYTICS_EVENTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE API_KEYS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  KEY_PREFIX                         VARCHAR2(16 CHAR) NOT NULL,
  KEY_HASH                           VARCHAR2(64 CHAR) NOT NULL,
  NAME                               VARCHAR2(100 CHAR),
  LAST_USED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  REVOKED_AT                         TIMESTAMP(6) WITH TIME ZONE,
  CONSTRAINT API_KEYS_PKEY PRIMARY KEY (ID),
  CONSTRAINT API_KEYS_KEY_HASH_KEY UNIQUE (KEY_HASH)
);

CREATE TABLE APPROVAL_REQUESTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TICKET_ID                          NUMBER(10),
  REQUESTER_ID                       NUMBER(10) NOT NULL,
  APPROVER_ROLE_ID                   NUMBER(10) NOT NULL,
  STATUS                             VARCHAR2(20 CHAR) DEFAULT 'pending' NOT NULL,
  APPROVED_BY                        NUMBER(10),
  APPROVED_AT                        TIMESTAMP(6),
  REJECTED_AT                        TIMESTAMP(6),
  REJECTION_REASON                   VARCHAR2(4000 CHAR),
  CONTEXT                            JSON DEFAULT '{}',
  CREATED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT APPROVAL_REQUESTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT APPROVAL_REQUESTS_STATUS_CHECK CHECK (STATUS IN ('pending', 'approved', 'rejected'))
);

CREATE TABLE AUDIT_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  OCCURRED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  USER_ID                            NUMBER(10),
  ACTION                             VARCHAR2(4000 CHAR) NOT NULL,
  SLUG                               VARCHAR2(4000 CHAR),
  ALLOWED                            BOOLEAN NOT NULL,
  REJECTION_REASON                   VARCHAR2(4000 CHAR),
  METADATA                           JSON DEFAULT '{}' NOT NULL,
  CONSTRAINT AUDIT_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE AUTO_REMEDIATION_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  TRIGGER_PATH                       VARCHAR2(4000 CHAR) NOT NULL,
  TRIGGERED_BY                       VARCHAR2(4000 CHAR) NOT NULL,
  CONSECUTIVE_COUNT                  NUMBER(10) NOT NULL,
  OP_KEY                             VARCHAR2(4000 CHAR) NOT NULL,
  OP_LABEL                           VARCHAR2(4000 CHAR) NOT NULL,
  OUTCOME                            VARCHAR2(4000 CHAR) NOT NULL,
  EXIT_CODE                          NUMBER(10),
  STDOUT_TAIL                        CLOB,
  TUNEOPS_TICKET_ID                  VARCHAR2(4000 CHAR),
  EXECUTED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT AUTO_REMEDIATION_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE BLOG_POSTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TITLE                              VARCHAR2(500 CHAR) NOT NULL,
  SLUG                               VARCHAR2(300 CHAR) NOT NULL,
  EXCERPT                            CLOB NOT NULL,
  CONTENT                            CLOB NOT NULL,
  AUTHOR                             VARCHAR2(255 CHAR) DEFAULT 'TuneVault Team' NOT NULL,
  PUBLISHED_AT                       TIMESTAMP(6) WITH TIME ZONE NOT NULL,
  READ_TIME_MINUTES                  NUMBER(10) DEFAULT 5 NOT NULL,
  SEO_NOINDEX                        BOOLEAN DEFAULT FALSE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  COMING_SOON                        BOOLEAN DEFAULT FALSE,
  CONSTRAINT BLOG_POSTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT BLOG_POSTS_SLUG_KEY UNIQUE (SLUG)
);

CREATE TABLE CHECK_FAILURE_BUNDLES (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CHECK_ID                           VARCHAR2(4000 CHAR),
  CONNECTION_ID                      NUMBER(10),
  USER_ID                            NUMBER(10),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  SOURCE                             VARCHAR2(4000 CHAR) DEFAULT 'health_check' NOT NULL,
  SQL_TEXT                           CLOB,
  BIND_VALUES_REDACTED_JSON          JSON,
  ORA_ERROR_CODE                     VARCHAR2(4000 CHAR),
  ORA_ERROR_MESSAGE                  VARCHAR2(4000 CHAR),
  PYTHON_TRACEBACK                   CLOB,
  PROXY_LOG_TAIL                     CLOB,
  NODE_STACK                         VARCHAR2(4000 CHAR),
  CONNECTION_PROFILE_REDACTED_JSON   JSON,
  AGENT_VERSION                      VARCHAR2(4000 CHAR),
  ORACLE_VERSION                     VARCHAR2(4000 CHAR),
  CX_ORACLE_VERSION                  VARCHAR2(4000 CHAR),
  OS_RELEASE                         VARCHAR2(4000 CHAR),
  CONTEXT_JSON                       JSON,
  CONSTRAINT CHECK_FAILURE_BUNDLES_PKEY PRIMARY KEY (ID),
  CONSTRAINT CHECK_FAILURE_BUNDLES_SOURCE_CHECK CHECK (SOURCE IN ('health_check', 'validation_suite', 'ssh_exec', 'proxy'))
);

CREATE TABLE CHECK_RESULTS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  RUN_ID                             VARCHAR2(36 CHAR) NOT NULL,
  CHECK_ID                           VARCHAR2(4000 CHAR) NOT NULL,
  CHECK_CATEGORY                     VARCHAR2(4000 CHAR) NOT NULL,
  STATUS                             VARCHAR2(4000 CHAR) NOT NULL,
  METRIC_NAME                        VARCHAR2(4000 CHAR),
  METRIC_VALUE                       NUMBER,
  METRIC_UNIT                        VARCHAR2(4000 CHAR),
  RAW_PAYLOAD                        JSON DEFAULT '{}' NOT NULL,
  AI_SUMMARY                         CLOB,
  RECOMMENDATION                     CLOB,
  EXECUTED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT CHECK_RESULTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE CHECKS_CATALOGUE (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CHECK_ID                           VARCHAR2(4000 CHAR) NOT NULL,
  CATEGORY                           VARCHAR2(4000 CHAR) NOT NULL,
  NAME                               VARCHAR2(4000 CHAR) NOT NULL,
  DESCRIPTION                        VARCHAR2(4000 CHAR) NOT NULL,
  SQL_OR_COMMAND                     VARCHAR2(4000 CHAR) NOT NULL,
  SEVERITY_THRESHOLDS                JSON DEFAULT '{}' NOT NULL,
  REMEDIATION_HINT                   VARCHAR2(4000 CHAR),
  REQUIRES                           JSON DEFAULT '[]' NOT NULL,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'experimental' NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT CHECKS_CATALOGUE_PKEY PRIMARY KEY (ID),
  CONSTRAINT CHECKS_CATALOGUE_CHECK_ID_KEY UNIQUE (CHECK_ID),
  CONSTRAINT CHECKS_CATALOGUE_STATUS_CHECK CHECK (STATUS IN ('production', 'experimental', 'needs_validation'))
);

CREATE TABLE CLONE_HISTORY (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  RECIPE_ID                          NUMBER(10),
  COMPANY_ID                         VARCHAR2(255 CHAR),
  SOURCE_CONNECTION_ID               NUMBER(10),
  TARGET_CONNECTION_ID               NUMBER(10),
  STARTED_BY                         NUMBER(10),
  STARTED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  COMPLETED_AT                       TIMESTAMP(6),
  STATUS                             VARCHAR2(20 CHAR) DEFAULT 'running' NOT NULL,
  CURRENT_STEP                       NUMBER(10) DEFAULT 0,
  TOTAL_STEPS                        NUMBER(10) DEFAULT 0,
  STEP_RESULTS                       JSON DEFAULT '[]' NOT NULL,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  DURATION_MS                        NUMBER(10),
  CONSTRAINT CLONE_HISTORY_PKEY PRIMARY KEY (ID),
  CONSTRAINT CLONE_HISTORY_STATUS_CHECK CHECK (STATUS IN ('running', 'success', 'failed', 'aborted'))
);

CREATE TABLE CLONE_RECIPES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  COMPANY_ID                         VARCHAR2(255 CHAR),
  SOURCE_CONNECTION_ID               NUMBER(10),
  TARGET_CONNECTION_ID               NUMBER(10),
  RECIPE_NAME                        VARCHAR2(255 CHAR) NOT NULL,
  PRE_CHECKS_CONFIG                  JSON DEFAULT '{}' NOT NULL,
  CLONE_STEPS                        JSON DEFAULT '[]' NOT NULL,
  POST_STEPS                         JSON DEFAULT '[]' NOT NULL,
  CREATED_BY                         NUMBER(10),
  LAST_RUN_AT                        TIMESTAMP(6),
  LAST_RUN_STATUS                    VARCHAR2(20 CHAR),
  CREATED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT CLONE_RECIPES_PKEY PRIMARY KEY (ID),
  CONSTRAINT CLONE_RECIPES_LAST_RUN_STATUS_CHECK CHECK (LAST_RUN_STATUS IN ('success', 'failed', 'partial', 'running'))
);

CREATE TABLE COMPANY_HC_USAGE (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  COMPANY_DOMAIN                     VARCHAR2(255 CHAR) NOT NULL,
  HC_COUNT                           NUMBER(10) DEFAULT 0 NOT NULL,
  FIRST_RUN_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  LAST_RUN_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT COMPANY_HC_USAGE_PKEY PRIMARY KEY (ID),
  CONSTRAINT COMPANY_HC_USAGE_COMPANY_DOMAIN_KEY UNIQUE (COMPANY_DOMAIN)
);

CREATE TABLE COMPLIANCE_REPORTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  REPORT_TYPE                        VARCHAR2(4000 CHAR) NOT NULL,
  TITLE                              VARCHAR2(4000 CHAR) NOT NULL,
  DATE_FROM                          DATE NOT NULL,
  DATE_TO                            DATE NOT NULL,
  GENERATED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  GENERATED_BY                       VARCHAR2(4000 CHAR) NOT NULL,
  GENERATED_DATA                     JSON DEFAULT '{}' NOT NULL,
  ROW_COUNTS                         JSON DEFAULT '{}' NOT NULL,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'completed' NOT NULL,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  COMPANY_DOMAIN                     VARCHAR2(255 CHAR) NOT NULL,
  CONSTRAINT COMPLIANCE_REPORTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT COMPLIANCE_REPORTS_REPORT_TYPE_CHECK CHECK (REPORT_TYPE IN ('sox_change', 'access_audit', 'activity_summary', 'security_posture', 'resilience')),
  CONSTRAINT COMPLIANCE_REPORTS_STATUS_CHECK CHECK (STATUS IN ('completed', 'failed'))
);

CREATE TABLE CONNECTION_HEALTH_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  RAN_AT                             TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  PROBE_1_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_1_MS                         NUMBER(10),
  PROBE_1_DETAIL                     VARCHAR2(4000 CHAR),
  PROBE_2_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_2_MS                         NUMBER(10),
  PROBE_2_DETAIL                     VARCHAR2(4000 CHAR),
  PROBE_3_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_3_MS                         NUMBER(10),
  PROBE_3_DETAIL                     VARCHAR2(4000 CHAR),
  PROBE_4_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_4_MS                         NUMBER(10),
  PROBE_4_DETAIL                     VARCHAR2(4000 CHAR),
  PROBE_5_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_5_MS                         NUMBER(10),
  PROBE_5_DETAIL                     VARCHAR2(4000 CHAR),
  PROBE_6_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_6_MS                         NUMBER(10),
  PROBE_6_DETAIL                     VARCHAR2(4000 CHAR),
  PASSED                             NUMBER(10) DEFAULT 0 NOT NULL,
  TOTAL                              NUMBER(10) DEFAULT 7 NOT NULL,
  AGENT_VERSION                      VARCHAR2(4000 CHAR),
  AGENT_UPTIME_S                     NUMBER(10),
  RUN_TRIGGER                        VARCHAR2(4000 CHAR) DEFAULT 'manual' NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  PROBE_7_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_7_MS                         NUMBER(10),
  PROBE_7_DETAIL                     VARCHAR2(4000 CHAR),
  PROBE_8_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_8_MS                         NUMBER(10),
  PROBE_8_DETAIL                     VARCHAR2(4000 CHAR),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PKEY PRIMARY KEY (ID),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_1_STATUS_CHECK CHECK (PROBE_1_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_2_STATUS_CHECK CHECK (PROBE_2_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_3_STATUS_CHECK CHECK (PROBE_3_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_4_STATUS_CHECK CHECK (PROBE_4_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_5_STATUS_CHECK CHECK (PROBE_5_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_6_STATUS_CHECK CHECK (PROBE_6_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_7_STATUS_CHECK CHECK (PROBE_7_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_PROBE_8_STATUS_CHECK CHECK (PROBE_8_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT CONNECTION_HEALTH_RUNS_TRIGGER_CHECK CHECK (RUN_TRIGGER IN ('sweeper', 'manual'))
);

CREATE TABLE CONNECTION_OPTION_CONSENT (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  OPTION_KEY                         VARCHAR2(4000 CHAR) NOT NULL,
  STATE                              VARCHAR2(4000 CHAR) DEFAULT 'unset' NOT NULL,
  ACKNOWLEDGED_BY                    NUMBER(10),
  ACKNOWLEDGED_AT                    TIMESTAMP(6) WITH TIME ZONE,
  SOURCE                             VARCHAR2(4000 CHAR),
  DETECTED_EVIDENCE                  JSON,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT CONNECTION_OPTION_CONSENT_PKEY PRIMARY KEY (ID),
  CONSTRAINT CONNECTION_OPTION_CONSENT_UNIQ UNIQUE (CONNECTION_ID, OPTION_KEY),
  CONSTRAINT CONNECTION_OPTION_CONSENT_OPTION_CHECK CHECK (OPTION_KEY IN ('diagnostics_pack', 'tuning_pack', 'advanced_compression', 'advanced_security', 'partitioning', 'in_memory', 'rat')),
  CONSTRAINT CONNECTION_OPTION_CONSENT_STATE_CHECK CHECK (STATE IN ('unset', 'granted', 'denied'))
);

CREATE TABLE CONNECTION_SCHEDULES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  CADENCE_MINUTES                    NUMBER(10) DEFAULT 240 NOT NULL,
  ENABLED                            BOOLEAN DEFAULT FALSE NOT NULL,
  NEXT_RUN_AT                        TIMESTAMP(6) WITH TIME ZONE,
  LAST_RUN_AT                        TIMESTAMP(6) WITH TIME ZONE,
  ALERT_EMAIL                        VARCHAR2(4000 CHAR),
  SEVERITY_THRESHOLD                 VARCHAR2(4000 CHAR) DEFAULT 'amber' NOT NULL,
  LAST_ALERT_SENT_AT                 TIMESTAMP(6) WITH TIME ZONE,
  LAST_ALERT_SUBJECT                 VARCHAR2(4000 CHAR),
  SNOOZED_UNTIL                      TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  AUTO_CREATE_TICKETS                BOOLEAN DEFAULT TRUE,
  AUTO_REMEDIATE                     BOOLEAN DEFAULT FALSE,
  REMEDIATE_THRESHOLD                NUMBER(10) DEFAULT 3,
  REMEDIATE_SERVICES                 JSON,
  REMEDIATE_COOLDOWN_MINUTES         NUMBER DEFAULT 30 NOT NULL,
  CONSTRAINT CONNECTION_SCHEDULES_PKEY PRIMARY KEY (ID),
  CONSTRAINT CONNECTION_SCHEDULES_CONNECTION_ID_KEY UNIQUE (CONNECTION_ID)
);

CREATE TABLE CONNECTION_SSH_PROFILES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  ROLE                               VARCHAR2(32 CHAR) DEFAULT 'db_host' NOT NULL,
  SSH_HOST                           VARCHAR2(253 CHAR) NOT NULL,
  SSH_PORT                           NUMBER(10) DEFAULT 22 NOT NULL,
  SSH_USER                           VARCHAR2(128 CHAR) DEFAULT 'oracle' NOT NULL,
  AUTH_METHOD                        VARCHAR2(32 CHAR) DEFAULT 'agent_forward' NOT NULL,
  SSH_KEY_ENCRYPTED                  VARCHAR2(4000 CHAR),
  SSH_KEY_FINGERPRINT                VARCHAR2(128 CHAR),
  BASTION_HOST                       VARCHAR2(253 CHAR),
  BASTION_PORT                       NUMBER(10) DEFAULT 22,
  BASTION_USER                       VARCHAR2(128 CHAR),
  BASTION_KEY_ENCRYPTED              VARCHAR2(4000 CHAR),
  KNOWN_HOSTS_PIN                    VARCHAR2(256 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  LAST_TESTED_AT                     TIMESTAMP(6) WITH TIME ZONE,
  LAST_TEST_STATUS                   VARCHAR2(16 CHAR),
  CONSTRAINT CONNECTION_SSH_PROFILES_PKEY PRIMARY KEY (ID),
  CONSTRAINT CONNECTION_SSH_PROFILES_AUTH_METHOD_CHECK CHECK (AUTH_METHOD IN ('agent_forward', 'key_upload', 'password')),
  CONSTRAINT CONNECTION_SSH_PROFILES_LAST_TEST_STATUS_CHECK CHECK (LAST_TEST_STATUS IN ('pass', 'fail', 'pending')),
  CONSTRAINT CONNECTION_SSH_PROFILES_ROLE_CHECK CHECK (ROLE IN ('db_host', 'apps_tier', 'concurrent_tier', 'web_tier'))
);

CREATE TABLE CREDENTIAL_ACCESS_LOG (
  ID                                 VARCHAR2(36 CHAR) DEFAULT LOWER(REGEXP_REPLACE(RAWTOHEX(SYS_GUID()),'(.{8})(.{4})(.{4})(.{4})(.{12})','\1-\2-\3-\4-\5')) NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  CREDENTIAL_TYPE                    VARCHAR2(4000 CHAR) NOT NULL,
  ACTION                             VARCHAR2(4000 CHAR) NOT NULL,
  USER_ID                            NUMBER(10),
  ACCESSED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT CREDENTIAL_ACCESS_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE DEMO_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  RUN_DATE                           DATE DEFAULT SYSDATE NOT NULL,
  RUN_COUNT                          NUMBER(10) DEFAULT 1 NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT DEMO_RUNS_PKEY PRIMARY KEY (ID),
  CONSTRAINT DEMO_RUNS_USER_ID_RUN_DATE_KEY UNIQUE (USER_ID, RUN_DATE)
);

CREATE TABLE EBS_ADOP_STATE (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  PATCHING                           BOOLEAN DEFAULT FALSE NOT NULL,
  PHASE                              VARCHAR2(4000 CHAR),
  SESSION_ID                         VARCHAR2(4000 CHAR),
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE,
  SERVICES_IN_PATCH_MODE             JSON DEFAULT '[]' NOT NULL,
  SOURCE                             VARCHAR2(4000 CHAR) DEFAULT 'vactive_services_only' NOT NULL,
  CHECKED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT EBS_ADOP_STATE_PKEY PRIMARY KEY (ID),
  CONSTRAINT UQ_EBS_ADOP_STATE_CONNECTION UNIQUE (CONNECTION_ID)
);

CREATE TABLE EBS_CONTROL_COMMANDS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  SLUG                               VARCHAR2(4000 CHAR) NOT NULL,
  LABEL                              VARCHAR2(4000 CHAR) NOT NULL,
  CATEGORY                           VARCHAR2(4000 CHAR) NOT NULL,
  SHELL_TEMPLATE                     VARCHAR2(4000 CHAR) NOT NULL,
  REQUIRES_CONTEXT_FILE              BOOLEAN DEFAULT FALSE NOT NULL,
  REQUIRES_APPS_PASSWORD             BOOLEAN DEFAULT FALSE NOT NULL,
  RISK_LEVEL                         VARCHAR2(4000 CHAR) DEFAULT 'low' NOT NULL,
  DRY_RUN_ONLY                       BOOLEAN DEFAULT TRUE NOT NULL,
  DESCRIPTION                        VARCHAR2(4000 CHAR) DEFAULT '' NOT NULL,
  EXPECTED_EFFECT                    VARCHAR2(4000 CHAR) DEFAULT '' NOT NULL,
  ROLLBACK_STEPS                     VARCHAR2(4000 CHAR) DEFAULT '' NOT NULL,
  CONSTRAINT EBS_CONTROL_COMMANDS_PKEY PRIMARY KEY (ID),
  CONSTRAINT EBS_CONTROL_COMMANDS_SLUG_KEY UNIQUE (SLUG),
  CONSTRAINT EBS_CONTROL_COMMANDS_RISK_LEVEL_CHECK CHECK (RISK_LEVEL IN ('low', 'medium', 'high'))
);

CREATE TABLE EBS_CREDENTIALS (
  ID                                 VARCHAR2(36 CHAR) DEFAULT LOWER(REGEXP_REPLACE(RAWTOHEX(SYS_GUID()),'(.{8})(.{4})(.{4})(.{4})(.{12})','\1-\2-\3-\4-\5')) NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  CREDENTIAL_TYPE                    VARCHAR2(4000 CHAR) NOT NULL,
  USERNAME                           VARCHAR2(4000 CHAR) NOT NULL,
  ENCRYPTED_VALUE                    VARCHAR2(4000 CHAR) NOT NULL,
  IV                                 VARCHAR2(4000 CHAR) NOT NULL,
  AUTH_TAG                           VARCHAR2(4000 CHAR) NOT NULL,
  ROTATED_AT                         TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT EBS_CREDENTIALS_PKEY PRIMARY KEY (ID),
  CONSTRAINT EBS_CREDENTIALS_CONNECTION_ID_CREDENTIAL_TYPE_KEY UNIQUE (CONNECTION_ID, CREDENTIAL_TYPE),
  CONSTRAINT EBS_CREDENTIALS_CREDENTIAL_TYPE_CHECK CHECK (CREDENTIAL_TYPE IN ('apps', 'system', 'sys', 'weblogic_admin', 'sysadmin_user', 'xml_gateway'))
);

CREATE TABLE EBS_DEEP_REPORTS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  CONNECTION_NAME                    VARCHAR2(4000 CHAR) DEFAULT '' NOT NULL,
  FINDINGS_JSON                      JSON DEFAULT '{}' NOT NULL,
  AI_ANALYSIS                        CLOB,
  IS_DEMO                            BOOLEAN DEFAULT FALSE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT EBS_DEEP_REPORTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE EBS_JOBS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  OP_KEY                             VARCHAR2(4000 CHAR) NOT NULL,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'queued' NOT NULL,
  OK                                 BOOLEAN,
  STDOUT                             CLOB,
  EXIT_CODE                          NUMBER(10),
  DURATION_MS                        NUMBER(10),
  CREATED_BY                         NUMBER(10),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  LOG_TAIL                           CLOB,
  CONSTRAINT EBS_JOBS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE EBS_MODULE_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  RUN_AT                             TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  OVERALL_STATUS                     VARCHAR2(4000 CHAR) NOT NULL,
  MODULES_JSON                       JSON NOT NULL,
  IS_DEMO                            BOOLEAN DEFAULT FALSE,
  CONSTRAINT EBS_MODULE_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE EBS_SANITY_RUNS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  RUN_AT                             TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  OVERALL_STATUS                     VARCHAR2(4000 CHAR) DEFAULT 'unknown' NOT NULL,
  FINDINGS_JSON                      JSON DEFAULT '[]' NOT NULL,
  IS_DEMO                            BOOLEAN DEFAULT FALSE NOT NULL,
  CONSTRAINT EBS_SANITY_RUNS_PKEY PRIMARY KEY (ID),
  CONSTRAINT EBS_SANITY_RUNS_OVERALL_STATUS_CHECK CHECK (OVERALL_STATUS IN ('pass', 'warn', 'fail', 'unknown'))
);

CREATE TABLE EBS_VALIDATION_RUNS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  RUN_AT                             TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  SUMMARY                            JSON DEFAULT '{}' NOT NULL,
  TESTS                              JSON DEFAULT '[]' NOT NULL,
  CONSTRAINT EBS_VALIDATION_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE EMAIL_DRIP_STATE (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  SEQUENCE_STEP                      NUMBER(10) NOT NULL,
  SENT_AT                            TIMESTAMP(6) WITH TIME ZONE,
  SUPPRESSED_AT                      TIMESTAMP(6) WITH TIME ZONE,
  SUPPRESSED_REASON                  VARCHAR2(4000 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT EMAIL_DRIP_STATE_PKEY PRIMARY KEY (ID),
  CONSTRAINT EMAIL_DRIP_STATE_USER_ID_SEQUENCE_STEP_KEY UNIQUE (USER_ID, SEQUENCE_STEP),
  CONSTRAINT EMAIL_DRIP_STATE_SEQUENCE_STEP_CHECK CHECK (SEQUENCE_STEP IN (1, 2, 3))
);

CREATE TABLE EMAIL_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10),
  USER_EMAIL                         VARCHAR2(255 CHAR) NOT NULL,
  TEMPLATE                           VARCHAR2(100 CHAR) NOT NULL,
  HC_ID                              NUMBER(10),
  STATUS                             VARCHAR2(20 CHAR) DEFAULT 'sent' NOT NULL,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  POSTMARK_MESSAGE_ID                VARCHAR2(255 CHAR),
  SENT_AT                            TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT EMAIL_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE FINDING_HISTORY (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  SCHEDULE_ID                        NUMBER(10),
  CHECK_ID                           VARCHAR2(4000 CHAR) NOT NULL,
  FINDING_KEY                        VARCHAR2(4000 CHAR) NOT NULL,
  TITLE                              VARCHAR2(4000 CHAR),
  METRIC_LINE                        VARCHAR2(4000 CHAR),
  REMEDIATION                        VARCHAR2(4000 CHAR),
  SEVERITY                           VARCHAR2(4000 CHAR) NOT NULL,
  FIRST_SEEN_AT                      TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  LAST_SEEN_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  RESOLVED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  OCCURRENCE_COUNT                   NUMBER(10) DEFAULT 1 NOT NULL,
  CONSTRAINT FINDING_HISTORY_PKEY PRIMARY KEY (ID),
  CONSTRAINT FINDING_HISTORY_CONNECTION_ID_FINDING_KEY_KEY UNIQUE (CONNECTION_ID, FINDING_KEY)
);

CREATE TABLE FNDLOAD_HISTORY (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10),
  USER_EMAIL                         VARCHAR2(255 CHAR) NOT NULL,
  ACTION                             VARCHAR2(50 CHAR) NOT NULL,
  SOURCE_CONN_ID                     NUMBER(10),
  SOURCE_CONN_NAME                   VARCHAR2(255 CHAR),
  TARGET_CONN_ID                     NUMBER(10),
  TARGET_CONN_NAME                   VARCHAR2(255 CHAR),
  OBJECT_TYPE                        VARCHAR2(100 CHAR) NOT NULL,
  LCT_FILE                           VARCHAR2(100 CHAR) NOT NULL,
  OBJECT_NAMES                       JSON,
  DIFF_SUMMARY                       CLOB,
  UPLOAD_RESULT                      JSON,
  PRE_STATE_LDT                      VARCHAR2(4000 CHAR),
  SUCCESS                            BOOLEAN,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  EXECUTED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT FNDLOAD_HISTORY_PKEY PRIMARY KEY (ID)
);

CREATE TABLE HEALTH_CHECK_REQUESTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  NAME                               VARCHAR2(255 CHAR) NOT NULL,
  EMAIL                              VARCHAR2(255 CHAR) NOT NULL,
  COMPANY                            VARCHAR2(255 CHAR) NOT NULL,
  ORACLE_VERSION                     VARCHAR2(50 CHAR),
  ENV_TYPE                           VARCHAR2(100 CHAR),
  NUM_DATABASES                      NUMBER(10),
  PAIN_POINT                         VARCHAR2(4000 CHAR),
  STATUS                             VARCHAR2(50 CHAR) DEFAULT 'new',
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  SOURCE_URL                         VARCHAR2(4000 CHAR),
  CONSTRAINT HEALTH_CHECK_REQUESTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE HEALTH_CHECKS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_NAME                    VARCHAR2(255 CHAR) NOT NULL,
  HOST                               VARCHAR2(255 CHAR),
  PORT                               NUMBER(10) DEFAULT 1521,
  SERVICE_NAME                       VARCHAR2(255 CHAR),
  STATUS                             VARCHAR2(50 CHAR) DEFAULT 'pending',
  IS_DEMO                            BOOLEAN DEFAULT FALSE,
  METRICS                            JSON DEFAULT '{}',
  AI_ANALYSIS                        CLOB,
  OVERALL_SCORE                      NUMBER(10),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  COMPLETED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  USER_ID                            NUMBER(10),
  CONNECTION_ID                      NUMBER(10),
  USERNAME                           VARCHAR2(255 CHAR),
  SUMMARY_TEXT                       CLOB,
  TOP_ACTION                         VARCHAR2(4000 CHAR),
  ANALYSIS_PROGRESS_MS               NUMBER(10),
  ANALYSIS_STAGE                     VARCHAR2(32 CHAR),
  EBS_SUMMARY                        CLOB,
  EBS_ACTION                         VARCHAR2(4000 CHAR),
  AI_RECOMMENDATIONS                 JSON,
  -- Join key to CHECK_RESULTS.RUN_ID (same VARCHAR2(36) UUID, stamped in
  -- runAIAnalysis). Do NOT join CHECK_RESULTS.RUN_ID to HEALTH_CHECKS.ID —
  -- that is UUID-vs-NUMBER and raises ORA-01722 (bug #6).
  RUN_ID                             VARCHAR2(36 CHAR),
  CONSTRAINT HEALTH_CHECKS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE INSTALL_JOBS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  CONNECTION_ID                      NUMBER(10),
  HOST                               VARCHAR2(4000 CHAR) NOT NULL,
  SSH_PORT                           NUMBER(10) DEFAULT 22 NOT NULL,
  SSH_USER                           VARCHAR2(4000 CHAR) NOT NULL,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'queued' NOT NULL,
  EXIT_CODE                          NUMBER(10),
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  DURATION_MS                        NUMBER(10),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT INSTALL_JOBS_PKEY PRIMARY KEY (ID),
  CONSTRAINT INSTALL_JOBS_STATUS_CHECK CHECK (STATUS IN ('queued', 'connecting', 'preflight', 'installing', 'verifying', 'success', 'failed'))
);

CREATE TABLE INSTALLER_SMOKE_RUNS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  RUN_ID                             VARCHAR2(4000 CHAR) NOT NULL,
  OS                                 VARCHAR2(4000 CHAR) NOT NULL,
  TRIGGER_SOURCE                     VARCHAR2(4000 CHAR) DEFAULT 'manual' NOT NULL,
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  OVERALL                            VARCHAR2(4000 CHAR) DEFAULT 'pending' NOT NULL,
  STEP_INSTALL_MS                    NUMBER(10),
  STEP_INSTALL_OK                    BOOLEAN,
  STEP_INSTALL_ERR                   VARCHAR2(4000 CHAR),
  STEP_REGISTER_MS                   NUMBER(10),
  STEP_REGISTER_OK                   BOOLEAN,
  STEP_REGISTER_ERR                  VARCHAR2(4000 CHAR),
  STEP_HEARTBEAT_MS                  NUMBER(10),
  STEP_HEARTBEAT_OK                  BOOLEAN,
  STEP_HEARTBEAT_ERR                 VARCHAR2(4000 CHAR),
  STEP_SYSTEMD_MS                    NUMBER(10),
  STEP_SYSTEMD_OK                    BOOLEAN,
  STEP_SYSTEMD_ERR                   VARCHAR2(4000 CHAR),
  STEP_COMMAND_MS                    NUMBER(10),
  STEP_COMMAND_OK                    BOOLEAN,
  STEP_COMMAND_ERR                   VARCHAR2(4000 CHAR),
  FAILURE_LOG                        CLOB,
  RESULTS_JSON                       JSON,
  AGENT_VERSION                      VARCHAR2(4000 CHAR),
  INSTALL_SHA                        VARCHAR2(4000 CHAR),
  DURATION_TOTAL_MS                  NUMBER(10),
  CONSTRAINT INSTALLER_SMOKE_RUNS_PKEY PRIMARY KEY (ID),
  CONSTRAINT INSTALLER_SMOKE_RUNS_OVERALL_CHECK CHECK (OVERALL IN ('pending', 'pass', 'fail', 'error'))
);

CREATE TABLE INSTALLER_SMOKE_TOKENS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TOKEN                              VARCHAR2(4000 CHAR) NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  EXPIRES_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP + INTERVAL '15' MINUTE NOT NULL,
  USED_AT                            TIMESTAMP(6) WITH TIME ZONE,
  RUN_ID                             VARCHAR2(4000 CHAR),
  CREATED_BY                         VARCHAR2(4000 CHAR),
  CONSTRAINT INSTALLER_SMOKE_TOKENS_PKEY PRIMARY KEY (ID),
  CONSTRAINT INSTALLER_SMOKE_TOKENS_TOKEN_KEY UNIQUE (TOKEN)
);

CREATE TABLE INSTALLER_VALIDATION_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  RUN_ID                             VARCHAR2(4000 CHAR) NOT NULL,
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  OS                                 VARCHAR2(4000 CHAR) NOT NULL,
  INSTALL_SHA                        VARCHAR2(4000 CHAR),
  AGENT_VERSION                      VARCHAR2(4000 CHAR),
  KERNEL_VERSION                     VARCHAR2(4000 CHAR),
  PROBE_1_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_1_MS                         NUMBER(10),
  PROBE_1_ERROR                      VARCHAR2(4000 CHAR),
  PROBE_2_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_2_MS                         NUMBER(10),
  PROBE_2_ERROR                      VARCHAR2(4000 CHAR),
  PROBE_3_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_3_MS                         NUMBER(10),
  PROBE_3_ERROR                      VARCHAR2(4000 CHAR),
  PROBE_4_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_4_MS                         NUMBER(10),
  PROBE_4_ERROR                      VARCHAR2(4000 CHAR),
  PROBE_5_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_5_MS                         NUMBER(10),
  PROBE_5_ERROR                      VARCHAR2(4000 CHAR),
  PROBE_6_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_6_MS                         NUMBER(10),
  PROBE_6_ERROR                      VARCHAR2(4000 CHAR),
  OVERALL                            VARCHAR2(4000 CHAR) DEFAULT 'pending' NOT NULL,
  DURATION_TOTAL_MS                  NUMBER(10),
  TRIGGER_SOURCE                     VARCHAR2(4000 CHAR) DEFAULT 'cron' NOT NULL,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  PROBE_7_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_7_MS                         NUMBER(10),
  PROBE_7_ERROR                      VARCHAR2(4000 CHAR),
  TOPOLOGY                           VARCHAR2(4000 CHAR),
  CHECKS_PASSED                      NUMBER(10),
  CHECKS_TOTAL                       NUMBER(10),
  SSH_RUNBOOK_EXECUTED               BOOLEAN DEFAULT FALSE NOT NULL,
  PROBE_8_STATUS                     VARCHAR2(4000 CHAR),
  PROBE_8_MS                         NUMBER(10),
  PROBE_8_ERROR                      VARCHAR2(4000 CHAR),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PKEY PRIMARY KEY (ID),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_OS_CHECK CHECK (OS IN ('ol7', 'ol8', 'ebs12210', 'post-upgrade')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_OVERALL_CHECK CHECK (OVERALL IN ('pending', 'pass', 'fail', 'error')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_1_STATUS_CHECK CHECK (PROBE_1_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_2_STATUS_CHECK CHECK (PROBE_2_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_3_STATUS_CHECK CHECK (PROBE_3_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_4_STATUS_CHECK CHECK (PROBE_4_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_5_STATUS_CHECK CHECK (PROBE_5_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_6_STATUS_CHECK CHECK (PROBE_6_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_7_STATUS_CHECK CHECK (PROBE_7_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_PROBE_8_STATUS_CHECK CHECK (PROBE_8_STATUS IN ('pass', 'fail', 'skip')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_TOPOLOGY_CHECK CHECK (TOPOLOGY IN ('greenfield-OL7', 'greenfield-OL8', 'live-EBS-12.2.10-db-dev', 'post-upgrade-v3-to-v6')),
  CONSTRAINT INSTALLER_VALIDATION_RUNS_TRIGGER_SOURCE_CHECK CHECK (TRIGGER_SOURCE IN ('cron', 'deploy_webhook', 'manual', 'post-upgrade-agent'))
);

CREATE TABLE JOB_LOG_LINES (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  JOB_ID                             NUMBER(19) NOT NULL,
  SEQ                                NUMBER(10) NOT NULL,
  STREAM                             VARCHAR2(4000 CHAR) DEFAULT 'stdout' NOT NULL,
  LINE                               VARCHAR2(4000 CHAR) NOT NULL,
  TS                                 TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT JOB_LOG_LINES_PKEY PRIMARY KEY (ID),
  CONSTRAINT JOB_LOG_LINES_STREAM_CHECK CHECK (STREAM IN ('stdout', 'stderr', 'system'))
);

CREATE TABLE LICENSE_CACHE (
  ID                                 NUMBER(10) DEFAULT 1 NOT NULL,
  LICENSE_KEY                        VARCHAR2(4000 CHAR) NOT NULL,
  TIER                               VARCHAR2(4000 CHAR),
  MAX_CONNECTIONS                    NUMBER(10),
  MAX_HEALTH_CHECKS_PER_MONTH        NUMBER(10),
  EXPIRES_AT                         TIMESTAMP(6) WITH TIME ZONE,
  VALIDATED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT LICENSE_CACHE_PKEY PRIMARY KEY (ID),
  CONSTRAINT LICENSE_CACHE_SINGLE_ROW CHECK (ID = 1)
);

CREATE TABLE LISTENER_PREFLIGHT_RUNS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10),
  OVERALL_STATUS                     VARCHAR2(16 CHAR) DEFAULT 'running' NOT NULL,
  STEPS                              JSON,
  STEPS_PASSED                       NUMBER(5),
  STEPS_FAILED                       NUMBER(5),
  STEPS_SKIPPED                      NUMBER(5),
  TOTAL_DURATION_MS                  NUMBER(10),
  ORACLE_HOME                        VARCHAR2(4000 CHAR),
  ORACLE_SID                         VARCHAR2(4000 CHAR),
  SSH_HOST                           VARCHAR2(4000 CHAR),
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  TRIGGERED_BY                       VARCHAR2(32 CHAR) DEFAULT 'manual',
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  CONSTRAINT LISTENER_PREFLIGHT_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE MAGIC_LINK_TOKENS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  EMAIL                              VARCHAR2(255 CHAR) NOT NULL,
  TOKEN                              VARCHAR2(128 CHAR) NOT NULL,
  EXPIRES_AT                         TIMESTAMP(6) WITH TIME ZONE NOT NULL,
  USED_AT                            TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT MAGIC_LINK_TOKENS_PKEY PRIMARY KEY (ID),
  CONSTRAINT MAGIC_LINK_TOKENS_TOKEN_KEY UNIQUE (TOKEN)
);

CREATE TABLE MFA_ATTEMPTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  ATTEMPTED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  SUCCEEDED                          BOOLEAN DEFAULT FALSE NOT NULL,
  METHOD                             VARCHAR2(4000 CHAR) DEFAULT 'totp' NOT NULL,
  IP                                 VARCHAR2(4000 CHAR),
  CONSTRAINT MFA_ATTEMPTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE OPTION_CONSENT_LOG (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  OPTION_KEY                         VARCHAR2(4000 CHAR) NOT NULL,
  OLD_STATE                          VARCHAR2(4000 CHAR),
  NEW_STATE                          VARCHAR2(4000 CHAR) NOT NULL,
  ACTOR                              NUMBER(10),
  SOURCE                             VARCHAR2(4000 CHAR),
  DETECTED_EVIDENCE                  JSON,
  IP                                 VARCHAR2(4000 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT OPTION_CONSENT_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE ORACLE_CONNECTIONS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  NAME                               VARCHAR2(255 CHAR) NOT NULL,
  HOST                               VARCHAR2(255 CHAR),
  PORT                               NUMBER(10) DEFAULT 1521,
  SERVICE_NAME                       VARCHAR2(255 CHAR),
  USERNAME                           VARCHAR2(255 CHAR),
  ENCRYPTED_PASSWORD                 VARCHAR2(4000 CHAR),
  LAST_TESTED_AT                     TIMESTAMP(6) WITH TIME ZONE,
  LAST_TEST_SUCCESS                  BOOLEAN,
  LAST_TEST_MESSAGE                  VARCHAR2(4000 CHAR),
  ORACLE_VERSION                     VARCHAR2(255 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  USER_ID                            NUMBER(10),
  CONNECTION_TYPE                    VARCHAR2(20 CHAR) DEFAULT 'direct',
  PROXY_URL                          VARCHAR2(4000 CHAR),
  PROXY_API_KEY_ENC                  VARCHAR2(4000 CHAR),
  PROXY_VERSION                      VARCHAR2(20 CHAR),
  IS_EBS                             BOOLEAN,
  EBS_OPT_IN                         BOOLEAN DEFAULT FALSE,
  SCHEDULE_ENABLED                   BOOLEAN DEFAULT FALSE NOT NULL,
  SCHEDULE_CRON                      VARCHAR2(4000 CHAR) DEFAULT '0 */6 * * *' NOT NULL,
  LAST_SCHEDULED_RUN_AT              TIMESTAMP(6) WITH TIME ZONE,
  NEXT_SCHEDULED_RUN_AT              TIMESTAMP(6) WITH TIME ZONE,
  EBS_CHECKS_ENABLED                 BOOLEAN DEFAULT TRUE,
  GI_OS_USER                         VARCHAR2(100 CHAR),
  GI_ORACLE_HOME                     VARCHAR2(500 CHAR),
  ASM_SID                            VARCHAR2(50 CHAR),
  EBS_LOGIN_URL                      VARCHAR2(4000 CHAR),
  WEBLOGIC_CONSOLE_URL               VARCHAR2(4000 CHAR),
  PROXY_KEY_LAST_USED_AT             TIMESTAMP(6) WITH TIME ZONE,
  PROXY_KEY_LAST_IP                  VARCHAR2(4000 CHAR),
  PROXY_KEY_IPS                      JSON DEFAULT '[]',
  PROXY_KEY_CREATED_AT               TIMESTAMP(6) WITH TIME ZONE,
  PRIVILEGE_MODEL                    VARCHAR2(16 CHAR) DEFAULT 'sysdba' NOT NULL,
  LAST_DIAGNOSTICS_AT                TIMESTAMP(6) WITH TIME ZONE,
  INSTALLED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  LAST_UPGRADE_AT                    TIMESTAMP(6) WITH TIME ZONE,
  PYTHON_VERSION                     VARCHAR2(30 CHAR),
  CX_ORACLE_VERSION                  VARCHAR2(30 CHAR),
  OS_ID                              VARCHAR2(40 CHAR),
  KERNEL_VERSION                     VARCHAR2(80 CHAR),
  PROXY_API_KEY_ENC_PREVIOUS         VARCHAR2(4000 CHAR),
  KEY_ROTATED_AT                     TIMESTAMP(6) WITH TIME ZONE,
  KEY_ROTATION_STATUS                VARCHAR2(4000 CHAR) DEFAULT 'idle' NOT NULL,
  KEY_ROTATION_ACTOR                 VARCHAR2(4000 CHAR),
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'active' NOT NULL,
  INSTALL_TOKEN_HASH                 VARCHAR2(4000 CHAR),
  AUTO_UPGRADE_ENABLED               BOOLEAN DEFAULT TRUE NOT NULL,
  CONNECTIVITY_MODE                  VARCHAR2(20 CHAR) DEFAULT 'tns' NOT NULL,
  CONSTRAINT ORACLE_CONNECTIONS_CONNECTIVITY_MODE_ENUM CHECK (CONNECTIVITY_MODE IN ('tns', 'ssh_sqlplus', 'both')),
  SSH_DB_HOST                        VARCHAR2(4000 CHAR),
  SSH_DB_USER                        VARCHAR2(4000 CHAR),
  SSH_DB_KEY_ENC                     VARCHAR2(4000 CHAR),
  SSH_ORACLE_HOME                    VARCHAR2(4000 CHAR) DEFAULT '/u01/app/oracle/product/19.0.0/db_1',
  SSH_ORACLE_SID                     VARCHAR2(4000 CHAR),
  FIRST_RUN_COMPLETED_AT             TIMESTAMP(6) WITH TIME ZONE,
  SERVICE_CLASSIFICATION             JSON,
  INSTALL_TOKEN_ISSUED_AT            TIMESTAMP(6) WITH TIME ZONE,
  FIRST_HEARTBEAT_AT                 TIMESTAMP(6) WITH TIME ZONE,
  INSTALL_ID                         VARCHAR2(64 CHAR),
  AGENT_IN_RESTART_LOOP              BOOLEAN DEFAULT FALSE NOT NULL,
  AGENT_RESTART_LOOP_REASON          VARCHAR2(4000 CHAR),
  AGENT_RESTART_LOOP_AT              TIMESTAMP(6) WITH TIME ZONE,
  EBS_SERVICE                        VARCHAR2(128 CHAR),
  SERVER_TYPE                        VARCHAR2(32 CHAR),
  APPS_PWD_ENC                       VARCHAR2(4000 CHAR),
  WEBLOGIC_PWD_ENC                   VARCHAR2(4000 CHAR),
  EBS_INSTANCE_NAME                  VARCHAR2(64 CHAR),
  EBS_CONTEXT_FILE                   VARCHAR2(4000 CHAR),
  EBS_SYSTEM_PWD_ENC                 VARCHAR2(4000 CHAR),
  CLIENT_NAME                        VARCHAR2(4000 CHAR),
  CLIENT_SLUG                        VARCHAR2(4000 CHAR),
  DISCOVERED_SERVICES                JSON,
  CONSTRAINT ORACLE_CONNECTIONS_PKEY PRIMARY KEY (ID),
  CONSTRAINT ORACLE_CONNECTIONS_KEY_ROTATION_STATUS_CHECK CHECK (KEY_ROTATION_STATUS IN ('idle', 'pending', 'acknowledged')),
  CONSTRAINT ORACLE_CONNECTIONS_PRIVILEGE_MODEL_CHECK CHECK (PRIVILEGE_MODEL IN ('reader', 'sysdba')),
  CONSTRAINT ORACLE_CONNECTIONS_STATUS_CHECK CHECK (STATUS IN ('pending_registration', 'active', 'stale', 'upgrade-degraded'))
);

CREATE TABLE ORACLE_DOC_CHUNKS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  DOC_NAME                           VARCHAR2(4000 CHAR) NOT NULL,
  CHUNK_INDEX                        NUMBER(10) NOT NULL,
  CONTENT                            CLOB NOT NULL,
  TOKEN_COUNT                        NUMBER(10),
  EMBEDDING                          VECTOR(1536, FLOAT32),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT ORACLE_DOC_CHUNKS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE OUTREACH_ATTEMPTS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  ATTEMPTED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  ATTEMPTED_TO                       VARCHAR2(4000 CHAR),
  ATTEMPTED_SUBJECT                  VARCHAR2(4000 CHAR),
  ATTEMPTED_BY                       VARCHAR2(4000 CHAR) NOT NULL,
  BLOCKED                            BOOLEAN NOT NULL,
  UNLOCK_TOKEN_PRESENT               BOOLEAN DEFAULT FALSE NOT NULL,
  BLOCKED_REASON                     VARCHAR2(4000 CHAR),
  METADATA                           JSON,
  CONSTRAINT OUTREACH_ATTEMPTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE OUTREACH_BATCHES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  NAME                               VARCHAR2(255 CHAR) NOT NULL,
  TEMPLATE_SUBJECT                   VARCHAR2(4000 CHAR) NOT NULL,
  TEMPLATE_BODY                      CLOB NOT NULL,
  SEND_WINDOW_START                  TIMESTAMP(6) WITH TIME ZONE,
  SEND_WINDOW_END                    TIMESTAMP(6) WITH TIME ZONE,
  APPROVAL_STATUS                    VARCHAR2(32 CHAR) DEFAULT 'PENDING' NOT NULL,
  APPROVED_BY                        NUMBER(10),
  APPROVED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  NOTES                              VARCHAR2(4000 CHAR),
  CONSTRAINT OUTREACH_BATCHES_PKEY PRIMARY KEY (ID),
  CONSTRAINT OUTREACH_BATCHES_NAME_KEY UNIQUE (NAME),
  CONSTRAINT OUTREACH_BATCHES_APPROVAL_STATUS_CHECK CHECK (APPROVAL_STATUS IN ('PENDING', 'APPROVED', 'REJECTED', 'SENT', 'CANCELLED'))
);

CREATE TABLE OUTREACH_RECIPIENTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  BATCH_ID                           NUMBER(10) NOT NULL,
  EMAIL                              VARCHAR2(255 CHAR) NOT NULL,
  NAME                               VARCHAR2(255 CHAR),
  COMPANY                            VARCHAR2(255 CHAR),
  SEND_AUTHORIZED                    BOOLEAN DEFAULT FALSE NOT NULL,
  SENT_AT                            TIMESTAMP(6) WITH TIME ZONE,
  STATUS                             VARCHAR2(32 CHAR) DEFAULT 'PENDING' NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT OUTREACH_RECIPIENTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT OUTREACH_RECIPIENTS_BATCH_ID_EMAIL_KEY UNIQUE (BATCH_ID, EMAIL),
  CONSTRAINT OUTREACH_RECIPIENTS_STATUS_CHECK CHECK (STATUS IN ('PENDING', 'SENT', 'BLOCKED', 'FAILED', 'UNSUBSCRIBED'))
);

CREATE TABLE OUTREACH_SEND_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  ATTEMPTED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  BATCH_ID                           NUMBER(10),
  RECIPIENT_ID                       NUMBER(10),
  RECIPIENT_EMAIL                    VARCHAR2(255 CHAR),
  GATE_RESULT                        VARCHAR2(16 CHAR) NOT NULL,
  BLOCKED_REASON                     VARCHAR2(64 CHAR),
  GATE_FAILED                        VARCHAR2(32 CHAR),
  POSTMARK_MESSAGE_ID                VARCHAR2(255 CHAR),
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  METADATA                           JSON,
  CONSTRAINT OUTREACH_SEND_LOG_PKEY PRIMARY KEY (ID),
  CONSTRAINT OUTREACH_SEND_LOG_GATE_RESULT_CHECK CHECK (GATE_RESULT IN ('ALLOWED', 'BLOCKED'))
);

CREATE TABLE PAGE_EVENTS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  EVENT_TYPE                         VARCHAR2(4000 CHAR) NOT NULL,
  EVENT_NAME                         VARCHAR2(4000 CHAR) NOT NULL,
  PAGE_URL                           VARCHAR2(4000 CHAR) NOT NULL,
  REFERRER                           VARCHAR2(4000 CHAR),
  USER_AGENT                         VARCHAR2(4000 CHAR),
  IP_HASH                            VARCHAR2(4000 CHAR),
  SESSION_ID                         VARCHAR2(36 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT PAGE_EVENTS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE PAYMENT_METHOD_TEST_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  RAN_AT                             TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  METHOD                             VARCHAR2(30 CHAR) NOT NULL,
  FLOW_TYPE                          VARCHAR2(30 CHAR),
  BANK                               VARCHAR2(10 CHAR),
  LABEL                              VARCHAR2(100 CHAR),
  ORDER_ID                           VARCHAR2(100 CHAR),
  PAYMENT_ID                         VARCHAR2(100 CHAR),
  AMOUNT_PAISE                       NUMBER(10) NOT NULL,
  STATUS                             VARCHAR2(20 CHAR) NOT NULL,
  ERROR_CODE                         VARCHAR2(100 CHAR),
  ERROR_DESCRIPTION                  VARCHAR2(4000 CHAR),
  LATENCY_MS                         NUMBER(10),
  RAW_RESPONSE                       JSON,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT PAYMENT_METHOD_TEST_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE PAYMENT_RECONCILIATION (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  OCCURRED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  EVENT_TYPE                         VARCHAR2(4000 CHAR) NOT NULL,
  RAZORPAY_PAYMENT_ID                VARCHAR2(4000 CHAR),
  RAZORPAY_ORDER_ID                  VARCHAR2(4000 CHAR),
  USER_ID                            NUMBER(10),
  USER_EMAIL                         VARCHAR2(4000 CHAR),
  PLAN_TIER                          VARCHAR2(4000 CHAR),
  FAILURE_STAGE                      VARCHAR2(4000 CHAR) NOT NULL,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  METADATA                           JSON DEFAULT '{}' NOT NULL,
  CONSTRAINT PAYMENT_RECONCILIATION_PKEY PRIMARY KEY (ID)
);

CREATE TABLE PAYMENT_TEST_RUNS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  RAN_AT                             TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  TEST_TYPE                          VARCHAR2(20 CHAR) NOT NULL,
  PLAN_TIER                          VARCHAR2(20 CHAR) NOT NULL,
  TEST_USER_ID                       NUMBER(10) NOT NULL,
  ORDER_ID                           VARCHAR2(100 CHAR),
  PAYMENT_ID                         VARCHAR2(100 CHAR),
  SUBSCRIPTION_ID                    VARCHAR2(100 CHAR),
  STAGE_ORDER_CREATED                VARCHAR2(10 CHAR),
  STAGE_SIG_VERIFIED                 VARCHAR2(10 CHAR),
  STAGE_CREDITS_UPDATED              VARCHAR2(10 CHAR),
  STAGE_WEBHOOK_RECEIVED             VARCHAR2(10 CHAR) DEFAULT 'SKIP',
  STAGE_RECEIPT_SENT                 VARCHAR2(10 CHAR) DEFAULT 'SKIP',
  CREDITS_BEFORE                     NUMBER(10),
  CREDITS_AFTER                      NUMBER(10),
  OVERALL_RESULT                     VARCHAR2(10 CHAR) NOT NULL,
  ERROR_DETAIL                       VARCHAR2(4000 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT PAYMENT_TEST_RUNS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE PAYMENTS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10),
  RAZORPAY_ORDER_ID                  VARCHAR2(4000 CHAR),
  RAZORPAY_PAYMENT_ID                VARCHAR2(4000 CHAR),
  RAZORPAY_SIGNATURE                 VARCHAR2(4000 CHAR),
  RAZORPAY_SUBSCRIPTION_ID           VARCHAR2(4000 CHAR),
  AMOUNT_PAISE                       NUMBER(10) NOT NULL,
  CURRENCY                           VARCHAR2(4000 CHAR) DEFAULT 'USD' NOT NULL,
  PLAN_TIER                          VARCHAR2(4000 CHAR) NOT NULL,
  PAYMENT_TYPE                       VARCHAR2(4000 CHAR) NOT NULL,
  BILLING_PERIOD                     VARCHAR2(4000 CHAR),
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'created' NOT NULL,
  METADATA                           JSON DEFAULT '{}' NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT PAYMENTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT PAYMENTS_RAZORPAY_ORDER_ID_KEY UNIQUE (RAZORPAY_ORDER_ID),
  CONSTRAINT PAYMENTS_BILLING_PERIOD_CHECK CHECK (BILLING_PERIOD IN ('monthly', 'annual')),
  CONSTRAINT PAYMENTS_PAYMENT_TYPE_CHECK CHECK (PAYMENT_TYPE IN ('one_time', 'subscription')),
  CONSTRAINT PAYMENTS_PLAN_TIER_CHECK CHECK (PLAN_TIER IN ('starter', 'growth', 'scale', 'custom', 'individual', 'team', 'professional', 'business', 'enterprise', 'managed_services')),
  CONSTRAINT PAYMENTS_STATUS_CHECK CHECK (STATUS IN ('created', 'captured', 'failed', 'refunded'))
);

CREATE TABLE PROXY_EXEC_AUDIT (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10),
  COMMAND_ID                         VARCHAR2(4000 CHAR) NOT NULL,
  ARGS                               JSON DEFAULT '{}' NOT NULL,
  EXIT_CODE                          NUMBER(10) DEFAULT '-1' NOT NULL,
  DURATION_MS                        NUMBER(10) DEFAULT 0 NOT NULL,
  STDOUT_BYTES                       NUMBER(10) DEFAULT 0 NOT NULL,
  STDERR_BYTES                       NUMBER(10) DEFAULT 0 NOT NULL,
  SUCCEEDED                          BOOLEAN DEFAULT FALSE NOT NULL,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  EXECUTED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT PROXY_EXEC_AUDIT_PKEY PRIMARY KEY (ID)
);

CREATE TABLE RBAC_AUDIT_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  METHOD                             VARCHAR2(10 CHAR) NOT NULL,
  PATH                               VARCHAR2(500 CHAR) NOT NULL,
  REQUIRED_ROLE                      VARCHAR2(20 CHAR) NOT NULL,
  ACTUAL_ROLE                        VARCHAR2(20 CHAR) NOT NULL,
  DENIED_AT                          TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT RBAC_AUDIT_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE ROADMAP_REMINDERS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TITLE                              VARCHAR2(4000 CHAR) NOT NULL,
  BODY_MD                            CLOB,
  TRIGGER_CONDITION_JSON             JSON,
  DOC_LINK                           VARCHAR2(4000 CHAR),
  MANUAL_FLAG                        BOOLEAN DEFAULT FALSE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  SURFACED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  DISMISSED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  CONSTRAINT ROADMAP_REMINDERS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE ROLES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TEAM_ID                            NUMBER(10) NOT NULL,
  NAME                               VARCHAR2(100 CHAR) NOT NULL,
  SLUG                               VARCHAR2(100 CHAR) NOT NULL,
  BRANCH                             VARCHAR2(20 CHAR) NOT NULL,
  PERMISSIONS                        JSON DEFAULT '{}' NOT NULL,
  APPROVAL_REQUIRED_FROM             NUMBER(10),
  IS_DEFAULT                         BOOLEAN DEFAULT FALSE NOT NULL,
  SORT_ORDER                         NUMBER(10) DEFAULT 0 NOT NULL,
  CREATED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT ROLES_PKEY PRIMARY KEY (ID),
  CONSTRAINT ROLES_TEAM_ID_SLUG_KEY UNIQUE (TEAM_ID, SLUG),
  CONSTRAINT ROLES_BRANCH_CHECK CHECK (BRANCH IN ('dba', 'functional', 'dev', 'management'))
);

CREATE TABLE SAMPLE_REPORT_LEADS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  EMAIL                              VARCHAR2(4000 CHAR) NOT NULL,
  CAPTURED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  IP                                 VARCHAR2(4000 CHAR),
  REFERRER                           VARCHAR2(4000 CHAR),
  CONSTRAINT SAMPLE_REPORT_LEADS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SELF_HOSTED_DOWNLOAD_LEADS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  EMAIL                              VARCHAR2(4000 CHAR) NOT NULL,
  DOWNLOADED_VERSION                 VARCHAR2(4000 CHAR),
  IP_HASH                            VARCHAR2(4000 CHAR),
  REFERRER                           VARCHAR2(4000 CHAR),
  UTM_SOURCE                         VARCHAR2(4000 CHAR),
  UTM_MEDIUM                         VARCHAR2(4000 CHAR),
  UTM_CAMPAIGN                       VARCHAR2(4000 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SELF_HOSTED_DOWNLOAD_LEADS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SELF_HOSTED_LICENSES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  LICENSE_KEY                        VARCHAR2(64 CHAR) NOT NULL,
  USER_ID                            NUMBER(10),
  PLAN_TIER                          VARCHAR2(32 CHAR) DEFAULT 'starter' NOT NULL,
  MAX_CONNECTIONS                    NUMBER(10) DEFAULT 5 NOT NULL,
  MAX_HEALTH_CHECKS_PER_MONTH        NUMBER(10),
  STATUS                             VARCHAR2(16 CHAR) DEFAULT 'active' NOT NULL,
  ISSUED_AT                          TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  EXPIRES_AT                         TIMESTAMP(6) WITH TIME ZONE,
  LAST_VALIDATED_AT                  TIMESTAMP(6) WITH TIME ZONE,
  INSTANCE_ID                        VARCHAR2(128 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT SELF_HOSTED_LICENSES_PKEY PRIMARY KEY (ID),
  CONSTRAINT SELF_HOSTED_LICENSES_LICENSE_KEY_KEY UNIQUE (LICENSE_KEY)
);

CREATE TABLE SERVICE_HEARTBEAT_STATE (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  OP_KEY                             VARCHAR2(4000 CHAR) NOT NULL,
  CONSECUTIVE_DOWN                   NUMBER(10) DEFAULT 0 NOT NULL,
  LAST_CHECKED_AT                    TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  LAST_STATUS                        VARCHAR2(4000 CHAR),
  CONSTRAINT SERVICE_HEARTBEAT_STATE_PKEY PRIMARY KEY (ID),
  CONSTRAINT SERVICE_HEARTBEAT_STATE_CONNECTION_ID_OP_KEY_KEY UNIQUE (CONNECTION_ID, OP_KEY)
);

CREATE TABLE SMOKE_TEST_RUNS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  OVERALL_STATUS                     VARCHAR2(4000 CHAR) DEFAULT 'running' NOT NULL,
  STEPS_JSONB                        JSON DEFAULT '[]' NOT NULL,
  TRIGGERED_BY_USER_ID               NUMBER(10),
  CONSTRAINT SMOKE_TEST_RUNS_PKEY PRIMARY KEY (ID),
  CONSTRAINT SMOKE_TEST_RUNS_OVERALL_STATUS_CHECK CHECK (OVERALL_STATUS IN ('running', 'pass', 'fail', 'error'))
);

CREATE TABLE SQL_AUDIT_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10),
  USER_EMAIL                         VARCHAR2(255 CHAR) NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  CONNECTION_NAME                    VARCHAR2(255 CHAR),
  SQL_TEXT                           CLOB NOT NULL,
  ALLOWED                            BOOLEAN NOT NULL,
  BLOCK_REASON                       VARCHAR2(255 CHAR),
  SUCCESS                            BOOLEAN,
  ROW_COUNT                          NUMBER(10),
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  DURATION_MS                        NUMBER(10),
  EXECUTED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SQL_AUDIT_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SQL_CONSOLE_HISTORY (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  SQL_TEXT                           CLOB NOT NULL,
  ELAPSED_MS                         NUMBER(10),
  ROWS_RETURNED                      NUMBER(10),
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  SUCCESS                            BOOLEAN DEFAULT TRUE,
  SOURCE_IP                          VARCHAR2(64 CHAR),
  USER_AGENT                         VARCHAR2(512 CHAR),
  SESSION_ID                         VARCHAR2(128 CHAR),
  EXECUTED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SQL_CONSOLE_HISTORY_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SQL_FIX_CACHE (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  SQL_ID                             VARCHAR2(30 CHAR) NOT NULL,
  PLAN_HASH_VALUE                    NUMBER(19) NOT NULL,
  FIX_TYPE                           VARCHAR2(30 CHAR) NOT NULL,
  FIX_SQL                            VARCHAR2(4000 CHAR) NOT NULL,
  RATIONALE                          VARCHAR2(4000 CHAR) NOT NULL,
  SQL_TEXT_PREFIX                    CLOB,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  EXPIRES_AT                         TIMESTAMP(6) WITH TIME ZONE NOT NULL,
  CONSTRAINT SQL_FIX_CACHE_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SQL_TUNING_FINDINGS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  SQL_ID                             VARCHAR2(13 CHAR) NOT NULL,
  PLAN_HASH                          NUMBER(19),
  RANK                               NUMBER(10),
  PARSING_SCHEMA_NAME                VARCHAR2(128 CHAR),
  EXECUTIONS                         NUMBER(19),
  ELAPSED_PER_EXEC_MS                NUMBER(12,2),
  CPU_PER_EXEC_MS                    NUMBER(12,2),
  BUFFER_GETS_PER_EXEC               NUMBER(19),
  DISK_READS_PER_EXEC                NUMBER(19),
  ROWS_PROCESSED_PER_EXEC            NUMBER(12,2),
  SQL_TEXT                           CLOB,
  METRICS_JSON                       JSON DEFAULT '{}' NOT NULL,
  PLAN_SUMMARY_JSON                  JSON DEFAULT '[]' NOT NULL,
  AI_RECOMMENDATION_TEXT             CLOB,
  RECOMMENDED_SQL_TEXT               CLOB,
  FIX_TYPE                           VARCHAR2(16 CHAR),
  DIAGNOSIS_TAG                      VARCHAR2(64 CHAR),
  IS_HEURISTIC                       BOOLEAN DEFAULT FALSE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SQL_TUNING_FINDINGS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SSH_AUDIT (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TARGET_ID                          NUMBER(10) NOT NULL,
  COMMAND_KEY                        VARCHAR2(128 CHAR) NOT NULL,
  RENDERED_COMMAND                   VARCHAR2(4000 CHAR) NOT NULL,
  EXIT_CODE                          NUMBER(10),
  STDOUT_BYTES                       NUMBER(10),
  STDERR_BYTES                       NUMBER(10),
  DURATION_MS                        NUMBER(10),
  WAS_REJECTED                       BOOLEAN DEFAULT FALSE NOT NULL,
  REJECTION_REASON                   VARCHAR2(128 CHAR),
  INITIATED_BY                       VARCHAR2(255 CHAR),
  TS                                 TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SSH_AUDIT_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SSH_INSTALL_CREDENTIALS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  SSH_HOST                           VARCHAR2(4000 CHAR) NOT NULL,
  SSH_PORT                           NUMBER(10) DEFAULT 22 NOT NULL,
  SSH_USER                           VARCHAR2(4000 CHAR) NOT NULL,
  AUTH_METHOD                        VARCHAR2(4000 CHAR) NOT NULL,
  ENCRYPTED_CREDENTIAL               VARCHAR2(4000 CHAR) NOT NULL,
  USE_SUDO                           BOOLEAN DEFAULT TRUE NOT NULL,
  INSTALL_STATUS                     VARCHAR2(4000 CHAR) DEFAULT 'pending' NOT NULL,
  INSTALL_LOG                        CLOB,
  ERROR_MESSAGE                      VARCHAR2(4000 CHAR),
  INSTALLED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SSH_INSTALL_CREDENTIALS_PKEY PRIMARY KEY (ID),
  CONSTRAINT SSH_INSTALL_CREDENTIALS_CONNECTION_ID_KEY UNIQUE (CONNECTION_ID),
  CONSTRAINT SSH_INSTALL_CREDENTIALS_AUTH_METHOD_CHECK CHECK (AUTH_METHOD IN ('password', 'key')),
  CONSTRAINT SSH_INSTALL_CREDENTIALS_INSTALL_STATUS_CHECK CHECK (INSTALL_STATUS IN ('pending', 'running', 'success', 'failed'))
);

CREATE TABLE SSH_TARGETS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  LABEL                              VARCHAR2(255 CHAR) NOT NULL,
  HOST                               VARCHAR2(255 CHAR) NOT NULL,
  PORT                               NUMBER(10) DEFAULT 22 NOT NULL,
  OS_USER                            VARCHAR2(128 CHAR) NOT NULL,
  AUTH_METHOD                        VARCHAR2(16 CHAR) DEFAULT 'key' NOT NULL,
  ENCRYPTED_PRIVATE_KEY              VARCHAR2(4000 CHAR),
  ENCRYPTED_PASSPHRASE               VARCHAR2(4000 CHAR),
  ROLE                               VARCHAR2(32 CHAR) DEFAULT 'db_tier' NOT NULL,
  CONNECTION_ID                      NUMBER(10),
  LAST_CONNECTED_AT                  TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  USER_ID                            NUMBER(10),
  RETIRED_AT                         TIMESTAMP(6) WITH TIME ZONE,
  ORPHANED                           BOOLEAN,
  RETIRE_NOTE                        VARCHAR2(4000 CHAR),
  CONSTRAINT SSH_TARGETS_PKEY PRIMARY KEY (ID),
  CONSTRAINT SSH_TARGETS_AUTH_METHOD_CHECK CHECK (AUTH_METHOD IN ('key', 'password')),
  CONSTRAINT SSH_TARGETS_ROLE_CHECK CHECK (ROLE IN ('apps_tier', 'db_tier', 'utility'))
);

CREATE TABLE SSO_CONFIGS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  COMPANY_DOMAIN                     VARCHAR2(4000 CHAR) NOT NULL,
  PROVIDER_TYPE                      VARCHAR2(4000 CHAR) DEFAULT 'custom' NOT NULL,
  SSO_URL                            VARCHAR2(4000 CHAR) NOT NULL,
  ENTITY_ID                          VARCHAR2(4000 CHAR) NOT NULL,
  CERTIFICATE                        VARCHAR2(4000 CHAR) NOT NULL,
  ATTRIBUTE_MAPPING                  JSON DEFAULT '{"name": "displayName", "email": "nameID"}' NOT NULL,
  GROUP_ROLE_MAPPING                 JSON DEFAULT '{}' NOT NULL,
  DEFAULT_ROLE                       VARCHAR2(4000 CHAR) DEFAULT 'junior_dba' NOT NULL,
  IS_ACTIVE                          BOOLEAN DEFAULT TRUE NOT NULL,
  REQUIRE_SSO                        BOOLEAN DEFAULT FALSE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SSO_CONFIGS_PKEY PRIMARY KEY (ID),
  CONSTRAINT SSO_CONFIGS_COMPANY_DOMAIN_KEY UNIQUE (COMPANY_DOMAIN)
);

CREATE TABLE SSO_LOGIN_LOG (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  COMPANY_DOMAIN                     VARCHAR2(4000 CHAR) NOT NULL,
  EMAIL                              VARCHAR2(4000 CHAR),
  SUCCEEDED                          BOOLEAN DEFAULT FALSE NOT NULL,
  FAILURE_REASON                     VARCHAR2(4000 CHAR),
  IP                                 VARCHAR2(4000 CHAR),
  USER_AGENT                         VARCHAR2(4000 CHAR),
  ATTEMPTED_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SSO_LOGIN_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE SUBSCRIPTIONS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10),
  RAZORPAY_SUBSCRIPTION_ID           VARCHAR2(4000 CHAR) NOT NULL,
  RAZORPAY_PLAN_ID                   VARCHAR2(4000 CHAR) NOT NULL,
  PLAN_TIER                          VARCHAR2(4000 CHAR) NOT NULL,
  BILLING_PERIOD                     VARCHAR2(4000 CHAR) DEFAULT 'monthly' NOT NULL,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'created' NOT NULL,
  CURRENT_PERIOD_START               TIMESTAMP(6) WITH TIME ZONE,
  CURRENT_PERIOD_END                 TIMESTAMP(6) WITH TIME ZONE,
  CANCELLED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT SUBSCRIPTIONS_PKEY PRIMARY KEY (ID),
  CONSTRAINT SUBSCRIPTIONS_RAZORPAY_SUBSCRIPTION_ID_KEY UNIQUE (RAZORPAY_SUBSCRIPTION_ID),
  CONSTRAINT SUBSCRIPTIONS_BILLING_PERIOD_CHECK CHECK (BILLING_PERIOD IN ('monthly', 'annual')),
  CONSTRAINT SUBSCRIPTIONS_PLAN_TIER_CHECK CHECK (PLAN_TIER IN ('starter', 'growth', 'scale', 'custom', 'individual', 'team', 'professional', 'business', 'enterprise', 'managed_services')),
  CONSTRAINT SUBSCRIPTIONS_STATUS_CHECK CHECK (STATUS IN ('created', 'authenticated', 'active', 'pending', 'halted', 'cancelled', 'completed', 'expired', 'paused'))
);

CREATE TABLE TEAM_INVITES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TEAM_ID                            NUMBER(10) NOT NULL,
  INVITED_BY                         NUMBER(10) NOT NULL,
  EMAIL                              VARCHAR2(255 CHAR) NOT NULL,
  ROLE                               VARCHAR2(20 CHAR) DEFAULT 'viewer' NOT NULL,
  TOKEN                              VARCHAR2(64 CHAR) NOT NULL,
  STATUS                             VARCHAR2(20 CHAR) DEFAULT 'pending' NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  EXPIRES_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP + INTERVAL '7' DAY NOT NULL,
  CONSTRAINT TEAM_INVITES_PKEY PRIMARY KEY (ID),
  CONSTRAINT TEAM_INVITES_TOKEN_KEY UNIQUE (TOKEN),
  CONSTRAINT TEAM_INVITES_ROLE_CHECK CHECK (ROLE IN ('admin', 'senior_dba', 'junior_dba', 'viewer')),
  CONSTRAINT TEAM_INVITES_STATUS_CHECK CHECK (STATUS IN ('pending', 'accepted', 'revoked', 'expired'))
);

CREATE TABLE TEAM_MEMBERS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TEAM_ID                            NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  ROLE                               VARCHAR2(20 CHAR) DEFAULT 'viewer' NOT NULL,
  JOINED_AT                          TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  ROLE_ID                            NUMBER(10),
  CONSTRAINT TEAM_MEMBERS_PKEY PRIMARY KEY (ID),
  CONSTRAINT TEAM_MEMBERS_TEAM_ID_USER_ID_KEY UNIQUE (TEAM_ID, USER_ID),
  CONSTRAINT TEAM_MEMBERS_ROLE_CHECK CHECK (ROLE IN ('admin', 'senior_dba', 'junior_dba', 'viewer'))
);

CREATE TABLE TEAMS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  NAME                               VARCHAR2(100 CHAR) NOT NULL,
  OWNER_ID                           NUMBER(10) NOT NULL,
  PLAN_TIER                          VARCHAR2(20 CHAR) DEFAULT 'team' NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  MFA_REQUIRED                       BOOLEAN DEFAULT FALSE NOT NULL,
  CONSTRAINT TEAMS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE TELEMETRY_PINGS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  VERSION                            VARCHAR2(4000 CHAR),
  NODE_VERSION                       VARCHAR2(4000 CHAR),
  PLATFORM                           VARCHAR2(4000 CHAR),
  RECEIVED_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT TELEMETRY_PINGS_PKEY PRIMARY KEY (ID)
);

CREATE TABLE TNS_TOPOLOGY_SNAPSHOTS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  SNAPSHOT_DATA                      JSON NOT NULL,
  SERVICE_NAMES                      JSON,
  PATCH_SERVICES                     JSON,
  RECOMMENDED_SVC                    VARCHAR2(4000 CHAR),
  PDB_COUNT                          NUMBER(10),
  INSTANCE_NAME                      VARCHAR2(4000 CHAR),
  DB_VERSION                         VARCHAR2(4000 CHAR),
  SHARE_TOKEN                        VARCHAR2(64 CHAR),
  SHARE_EXPIRES_AT                   TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT TNS_TOPOLOGY_SNAPSHOTS_PKEY PRIMARY KEY (ID),
  CONSTRAINT TNS_TOPOLOGY_SNAPSHOTS_SHARE_TOKEN_KEY UNIQUE (SHARE_TOKEN)
);

CREATE TABLE TUNEOPS_NOTIFICATION_LOG (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  TICKET_NUMBER                      VARCHAR2(16 CHAR) NOT NULL,
  EVENT_TYPE                         VARCHAR2(32 CHAR) NOT NULL,
  RECIPIENT_EMAIL                    VARCHAR2(255 CHAR) NOT NULL,
  SENT_AT                            TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  METADATA                           JSON,
  CONSTRAINT TUNEOPS_NOTIFICATION_LOG_PKEY PRIMARY KEY (ID)
);

CREATE TABLE TUNEOPS_NOTIFICATION_MUTES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  MUTED_UNTIL                        TIMESTAMP(6) WITH TIME ZONE NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT TUNEOPS_NOTIFICATION_MUTES_PKEY PRIMARY KEY (ID)
);

CREATE TABLE TUNEOPS_NOTIFICATION_PREFS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  NOTIFICATIONS_ENABLED              BOOLEAN DEFAULT TRUE NOT NULL,
  SEVERITY_THRESHOLD                 VARCHAR2(16 CHAR) DEFAULT 'warning' NOT NULL,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT TUNEOPS_NOTIFICATION_PREFS_PKEY PRIMARY KEY (ID),
  CONSTRAINT TUNEOPS_NOTIFICATION_PREFS_USER_ID_KEY UNIQUE (USER_ID)
);

CREATE TABLE TUNEOPS_TICKETS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  COMPANY_ID                         VARCHAR2(255 CHAR),
  CONNECTION_ID                      NUMBER(10),
  TICKET_NUMBER                      VARCHAR2(20 CHAR) NOT NULL,
  CANONICAL_KEY                      VARCHAR2(500 CHAR),
  TITLE                              VARCHAR2(500 CHAR) NOT NULL,
  DESCRIPTION                        VARCHAR2(4000 CHAR),
  SEVERITY                           VARCHAR2(20 CHAR) DEFAULT 'info' NOT NULL,
  STATUS                             VARCHAR2(20 CHAR) DEFAULT 'OPEN' NOT NULL,
  SOURCE                             VARCHAR2(20 CHAR) DEFAULT 'health_check' NOT NULL,
  SOURCE_REFERENCE                   JSON DEFAULT '{}',
  ASSIGNED_TO                        NUMBER(10),
  RECOMMENDED_FIX                    VARCHAR2(4000 CHAR),
  FIX_TYPE                           VARCHAR2(10 CHAR),
  REQUIRES_APPROVAL                  BOOLEAN DEFAULT FALSE NOT NULL,
  APPROVED_BY                        NUMBER(10),
  APPROVED_AT                        TIMESTAMP(6),
  EXECUTED_BY                        NUMBER(10),
  EXECUTED_AT                        TIMESTAMP(6),
  EXECUTION_RESULT                   JSON,
  RESOLVED_AT                        TIMESTAMP(6),
  RESOLVED_BY                        NUMBER(10),
  RESOLUTION_NOTES                   VARCHAR2(4000 CHAR),
  REOPENED_COUNT                     NUMBER(10) DEFAULT 0 NOT NULL,
  CREATED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) DEFAULT SYSTIMESTAMP NOT NULL,
  ACKNOWLEDGED_AT                    TIMESTAMP(6) WITH TIME ZONE,
  OCCURRENCE_COUNT                   NUMBER(10) DEFAULT 1,
  LAST_SEEN_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT TUNEOPS_TICKETS_PKEY PRIMARY KEY (ID),
  CONSTRAINT TUNEOPS_TICKETS_FIX_TYPE_CHECK CHECK (FIX_TYPE IN ('sql', 'ssh', 'manual')),
  CONSTRAINT TUNEOPS_TICKETS_SEVERITY_CHECK CHECK (SEVERITY IN ('critical', 'warning', 'info')),
  CONSTRAINT TUNEOPS_TICKETS_SOURCE_CHECK CHECK (SOURCE IN ('health_check', 'db_ops', 'control_tab', 'ssh_command', 'sql_command', 'manual')),
  CONSTRAINT TUNEOPS_TICKETS_STATUS_CHECK CHECK (STATUS IN ('OPEN', 'ACKNOWLEDGED', 'EXECUTING', 'RESOLVED', 'REOPENED', 'DEFERRED'))
);

CREATE TABLE USER_CREDITS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  CHECKS_REMAINING                   NUMBER(10) DEFAULT 0 NOT NULL,
  CHECKS_TOTAL                       NUMBER(10) DEFAULT 0 NOT NULL,
  PLAN_TIER                          VARCHAR2(4000 CHAR) DEFAULT 'free' NOT NULL,
  SUBSCRIPTION_ID                    NUMBER(10),
  PERIOD_START                       TIMESTAMP(6) WITH TIME ZONE,
  PERIOD_END                         TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  SCHEDULED_PLAN_CHANGE              VARCHAR2(4000 CHAR),
  SCHEDULED_PLAN_CHANGE_DATE         TIMESTAMP(6) WITH TIME ZONE,
  CANCEL_AT_PERIOD_END               BOOLEAN DEFAULT FALSE NOT NULL,
  CONSTRAINT USER_CREDITS_PKEY PRIMARY KEY (ID),
  CONSTRAINT USER_CREDITS_USER_ID_KEY UNIQUE (USER_ID),
  CONSTRAINT USER_CREDITS_PLAN_TIER_CHECK CHECK (PLAN_TIER IN ('free', 'starter', 'growth', 'scale', 'custom', 'individual', 'team', 'professional', 'business', 'enterprise', 'managed_services'))
);

CREATE TABLE USER_HC_USAGE (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_EMAIL                         VARCHAR2(255 CHAR) NOT NULL,
  HC_COUNT                           NUMBER(10) DEFAULT 0 NOT NULL,
  FIRST_RUN_AT                       TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  LAST_RUN_AT                        TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  CONSTRAINT USER_HC_USAGE_PKEY PRIMARY KEY (ID),
  CONSTRAINT USER_HC_USAGE_USER_EMAIL_KEY UNIQUE (USER_EMAIL)
);

CREATE TABLE USER_MFA (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  -- NOT NULL relaxed for Oracle: disableMfa() sets totp_secret = '' to clear the
  -- secret, and Oracle folds '' -> NULL. Empty == NULL is the Oracle-native
  -- representation, so this column is nullable here (was NOT NULL on Neon).
  TOTP_SECRET                        VARCHAR2(4000 CHAR),
  IS_ENABLED                         BOOLEAN DEFAULT FALSE NOT NULL,
  RECOVERY_CODES                     JSON DEFAULT '[]' NOT NULL,
  VERIFIED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  LAST_USED_AT                       TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT USER_MFA_PKEY PRIMARY KEY (ID),
  CONSTRAINT USER_MFA_USER_ID_KEY UNIQUE (USER_ID)
);

CREATE TABLE USER_PREFERENCES (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  USER_ID                            NUMBER(10) NOT NULL,
  HC_COMPLETION_EMAIL                BOOLEAN DEFAULT TRUE NOT NULL,
  LAST_HC_EMAIL_SENT_AT              TIMESTAMP(6) WITH TIME ZONE,
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  ONBOARDING_DISMISSED               BOOLEAN DEFAULT FALSE,
  CONSTRAINT USER_PREFERENCES_PKEY PRIMARY KEY (ID),
  CONSTRAINT USER_PREFERENCES_USER_ID_KEY UNIQUE (USER_ID)
);

CREATE TABLE USERS (
  ID                                 NUMBER(10) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  EMAIL                              VARCHAR2(255 CHAR) NOT NULL,
  NAME                               VARCHAR2(255 CHAR),
  PASSWORD_HASH                      VARCHAR2(255 CHAR),
  CREATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  UPDATED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP,
  STRIPE_SUBSCRIPTION_ID             VARCHAR2(255 CHAR),
  SUBSCRIPTION_STATUS                VARCHAR2(50 CHAR),
  SUBSCRIPTION_PLAN                  VARCHAR2(255 CHAR),
  SUBSCRIPTION_EXPIRES_AT            TIMESTAMP(6) WITH TIME ZONE,
  SUBSCRIPTION_UPDATED_AT            TIMESTAMP(6) WITH TIME ZONE,
  COMPANY_DOMAIN                     VARCHAR2(255 CHAR),
  GOOGLE_ID                          VARCHAR2(255 CHAR),
  LAST_LOGIN                         TIMESTAMP(6) WITH TIME ZONE,
  TEAM_ID                            NUMBER(10),
  SSO_PROVIDER                       VARCHAR2(4000 CHAR),
  MANAGER_ROLE                       VARCHAR2(4000 CHAR),
  CONSTRAINT USERS_PKEY PRIMARY KEY (ID),
  CONSTRAINT USERS_GOOGLE_ID_KEY UNIQUE (GOOGLE_ID),
  CONSTRAINT USERS_MANAGER_ROLE_CHECK CHECK (MANAGER_ROLE IN ('dba', 'apps_dba', 'functional', 'developer', 'manager', 'sdm', 'admin'))
);

CREATE TABLE VALIDATION_RUNS (
  ID                                 NUMBER(19) GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  CONNECTION_ID                      NUMBER(10) NOT NULL,
  USER_ID                            NUMBER(10),
  STARTED_AT                         TIMESTAMP(6) WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL,
  FINISHED_AT                        TIMESTAMP(6) WITH TIME ZONE,
  STATUS                             VARCHAR2(4000 CHAR) DEFAULT 'running' NOT NULL,
  SHARE_TOKEN                        VARCHAR2(4000 CHAR),
  SHARE_EXPIRES_AT                   TIMESTAMP(6) WITH TIME ZONE,
  SUMMARY_JSON                       JSON,
  FULL_RESULTS_JSON                  JSON,
  CONSTRAINT VALIDATION_RUNS_PKEY PRIMARY KEY (ID),
  CONSTRAINT VALIDATION_RUNS_SHARE_TOKEN_KEY UNIQUE (SHARE_TOKEN),
  CONSTRAINT VALIDATION_RUNS_STATUS_CHECK CHECK (STATUS IN ('running', 'pass', 'partial', 'fail', 'error'))
);
