-- Table creation for Tier0 Oracle schema

-- Run related tables
CREATE TABLE run_status (
    id     NUMBER(11)    GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(25)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT run_sta_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/

CREATE TABLE run (
    run_id             NUMBER(11)     NOT NULL,
    status             NUMBER(11)     DEFAULT 1 NOT NULL,
    express_released   NUMBER(11)     DEFAULT 0 NOT NULL,
    hltkey             VARCHAR2(255)  NOT NULL,
    setup_label        VARCHAR2(255)  NOT NULL,
    start_time         NUMBER(11)     DEFAULT 0 NOT NULL,
    stop_time          NUMBER(11)     DEFAULT 0 NOT NULL,
    close_time         NUMBER(11)     DEFAULT 0 NOT NULL,
    lumicount          NUMBER(11)     DEFAULT 0 NOT NULL,
    in_datasvc         NUMBER(11)     DEFAULT 0 NOT NULL,
    process            VARCHAR2(255),
    acq_era            VARCHAR2(255),
    backfill           VARCHAR2(255),
    bulk_data_type     VARCHAR2(50),
    dqmuploadurl       VARCHAR2(255),
    ah_timeout         NUMBER(11),
    ah_cond_lfnbase    VARCHAR2(255),
    ah_lumi_url        VARCHAR2(255),
    cond_timeout       NUMBER(11),
    db_host            VARCHAR2(255),
    valid_mode         NUMBER(11),
    PRIMARY KEY(run_id),
    CONSTRAINT run_sta_fk FOREIGN KEY (status) REFERENCES run_status(id)
) ORGANIZATION INDEX
/

-- Configuration tables
CREATE TABLE t0_config (
    run_id   NUMBER(11)    NOT NULL,
    config   VARCHAR2(255) NOT NULL,
    PRIMARY KEY(run_id),
    CONSTRAINT t0_conf_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id)
) ORGANIZATION INDEX
/

CREATE TABLE t0_deployment_id (
    name   VARCHAR2(15)  NOT NULL,
    id     NUMBER(16)    NOT NULL,
    PRIMARY KEY(name),
    CONSTRAINT CK_DEPLOY_ID CHECK (name='deployment_id')
) ORGANIZATION INDEX
/

-- Status and type tables
CREATE TABLE processing_style (
    id     NUMBER(11)    GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(25)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT pro_sty_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/

CREATE TABLE event_scenario (
    id     NUMBER(11)    GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(50)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT eve_sce_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/

CREATE TABLE cmssw_version (
    id     NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(255)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT cms_ver_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/

CREATE TABLE stream (
    id     NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(255)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT str_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/

CREATE TABLE trigger_label (
    id     NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(255)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT tri_lab_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/

CREATE TABLE primary_dataset (
    id     NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(255)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT pri_dat_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/

CREATE TABLE storage_node (
    id     NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    name   VARCHAR2(255)  NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT sto_nod_name_uq UNIQUE(name)
) ORGANIZATION INDEX
/


-- Association tables
CREATE TABLE run_trig_primds_assoc (
    run_id      NUMBER(11) NOT NULL,
    primds_id   NUMBER(11) NOT NULL,
    trig_id     NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, primds_id, trig_id),
    CONSTRAINT run_tri_pri_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT run_tri_pri_pri_id_fk FOREIGN KEY (primds_id) REFERENCES primary_dataset(id),
    CONSTRAINT run_tri_pri_tri_id_fk FOREIGN KEY (trig_id) REFERENCES trigger_label(id)
) ORGANIZATION INDEX COMPRESS 2
/

CREATE TABLE run_primds_stream_assoc (
    run_id      NUMBER(11) NOT NULL,
    primds_id   NUMBER(11) NOT NULL,
    stream_id   NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, primds_id),
    CONSTRAINT run_pri_tri_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT run_pri_tri_pri_id_fk FOREIGN KEY (primds_id) REFERENCES primary_dataset(id),
    CONSTRAINT run_pri_tri_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id)
)
/

CREATE TABLE run_primds_scenario_assoc (
    run_id        NUMBER(11) NOT NULL,
    primds_id     NUMBER(11) NOT NULL,
    scenario_id   NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, primds_id),
    CONSTRAINT run_pri_sce_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT run_pri_sce_pri_id_fk FOREIGN KEY (primds_id) REFERENCES primary_dataset(id),
    CONSTRAINT run_pri_sce_sce_id_fk FOREIGN KEY (scenario_id) REFERENCES event_scenario(id)
) ORGANIZATION INDEX
/

CREATE TABLE run_stream_style_assoc (
    run_id      NUMBER(11) NOT NULL,
    stream_id   NUMBER(11) NOT NULL,
    style_id    NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, stream_id),
    CONSTRAINT run_str_sty_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT run_str_sty_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id),
    CONSTRAINT run_str_sty_sty_id_fk FOREIGN KEY (style_id) REFERENCES processing_style(id)
) ORGANIZATION INDEX
/

