--MARS-770

set verify off
SET serveroutput ON
--SET TIMING ON
whenever sqlerror exit sql.sqlcode rollback;

column owner format a15
column object_name format a30
column spoolname new_value spoolname 

select 'install_package_MarsGeneralRelease_MARS770_file_manager_' || name || '_' ||to_char(sysdate,'YYYYMMDD_HH24MISS')||'.log' spoolname from v$pdbs;
spool '&spoolname'


prompt ##### started at time #####
select systimestamp from dual;
prompt ##### database name #####
select name pdb_name from v$pdbs;

--set echo on
--=============================================================================================================================
-- start install --MARS-770 file manager relevant objects
--=============================================================================================================================

create or replace type CT_MRDS.T_FILENAME as object  (
       FILENAME  varchar2(200)
      ,PATHNAME  varchar2(800)
      ,YEAR       varchar2(4)
      ,MONTH      varchar2(2)
   );
/
create type CT_MRDS.T_FILENAMES is table of T_FILENAME;
/

--
-- CT_MRDS.A_FILE_MANAGER_CONFIG
--

CREATE TABLE "CT_MRDS"."A_FILE_MANAGER_CONFIG" (
    environment_id VARCHAR2(100),
    config_variable VARCHAR2(200),
    config_variable_value VARCHAR2(200),
    config_variable_comment VARCHAR2(600),
    PRIMARY KEY (environment_id, config_variable)
);

CREATE TABLE "CT_MRDS"."A_TABLE_STAT"
   (	"A_TABLE_STAT_KEY"                  NUMBER(38,0) PRIMARY KEY, 
        "A_SOURCE_FILE_CONFIG_KEY"        NUMBER(38,0) NOT NULL,
        "TABLE_NAME"                      VARCHAR2(200) NOT NULL,
        "FILE_COUNT"                      NUMBER(38,0),
        "OVER_HIST_THRESOLD_FILE_COUNT"   NUMBER(38,0),
        "ROW_COUNT"                       NUMBER(38,0), 
        "OVER_HIST_THRESOLD_ROW_COUNT"    NUMBER(38,0), 
        "SIZE"                            NUMBER(38,0),
        "OVER_HIST_THRESOLD_SIZE"         NUMBER(38,0),
        "HIST_THRESHOLD_DAYS"             NUMBER(4,0),
	     "CREATED"                         TIMESTAMP (6) default systimestamp
   )  
  TABLESPACE "DATA" ;

alter table "CT_MRDS"."A_TABLE_STAT" add constraint A_TABLE_STAT_UK1 unique("A_SOURCE_FILE_CONFIG_KEY");

CREATE SEQUENCE CT_MRDS.A_TABLE_STAT_KEY_SEQ START WITH 1 INCREMENT BY 1;
GRANT SELECT ON CT_MRDS.A_TABLE_STAT_KEY_SEQ TO MRDS_LOADER_ROLE;

---
CREATE TABLE "CT_MRDS"."A_TABLE_STAT_HIST"
   (	"A_TABLE_STAT_HIST_KEY"           NUMBER(38,0) PRIMARY KEY, 
        "A_SOURCE_FILE_CONFIG_KEY"        NUMBER(38,0) NOT NULL,
        "TABLE_NAME"                      VARCHAR2(200) NOT NULL,
        "FILE_COUNT"                      NUMBER(38,0),
        "OVER_HIST_THRESOLD_FILE_COUNT"   NUMBER(38,0),
        "ROW_COUNT"                       NUMBER(38,0), 
        "OVER_HIST_THRESOLD_ROW_COUNT"    NUMBER(38,0), 
        "SIZE"                            NUMBER(38,0),
        "OVER_HIST_THRESOLD_SIZE"         NUMBER(38,0),
        "HIST_THRESHOLD_DAYS"             NUMBER(4,0),
	     "CREATED"                         TIMESTAMP (6) default systimestamp
   )  
  TABLESPACE "DATA" ;

--
-- CT_MRDS.A_SOURCE
--


CREATE TABLE "CT_MRDS"."A_SOURCE"
   (	"A_SOURCE_KEY" VARCHAR2(30), 
        "SOURCE_NAME" VARCHAR2(200)
   )  
  TABLESPACE "DATA" ;

ALTER TABLE "CT_MRDS"."A_SOURCE"
ADD CONSTRAINT A_SOURCE_PK PRIMARY KEY (A_SOURCE_KEY);
GRANT SELECT, INSERT, UPDATE, DELETE ON "CT_MRDS"."A_SOURCE"  TO MRDS_LOADER_ROLE;

