-- Table creation statements for Oracle

-- Independent tables (no dependencies)
CREATE TABLE wmbs_fileset (
    id          NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    name        VARCHAR2(1250) NOT NULL,
    open        NUMBER(1)      DEFAULT 0 NOT NULL,
    last_update NUMBER(11)     NOT NULL,
    CONSTRAINT wmbs_fileset_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_fileset_unique UNIQUE (name)
)
/

CREATE TABLE wmbs_location_state (
    id   NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    name VARCHAR2(100)  NOT NULL,
    CONSTRAINT wmbs_location_state_pk PRIMARY KEY (id)
)
/

CREATE TABLE wmbs_pnns (
    id  NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    pnn VARCHAR2(255),
    CONSTRAINT wmbs_pnns_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_pnns_unique UNIQUE (pnn)
)
/

CREATE TABLE wmbs_users (
    id          NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    cert_dn     VARCHAR2(255)  NOT NULL,
    name_hn     VARCHAR2(255),
    owner       VARCHAR2(255),
    grp         VARCHAR2(255),
    group_name  VARCHAR2(255),
    role_name   VARCHAR2(255),
    CONSTRAINT wmbs_users_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_users_unique UNIQUE (cert_dn, group_name, role_name)
)
/

CREATE TABLE wmbs_job_state (
    id   NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    name VARCHAR2(100) NOT NULL,
    CONSTRAINT wmbs_job_state_pk PRIMARY KEY (id)
)
/

CREATE TABLE wmbs_checksum_type (
    id   NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    type VARCHAR2(255)  NOT NULL,
    CONSTRAINT wmbs_checksum_type_pk PRIMARY KEY (id)
)
/

CREATE TABLE wmbs_sub_types (
    id       NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    name     VARCHAR2(255)  NOT NULL,
    priority NUMBER(11)     DEFAULT 0,
    CONSTRAINT wmbs_sub_types_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_sub_types_unique UNIQUE (name)
)
/

-- Tables with single dependencies
CREATE TABLE wmbs_file_details (
    id           NUMBER(11)     GENERATED BY DEFAULT AS IDENTITY,
    lfn          VARCHAR2(1250) NOT NULL,
    filesize     NUMBER(20),
    events       NUMBER(20),
    first_event  NUMBER(20)     DEFAULT 0 NOT NULL,
    merged       NUMBER(1)      DEFAULT 0 NOT NULL,
    CONSTRAINT wmbs_file_details_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_file_details_unique UNIQUE (lfn)
)
/

CREATE TABLE wmbs_location (
    id            NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    site_name     VARCHAR2(255)  NOT NULL,
    state         NUMBER(11)     NOT NULL,
    cms_name      VARCHAR2(255),
    ce_name       VARCHAR2(255),
    running_slots NUMBER(11),
    pending_slots NUMBER(11),
    plugin        VARCHAR2(255),
    state_time    NUMBER(11)     DEFAULT 0,
    CONSTRAINT wmbs_location_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_location_unique UNIQUE (site_name),
    CONSTRAINT wmbs_location_state_fk FOREIGN KEY (state) 
        REFERENCES wmbs_location_state(id)
)
/

CREATE TABLE wmbs_workflow (
    id           NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    spec         VARCHAR2(700)   NOT NULL,
    name         VARCHAR2(255)   NOT NULL,
    task         VARCHAR2(1250)  NOT NULL,
    type         VARCHAR2(255),
    owner        NUMBER(11)      NOT NULL,
    alt_fs_close NUMBER(1)       NOT NULL,
    injected     NUMBER(1)       DEFAULT 0,
    priority     NUMBER(11)      DEFAULT 0,
    CONSTRAINT wmbs_workflow_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_workflow_unique UNIQUE (name, spec, task),
    CONSTRAINT wmbs_workflow_fk_users FOREIGN KEY (owner) 
        REFERENCES wmbs_users(id) ON DELETE CASCADE
)
/