CREATE TABLE run_stream_cmssw_assoc (
    run_id             NUMBER(11) NOT NULL,
    stream_id          NUMBER(11) NOT NULL,
    online_version     NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, stream_id),
    CONSTRAINT run_str_cms_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT run_str_cms_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id),
    CONSTRAINT run_str_cms_onl_ver_fk FOREIGN KEY (online_version) REFERENCES cmssw_version(id)
) ORGANIZATION INDEX
/

CREATE TABLE run_stream_fileset_assoc (
    run_id      NUMBER(11) NOT NULL,
    stream_id   NUMBER(11) NOT NULL,
    fileset     NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, stream_id),
    CONSTRAINT run_str_fil_ass_fil_uq UNIQUE(fileset),
    CONSTRAINT run_str_fil_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT run_str_fil_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id),
    CONSTRAINT run_str_fil_fil_id_fk FOREIGN KEY (fileset) REFERENCES wmbs_fileset(id) ON DELETE CASCADE
)
/

CREATE TABLE run_stream_done (
    run_id      NUMBER(11) NOT NULL,
    stream_id   NUMBER(11) NOT NULL,
    in_datasvc  NUMBER(11) DEFAULT 0 NOT NULL,
    PRIMARY KEY(run_id, stream_id),
    CONSTRAINT run_str_don_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT run_str_don_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id)
)
/

-- Configuration tables
CREATE TABLE reco_release_config (
    run_id         NUMBER(11) NOT NULL,
    primds_id      NUMBER(11) NOT NULL,
    in_datasvc     NUMBER(11) DEFAULT 0 NOT NULL,
    released       NUMBER(11) DEFAULT 0 NOT NULL,
    fileset        NUMBER(11) NOT NULL,
    delay          NUMBER(11) NOT NULL,
    delay_offset   NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, primds_id),
    CONSTRAINT rec_rel_con_fil_uq UNIQUE(fileset),
    CONSTRAINT rec_rel_con_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT rec_rel_con_pri_id_fk FOREIGN KEY (primds_id) REFERENCES primary_dataset(id),
    CONSTRAINT rec_rel_con_fil_id_fk FOREIGN KEY (fileset) REFERENCES wmbs_fileset(id) ON DELETE CASCADE
)
/

CREATE TABLE stream_special_primds_assoc (
    stream_id   NUMBER(11) NOT NULL,
    primds_id   NUMBER(11) NOT NULL,
    PRIMARY KEY(stream_id),
    CONSTRAINT str_spe_pri_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id),
    CONSTRAINT str_spe_pri_pri_id_fk FOREIGN KEY (primds_id) REFERENCES primary_dataset(id)
) ORGANIZATION INDEX
/

-- Lumi section tables
CREATE TABLE lumi_section (
    run_id    NUMBER(11) NOT NULL,
    lumi_id   NUMBER(11) NOT NULL,
    PRIMARY KEY(run_id, lumi_id),
    CONSTRAINT lum_sec_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id)
) ORGANIZATION INDEX
/

CREATE TABLE lumi_section_closed (
    run_id      NUMBER(11) NOT NULL,
    stream_id   NUMBER(11) NOT NULL,
    lumi_id     NUMBER(11) NOT NULL,
    filecount   NUMBER(11) NOT NULL,
    insert_time NUMBER(11) NOT NULL,
    close_time  NUMBER(11) DEFAULT 0 NOT NULL,
    PRIMARY KEY(run_id, stream_id, lumi_id),
    CONSTRAINT lum_sec_clo_rl_id_fk FOREIGN KEY (run_id, lumi_id) REFERENCES lumi_section(run_id, lumi_id),
    CONSTRAINT lum_sec_clo_stre_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id)
) ORGANIZATION INDEX
/