--
-- CT_MRDS.A_SOURCE_FILE_CONFIG
--

CREATE SEQUENCE CT_MRDS.A_SOURCE_FILE_CONFIG_KEY_SEQ START WITH 1 INCREMENT BY 1;
GRANT SELECT ON CT_MRDS.A_SOURCE_FILE_CONFIG_KEY_SEQ TO MRDS_LOADER_ROLE;

-- DROP TABLE "CT_MRDS"."A_SOURCE_FILE_CONFIG";

CREATE TABLE "CT_MRDS"."A_SOURCE_FILE_CONFIG"
   (	"A_SOURCE_FILE_CONFIG_KEY" NUMBER(38,0) NOT NULL ENABLE, 
        "A_SOURCE_KEY" VARCHAR2(30) NOT NULL ENABLE,
        "SOURCE_FILE_TYPE" VARCHAR2(200), -- Can be 'INPUT' or 'CONTAINER' or 'LOAD_CONFIG', the latter is for files that are mapped to multiple tables and split into different input files
        "SOURCE_FILE_ID" VARCHAR2(200), 
        "SOURCE_FILE_DESC" VARCHAR2(2000), 
        "SOURCE_FILE_NAME_PATTERN" VARCHAR2(200), 
        "TABLE_ID" VARCHAR2(200), 
        "TEMPLATE_TABLE_NAME" VARCHAR2(200), 
        "CONTAINER_FILE_KEY" NUMBER(38,0),
        "DAYS_FOR_HISTORY_THRESHOLD" NUMBER(4,0), 
        "FILES_COUNT_OVER_HISTORY_THRESHOLD" NUMBER(38,0), 
        "BYTES_SUM_OVER_HISTORY_THRESHOLD" NUMBER(38,0), 
        "ODS_SCHEMA_NAME" VARCHAR2(100), 
        "ROWS_COUNT_OVER_HISTORY_THRESHOLD" NUMBER(38,0), 
        "HOURS_TO_EXPIRE_STATISTICS" NUMBER(38,3)
   )  
  TABLESPACE "DATA" 
;

ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_CONFIG"
ADD CONSTRAINT A_SOURCE_FILE_CONFIG_PK PRIMARY KEY (A_SOURCE_FILE_CONFIG_KEY);
ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_CONFIG"
ADD CONSTRAINT SOURCE_FILE_TYPE_CHK
  CHECK (SOURCE_FILE_TYPE IN ('INPUT', 'CONTAINER', 'LOAD_CONFIG'));
ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_CONFIG" 
ADD CONSTRAINT ASFC_A_SOURCE_KEY_FK FOREIGN KEY("A_SOURCE_KEY") REFERENCES "CT_MRDS"."A_SOURCE"("A_SOURCE_KEY");
ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_CONFIG" 
ADD CONSTRAINT ASFC_CONTAINER_FILE_KEY_FK FOREIGN KEY("CONTAINER_FILE_KEY") REFERENCES "CT_MRDS"."A_SOURCE_FILE_CONFIG"("A_SOURCE_FILE_CONFIG_KEY");
ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_CONFIG" 
ADD CONSTRAINT A_SOURCE_FILE_CONFIG_UQ1 UNIQUE("SOURCE_FILE_TYPE","SOURCE_FILE_ID","TABLE_ID");

GRANT SELECT, INSERT, UPDATE, DELETE ON "CT_MRDS"."A_SOURCE_FILE_CONFIG"  TO MRDS_LOADER_ROLE;

--
-- CT_MRDS.A_SOURCE_FILE_RECEIVED
--

CREATE SEQUENCE CT_MRDS.A_SOURCE_FILE_RECEIVED_KEY_SEQ START WITH 1 INCREMENT BY 1;
GRANT SELECT ON CT_MRDS.A_SOURCE_FILE_RECEIVED_KEY_SEQ TO MRDS_LOADER_ROLE;

-- DROP TABLE "CT_MRDS"."A_SOURCE_FILE_RECEIVED";