-- Tables with multiple dependencies
CREATE TABLE wmbs_fileset_files (
    fileid      NUMBER(11) NOT NULL,
    fileset     NUMBER(11) NOT NULL,
    insert_time NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_fileset_files_pk UNIQUE (fileid, fileset),
    CONSTRAINT wmbs_fileset_files_fk FOREIGN KEY (fileset) 
        REFERENCES wmbs_fileset(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_file_parent (
    child  NUMBER(11) NOT NULL,
    parent NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_file_parent_pk UNIQUE (child, parent),
    CONSTRAINT wmbs_file_parent_child_fk FOREIGN KEY (child) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_file_parent_parent_fk FOREIGN KEY (parent) 
        REFERENCES wmbs_file_details(id)
)
/

CREATE TABLE wmbs_file_runlumi_map (
    fileid     NUMBER(11) NOT NULL,
    run        NUMBER(11) NOT NULL,
    lumi       NUMBER(11) NOT NULL,
    num_events NUMBER(20),
    CONSTRAINT wmbs_file_runlumi_pk PRIMARY KEY (fileid, run, lumi),
    CONSTRAINT wmbs_file_runlumi_fk FOREIGN KEY (fileid) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_location_pnns (
    location NUMBER(11),
    pnn      NUMBER(11),
    CONSTRAINT wmbs_location_pnns_pk UNIQUE (location, pnn),
    CONSTRAINT wmbs_location_pnns_loc_fk FOREIGN KEY (location) 
        REFERENCES wmbs_location(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_location_pnns_pnn_fk FOREIGN KEY (pnn) 
        REFERENCES wmbs_pnns(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_file_location (
    fileid   NUMBER(11) NOT NULL,
    pnn      NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_file_location_pk PRIMARY KEY (fileid, pnn),
    CONSTRAINT wmbs_file_location_fk_file FOREIGN KEY (fileid) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_file_location_fk_pnn FOREIGN KEY (pnn)
        REFERENCES wmbs_pnns(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_workflow_output (
    workflow_id           NUMBER(11) NOT NULL,
    output_identifier     VARCHAR2(255) NOT NULL,
    output_fileset        NUMBER(11) NOT NULL,
    merged_output_fileset NUMBER(11),
    CONSTRAINT wmbs_workflow_output_pk PRIMARY KEY (workflow_id, output_identifier, output_fileset),
    CONSTRAINT wmbs_workflow_output_fk1 FOREIGN KEY (workflow_id) 
        REFERENCES wmbs_workflow(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_workflow_output_fk2 FOREIGN KEY (output_fileset) 
        REFERENCES wmbs_fileset(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_workflow_output_fk3 FOREIGN KEY (merged_output_fileset) 
        REFERENCES wmbs_fileset(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_subscription (
    id           NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    fileset      NUMBER(11)     NOT NULL,
    workflow     NUMBER(11)     NOT NULL,
    split_algo   VARCHAR2(255)  NOT NULL,
    subtype      NUMBER(11)     NOT NULL,
    last_update  NUMBER(11)     NOT NULL,
    finished     NUMBER(1)      DEFAULT 0,
    CONSTRAINT wmbs_subscription_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_subscription_fk_fileset FOREIGN KEY (fileset) 
        REFERENCES wmbs_fileset(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_subscription_fk_workflow FOREIGN KEY (workflow) 
        REFERENCES wmbs_workflow(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_subscription_fk_subtype FOREIGN KEY (subtype) 
        REFERENCES wmbs_sub_types(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_subscription_validation (
    subscription_id NUMBER(11) NOT NULL,
    location_id    NUMBER(11) NOT NULL,
    valid         NUMBER(1),
    CONSTRAINT wmbs_sub_val_pk UNIQUE (subscription_id, location_id),
    CONSTRAINT wmbs_sub_val_fk1 FOREIGN KEY (subscription_id) 
        REFERENCES wmbs_subscription(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_sub_val_fk2 FOREIGN KEY (location_id) 
        REFERENCES wmbs_location(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_sub_files_available (
    subscription NUMBER(11) NOT NULL,
    fileid      NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_sub_files_available_pk PRIMARY KEY (subscription, fileid),
    CONSTRAINT wmbs_sub_files_available_fk_sub FOREIGN KEY (subscription) 
        REFERENCES wmbs_subscription(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_sub_files_available_fk_file FOREIGN KEY (fileid) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_sub_files_acquired (
    subscription NUMBER(11) NOT NULL,
    fileid      NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_sub_files_acquired_pk PRIMARY KEY (subscription, fileid),
    CONSTRAINT wmbs_sub_files_acquired_fk_sub FOREIGN KEY (subscription) 
        REFERENCES wmbs_subscription(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_sub_files_acquired_fk_file FOREIGN KEY (fileid) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_sub_files_failed (
    subscription NUMBER(11) NOT NULL,
    fileid      NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_sub_files_failed_pk PRIMARY KEY (subscription, fileid),
    CONSTRAINT wmbs_sub_files_failed_fk_sub FOREIGN KEY (subscription) 
        REFERENCES wmbs_subscription(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_sub_files_failed_fk_file FOREIGN KEY (fileid) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_sub_files_complete (
    subscription NUMBER(11) NOT NULL,
    fileid      NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_sub_files_complete_pk PRIMARY KEY (subscription, fileid),
    CONSTRAINT wmbs_sub_files_complete_fk_sub FOREIGN KEY (subscription) 
        REFERENCES wmbs_subscription(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_sub_files_complete_fk_file FOREIGN KEY (fileid) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_jobgroup (
    id           NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    subscription NUMBER(11)     NOT NULL,
    guid         VARCHAR2(255),
    output       NUMBER(11),
    last_update  NUMBER(11)     NOT NULL,
    location     NUMBER(11),
    CONSTRAINT wmbs_jobgroup_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_jobgroup_unique UNIQUE (guid),
    CONSTRAINT wmbs_jobgroup_fk_sub FOREIGN KEY (subscription) 
        REFERENCES wmbs_subscription(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_jobgroup_fk_output FOREIGN KEY (output) 
        REFERENCES wmbs_fileset(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_job (
    id           NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    jobgroup     NUMBER(11)     NOT NULL,
    name         VARCHAR2(255),
    state        NUMBER(11)     NOT NULL,
    state_time   NUMBER(11)     NOT NULL,
    retry_count  NUMBER(6)     DEFAULT 0,
    couch_record VARCHAR2(255),
    location     NUMBER(11),
    outcome      NUMBER(11)     DEFAULT 0,
    cache_dir    VARCHAR2(1250) DEFAULT NULL,
    fwjr_path    VARCHAR2(1250),
    CONSTRAINT wmbs_job_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_job_unique UNIQUE (name, cache_dir, fwjr_path),
    CONSTRAINT wmbs_job_fk_group FOREIGN KEY (jobgroup) 
        REFERENCES wmbs_jobgroup(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_job_fk_state FOREIGN KEY (state) 
        REFERENCES wmbs_job_state(id),
    CONSTRAINT wmbs_job_fk_location FOREIGN KEY (location) 
        REFERENCES wmbs_location(id)
)
/

CREATE TABLE wmbs_job_assoc (
    job    NUMBER(11) NOT NULL,
    fileid NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_job_assoc_pk PRIMARY KEY (job, fileid),
    CONSTRAINT wmbs_job_assoc_fk1 FOREIGN KEY (job) 
        REFERENCES wmbs_job(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_job_assoc_fk2 FOREIGN KEY (fileid) 
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_job_mask (
    job           NUMBER(15) NOT NULL,
    FirstEvent    NUMBER(20),
    LastEvent     NUMBER(20),
    FirstLumi     NUMBER(15),
    LastLumi      NUMBER(15),
    FirstRun      NUMBER(15),
    LastRun       NUMBER(15),
    inclusivemask NUMBER(1)  DEFAULT 1,
    CONSTRAINT wmbs_job_mask_fk FOREIGN KEY (job) 
        REFERENCES wmbs_job(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_file_checksums (
    fileid NUMBER(11)    NOT NULL,
    typeid NUMBER(11)    NOT NULL,
    cksum  VARCHAR2(100) NOT NULL,
    CONSTRAINT wmbs_file_checksums_pk UNIQUE (fileid, typeid),
    CONSTRAINT wmbs_file_checksums_type_fk FOREIGN KEY (typeid)
        REFERENCES wmbs_checksum_type(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_file_checksums_file_fk FOREIGN KEY (fileid)
        REFERENCES wmbs_file_details(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_workunit (
    id              NUMBER(11) GENERATED BY DEFAULT AS IDENTITY,
    taskid          NUMBER(11) NOT NULL,
    retry_count     NUMBER(6) DEFAULT 0,
    last_unit_count NUMBER(11) NOT NULL,
    last_submit_time NUMBER(11) NOT NULL,
    status          NUMBER(1)  DEFAULT 0,
    CONSTRAINT wmbs_workunit_pk PRIMARY KEY (id),
    CONSTRAINT wmbs_workunit_fk_task FOREIGN KEY (taskid) 
        REFERENCES wmbs_workflow(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_job_workunit_assoc (
    job      NUMBER(11) NOT NULL,
    workunit NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_job_wu_assoc_pk PRIMARY KEY (job, workunit),
    CONSTRAINT wmbs_job_wu_assoc_fk1 FOREIGN KEY (job) 
        REFERENCES wmbs_job(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_job_wu_assoc_fk2 FOREIGN KEY (workunit) 
        REFERENCES wmbs_workunit(id) ON DELETE CASCADE
)
/

CREATE TABLE wmbs_frl_workunit_assoc (
    workunit   NUMBER(11) NOT NULL,
    firstevent NUMBER(11) DEFAULT 0,
    lastevent  NUMBER(11) DEFAULT 0,
    fileid     NUMBER(11) NOT NULL,
    run        NUMBER(11) NOT NULL,
    lumi       NUMBER(11) NOT NULL,
    CONSTRAINT wmbs_frl_wu_assoc_pk PRIMARY KEY (workunit, fileid, run, lumi),
    CONSTRAINT wmbs_frl_wu_assoc_fk1 FOREIGN KEY (workunit) 
        REFERENCES wmbs_workunit(id) ON DELETE CASCADE,
    CONSTRAINT wmbs_frl_wu_assoc_fk2 FOREIGN KEY (fileid, run, lumi) 
        REFERENCES wmbs_file_runlumi_map(fileid, run, lumi) ON DELETE CASCADE
)
/ 