CREATE TABLE lumi_section_split_active (
    subscription   NUMBER(11) NOT NULL,
    run_id         NUMBER(11) NOT NULL,
    lumi_id        NUMBER(11) NOT NULL,
    nfiles         NUMBER(11) NOT NULL,
    PRIMARY KEY(subscription, run_id, lumi_id),
    CONSTRAINT lum_sec_spli_act_rl_id_fk FOREIGN KEY (run_id, lumi_id) REFERENCES lumi_section(run_id, lumi_id),
    CONSTRAINT lum_sec_spli_act_stre_id_fk FOREIGN KEY (subscription) REFERENCES wmbs_subscription(id)
) ORGANIZATION INDEX
/

-- Streamer table
CREATE TABLE streamer (
    id            NUMBER(11) NOT NULL,
    p5_id         NUMBER(11) NOT NULL,
    run_id        NUMBER(11) NOT NULL,
    stream_id     NUMBER(11) NOT NULL,
    lumi_id       NUMBER(11) NOT NULL,
    insert_time   NUMBER(11) NOT NULL,
    used          NUMBER(11) DEFAULT 0 NOT NULL,
    deleted       NUMBER(11) DEFAULT 0 NOT NULL,
    skipped       NUMBER(11) DEFAULT 0 NOT NULL,
    PRIMARY KEY(id),
    CONSTRAINT str_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT str_rl_id_fk FOREIGN KEY (run_id, lumi_id) REFERENCES lumi_section(run_id, lumi_id),
    CONSTRAINT str_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id)
)
/

-- Configuration tables
CREATE TABLE repack_config (
    run_id               NUMBER(11)     NOT NULL,
    stream_id            NUMBER(11)     NOT NULL,
    proc_version         NUMBER(11)     NOT NULL,
    max_size_single_lumi NUMBER(11)     NOT NULL,
    max_size_multi_lumi  NUMBER(11)     NOT NULL,
    min_size             NUMBER(11)     NOT NULL,
    max_size             NUMBER(11)     NOT NULL,
    max_edm_size         NUMBER(11)     NOT NULL,
    max_over_size        NUMBER(11)     NOT NULL,
    max_events           NUMBER(11)     NOT NULL,
    max_files            NUMBER(11)     NOT NULL,
    cmssw_id             NUMBER(11)     NOT NULL,
    scram_arch           VARCHAR2(50)   NOT NULL,
    PRIMARY KEY (run_id, stream_id),
    CONSTRAINT rep_con_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT rep_con_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id)
) ORGANIZATION INDEX
/

CREATE TABLE express_config (
    run_id          NUMBER(11)     NOT NULL,
    stream_id       NUMBER(11)     NOT NULL,
    in_datasvc      NUMBER(11)     DEFAULT 0 NOT NULL,
    proc_version    NUMBER(11)     NOT NULL,
    write_tiers     VARCHAR2(255),
    write_dqm       NUMBER(11)     NOT NULL,
    global_tag      VARCHAR2(255)  NOT NULL,
    max_rate        NUMBER(11)     NOT NULL,
    max_events      NUMBER(11)     NOT NULL,
    max_size        NUMBER(11)     NOT NULL,
    max_files       NUMBER(11)     NOT NULL,
    max_latency     NUMBER(11)     NOT NULL,
    dqm_interval    NUMBER(11)     NOT NULL,
    cmssw_id        NUMBER(11)     NOT NULL,
    scram_arch      VARCHAR2(50)   NOT NULL,
    data_type       VARCHAR2(50)   NOT NULL,
    reco_cmssw_id   NUMBER(11),
    multicore       NUMBER(11),
    reco_scram_arch VARCHAR2(50),
    alca_skim       VARCHAR2(700),
    dqm_seq         VARCHAR2(700),
    PRIMARY KEY (run_id, stream_id),
    CONSTRAINT exp_con_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT exp_con_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id),
    CONSTRAINT exp_con_cms_id_fk FOREIGN KEY (cmssw_id) REFERENCES cmssw_version(id)
) ORGANIZATION INDEX
/

CREATE TABLE prompt_calib (
    run_id        NUMBER(11) NOT NULL,
    stream_id     NUMBER(11) NOT NULL,
    num_producer  NUMBER(11) NOT NULL,
    finished      NUMBER(11) DEFAULT 0 NOT NULL,
    PRIMARY KEY (run_id, stream_id),
    CONSTRAINT pro_cal_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT pro_cal_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id)
) ORGANIZATION INDEX
/