CREATE TABLE "CT_MRDS"."A_SOURCE_FILE_RECEIVED"
   (	"A_SOURCE_FILE_RECEIVED_KEY" NUMBER(38,0) NOT NULL ENABLE, 
        "A_SOURCE_FILE_CONFIG_KEY" NUMBER(38,0) NOT NULL ENABLE,
        "SOURCE_FILE_NAME"  VARCHAR2(1000) NOT NULL,
        "CHECKSUM"          VARCHAR2(128), 
	      "CREATED"           TIMESTAMP (6) WITH TIME ZONE, 
	      "BYTES"             NUMBER,
        "RECEPTION_DATE" DATE NOT NULL,
        "PROCESSING_STATUS" VARCHAR2(200),
        "EXTERNAL_TABLE_NAME" VARCHAR2(200),
        "PARTITION_YEAR" VARCHAR2(4), 
        "PARTITION_MONTH" VARCHAR2(2),
        "HIST_FILE_NAME"	VARCHAR2(1000)
   )  
  TABLESPACE "DATA" ;

ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_RECEIVED"
ADD CONSTRAINT A_SOURCE_FILE_RECEIVED_PK PRIMARY KEY (A_SOURCE_FILE_RECEIVED_KEY);
ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_RECEIVED" 
ADD CONSTRAINT ASFR_A_SOURCE_FILE_CONFIG_KEY_FK FOREIGN KEY("A_SOURCE_FILE_CONFIG_KEY") REFERENCES "CT_MRDS"."A_SOURCE_FILE_CONFIG"("A_SOURCE_FILE_CONFIG_KEY");

ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_RECEIVED"
ADD CONSTRAINT A_SOURCE_FILE_RECEIVED_CHK
  CHECK (PROCESSING_STATUS IN ('RECEIVED', 'VALIDATED', 'VALIDATION_FAILED', 'READY_FOR_INGESTION', 'INGESTED', 'ARCHIVED'));
  
-- Because "CREATED" column is type: TIMESTAMP WITH TIMEZONE, the unique constraint cannot be created.
-- As a workaround the unique index is created: A_SOURCE_FILE_RECEIVED_UK1
--ALTER TABLE "CT_MRDS"."A_SOURCE_FILE_RECEIVED" 
--ADD CONSTRAINT A_SOURCE_FILE_RECEIVED_UK1 UNIQUE("CHECKSUM","CREATED","BYTES");
create unique index CT_MRDS.A_SOURCE_FILE_RECEIVED_UK1 on CT_MRDS.A_SOURCE_FILE_RECEIVED("CHECKSUM","CREATED","BYTES");

GRANT SELECT, INSERT, UPDATE, DELETE ON "CT_MRDS"."A_SOURCE_FILE_RECEIVED"  TO MRDS_LOADER_ROLE;

--
-- CT_MRDS.A_COLUMN_DATE_FORMAT
--

-- DROP TABLE "CT_MRDS"."A_COLUMN_DATE_FORMAT";

CREATE TABLE "CT_MRDS"."A_COLUMN_DATE_FORMAT"
   (	   "TEMPLATE_TABLE_NAME" VARCHAR2(200) NOT NULL ENABLE,
        "COLUMN_NAME" VARCHAR2(200) NOT NULL,
        "DATE_FORMAT" VARCHAR2(200) NOT NULL
   )  
  TABLESPACE "DATA" ;

ALTER TABLE "CT_MRDS"."A_COLUMN_DATE_FORMAT" 
ADD CONSTRAINT A_COLUMN_DATE_FORMAT_PK PRIMARY KEY (TEMPLATE_TABLE_NAME, COLUMN_NAME);

GRANT SELECT, INSERT, UPDATE, DELETE ON "CT_MRDS"."A_COLUMN_DATE_FORMAT"  TO MRDS_LOADER_ROLE;

--
-- CT_MRDS.A_WORKFLOW_HISTORY_PROPERTY
--
CREATE TABLE CT_MRDS.A_WORKFLOW_HISTORY_PROPERTY
   (
      "A_WORKFLOW_HISTORY_KEY" NUMBER,
      "SERVICE_NAME" VARCHAR2(200),
      "PROPERTY" VARCHAR2(200),
      "VALUE" VARCHAR2(4000 CHAR)
   )
  TABLESPACE "DATA" ;

--ALTER TABLE "CT_MRDS"."A_WORKFLOW_HISTORY_PROPERTY" 
--ADD CONSTRAINT A_WORKFLOW_HISTORY_FK FOREIGN KEY (A_WORKFLOW_HISTORY_KEY, SERVICE_NAME) REFERENCES CT_MRDS.A_WORKFLOW_HISTORY (A_WORKFLOW_HISTORY_KEY, SERVICE_NAME)
--;
CREATE INDEX CT_MRDS.A_WORKFLOW_HISTORY_PROPERTY_IDX1 ON CT_MRDS.A_WORKFLOW_HISTORY_PROPERTY (A_WORKFLOW_HISTORY_KEY, SERVICE_NAME);