CREATE TABLE prompt_calib_file (
    run_id        NUMBER(11) NOT NULL,
    stream_id     NUMBER(11) NOT NULL,
    fileid        NUMBER(11) NOT NULL,
    subscription  NUMBER(11) NOT NULL,
    PRIMARY KEY (run_id, stream_id, fileid),
    CONSTRAINT pro_cal_fil_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT pro_cal_fil_str_id_fk FOREIGN KEY (stream_id) REFERENCES stream(id),
    CONSTRAINT pro_cal_fil_fil_id_fk FOREIGN KEY (fileid) REFERENCES wmbs_file_details(id) ON DELETE CASCADE,
    CONSTRAINT pro_cal_fil_sub_fk FOREIGN KEY (subscription) REFERENCES wmbs_subscription(id) ON DELETE CASCADE
) ORGANIZATION INDEX
/

CREATE TABLE reco_config (
    run_id         NUMBER(11)     NOT NULL,
    primds_id      NUMBER(11)     NOT NULL,
    in_datasvc     NUMBER(11)     DEFAULT 0 NOT NULL,
    do_reco        NUMBER(11)     NOT NULL,
    reco_split     NUMBER(11)     NOT NULL,
    write_reco     NUMBER(11)     NOT NULL,
    write_dqm      NUMBER(11)     NOT NULL,
    write_aod      NUMBER(11)     NOT NULL,
    write_miniaod  NUMBER(11)     NOT NULL,
    write_nanoaod  NUMBER(11)     NOT NULL,
    proc_version   NUMBER(11)     NOT NULL,
    cmssw_id       NUMBER(11)     NOT NULL,
    scram_arch     VARCHAR2(50)   NOT NULL,
    global_tag     VARCHAR2(255)  NOT NULL,
    multicore      NUMBER(11),
    alca_skim      VARCHAR2(700),
    physics_skim   VARCHAR2(700),
    dqm_seq        VARCHAR2(700),
    nano_flavour   VARCHAR2(50),
    PRIMARY KEY (run_id, primds_id),
    CONSTRAINT rec_con_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT rec_con_primds_id_fk FOREIGN KEY (primds_id) REFERENCES primary_dataset(id),
    CONSTRAINT rec_con_cms_id_fk FOREIGN KEY (cmssw_id) REFERENCES cmssw_version(id)
) ORGANIZATION INDEX
/

CREATE TABLE phedex_config (
    run_id            NUMBER(11) NOT NULL,
    primds_id         NUMBER(11) NOT NULL,
    archival_node_id  NUMBER(11),
    tape_node_id      NUMBER(11),
    disk_node_id      NUMBER(11),
    disk_node_reco_id NUMBER(11),
    PRIMARY KEY (run_id, primds_id),
    CONSTRAINT phe_con_run_id_fk FOREIGN KEY (run_id) REFERENCES run(run_id),
    CONSTRAINT phe_con_primds_id_fk FOREIGN KEY (primds_id) REFERENCES primary_dataset(id),
    CONSTRAINT phe_con_arc_nod_id_fk FOREIGN KEY (archival_node_id) REFERENCES storage_node(id),
    CONSTRAINT phe_con_tap_nod_id_fk FOREIGN KEY (tape_node_id) REFERENCES storage_node(id),
    CONSTRAINT phe_con_dis_nod_id_fk FOREIGN KEY (disk_node_id) REFERENCES storage_node(id)
) ORGANIZATION INDEX
/

-- Monitoring tables
CREATE TABLE workflow_monitoring (
    workflow   NUMBER(11) NOT NULL,
    tracked    NUMBER(11) DEFAULT 0 NOT NULL,
    closeout   NUMBER(11) DEFAULT 0 NOT NULL,
    PRIMARY KEY (workflow),
    CONSTRAINT wor_mon_wor_fk FOREIGN KEY (workflow) REFERENCES wmbs_workflow(id) ON DELETE CASCADE
) ORGANIZATION INDEX
/

CREATE TABLE dataset_locked (
    dataset_id  NUMBER(11) NOT NULL,
    PRIMARY KEY (dataset_id),
    CONSTRAINT dat_loc FOREIGN KEY (dataset_id) REFERENCES dbsbuffer_dataset(id) ON DELETE CASCADE
) ORGANIZATION INDEX
/ 