--- CHI ---
rename table tctg_chi_category_hierarchy to chi_copy;
create table tctg_chi_category_hierarchy
( 
    chi_company_id                integer not null,
    chi_cat_tree_id               integer,
    chi_parent_id                 integer not null,
    chi_child_id                  integer not null,
    chi_version_id                integer,
    chi_next_version_id           integer not null,
    constraint chi_0_pk
        primary key (chi_parent_id, chi_child_id, chi_next_version_id, chi_company_id)
) 

in users index in indx;


drop synonym chi;
create synonym chi for tctg_chi_category_hierarchy;

insert into tctg_chi_category_hierarchy select * from chi_copy;
commit;

drop index ictg_chi_0;
create index ictg_chi_0 on tctg_chi_category_hierarchy (
        chi_child_id,
        chi_next_version_id,
        chi_version_id,
        chi_company_id,
        chi_cat_tree_id,
        chi_parent_id
)   ALLOW REVERSE SCANS;

drop index ictg_chi_1;
create index ictg_chi_1 on tctg_chi_category_hierarchy (
        chi_parent_id,
        chi_next_version_id,
        chi_version_id,
        chi_company_id,
        chi_cat_tree_id,
        chi_child_id
)   ALLOW REVERSE SCANS;
select count(1) from tctg_chi_category_hierarchy;
select count(1) from chi_copy;
commit;

--- CAT ---
rename table tctg_cat_category to cat_copy;
create table tctg_cat_category
( 
    cat_company_id                integer not null,
    cat_id                        integer not null,
    cat_container_id              integer not null,
    cat_primary_key               varchar(300) not null,
    cat_version_id                integer,
    cat_next_version_id           integer not null,
    cat_status                    varchar(300),
    cat_collab_info               varchar(3000),
    cat_last_modified             bigint,
    constraint cat_0_pk
        primary key (cat_id, cat_next_version_id, cat_container_id, cat_company_id),
    constraint cat_1_uk
        unique (cat_container_id, cat_primary_key, cat_next_version_id)
) 

in users index in indx;


drop synonym cat;
create synonym cat for tctg_cat_category;

insert into tctg_cat_category select * from cat_copy;
commit;

drop index ictg_cat_0;
create index ictg_cat_0 on tctg_cat_category (
        cat_container_id,
        cat_next_version_id,
        cat_version_id,
        cat_primary_key,
        cat_id
)   ALLOW REVERSE SCANS;

drop index ictg_cat_1;
create index ictg_cat_1 on tctg_cat_category (
        cat_primary_key,
        cat_next_version_id,
        cat_version_id,
        cat_container_id,
        cat_id
)   ALLOW REVERSE SCANS;

drop index ictg_cat_2;
create index ictg_cat_2 on tctg_cat_category (
        cat_container_id,
        cat_last_modified,
        cat_id
)   ALLOW REVERSE SCANS;
select count(1) from tctg_cat_category;
select count(1) from cat_copy;
commit;

--- ITM ---
rename table tctg_itm_item to itm_copy;
create table tctg_itm_item
( 
    itm_company_id                integer not null,
    itm_id                        integer not null,
    itm_container_id              integer not null,
    itm_primary_key               varchar(300) not null,
    itm_version_id                integer,
    itm_next_version_id           integer not null,
    itm_status                    varchar(300),
    itm_collab_info               varchar(3000),
    itm_last_modified             bigint,
    constraint itm_0_pk
        primary key (itm_id, itm_next_version_id, itm_container_id, itm_company_id),
    constraint itm_1_uk
        unique (itm_container_id, itm_primary_key, itm_next_version_id)
) 

in users index in indx;


drop synonym itm;
create synonym itm for tctg_itm_item;

insert into tctg_itm_item select * from itm_copy;
commit;

drop index ictg_itm_0;
create index ictg_itm_0 on tctg_itm_item (
        itm_primary_key,
        itm_container_id,
        itm_next_version_id,
        itm_version_id,
        itm_id,
        itm_company_id
)   ALLOW REVERSE SCANS;

drop index ictg_itm_1;
create index ictg_itm_1 on tctg_itm_item (
        itm_container_id,
        itm_next_version_id,
        itm_version_id,
        itm_id,
        itm_company_id
)   ALLOW REVERSE SCANS;
select count(1) from tctg_itm_item;
select count(1) from itm_copy;
commit;

--- ICM ---
rename table tctg_icm_item_category_map to icm_copy;
create table tctg_icm_item_category_map
( 
    icm_company_id                integer not null,
    icm_catalog_id                integer,
    icm_item_id                   integer not null,
    icm_cat_tree_id               integer not null,
    icm_category_id               integer not null,
    icm_version_id                integer,
    icm_next_version_id           integer not null,
    constraint icm_0_pk
        primary key (icm_item_id, icm_next_version_id, icm_cat_tree_id, icm_category_id, icm_company_id)
) 

in users index in indx;

drop synonym icm;
create synonym icm for tctg_icm_item_category_map;

insert into tctg_icm_item_category_map select * from icm_copy;
commit;

drop index ictg_icm_0;
create index ictg_icm_0 on tctg_icm_item_category_map (
        icm_item_id,
        icm_category_id,
        icm_next_version_id,
        icm_version_id,
        icm_cat_tree_id,
        icm_catalog_id
)   ALLOW REVERSE SCANS;

drop index ictg_icm_1;
create index ictg_icm_1 on tctg_icm_item_category_map (
        icm_category_id,
        icm_next_version_id,
        icm_version_id,
        icm_cat_tree_id,
        icm_catalog_id,
        icm_item_id
)   ALLOW REVERSE SCANS;

drop index ictg_icm_2;
create index ictg_icm_2 on tctg_icm_item_category_map (
        icm_catalog_id,
        icm_next_version_id,
        icm_version_id,
        icm_cat_tree_id,
        icm_category_id,
        icm_item_id
)   ALLOW REVERSE SCANS;
select count(1) from tctg_icm_item_category_map;
select count(1) from icm_copy;
commit;

--- CFP ---
rename table tctg_cfp_cat_full_paths to cfp_copy;
create table tctg_cfp_cat_full_paths
( 
    cfp_company_id                integer,
    cfp_cat_tree_id               integer not null,
    cfp_category_id               integer,
    cfp_full_path_type            varchar(100) not null,
    cfp_full_path                 varchar(850) not null,
    cfp_version_id                integer,
    cfp_next_version_id           integer not null,
    constraint cfp_0_pk
        primary key (cfp_full_path, cfp_full_path_type, cfp_next_version_id, cfp_cat_tree_id)
) 

in users index in indx;


drop synonym cfp;
create synonym cfp for tctg_cfp_cat_full_paths;

insert into tctg_cfp_cat_full_paths select * from cfp_copy;
commit;

drop index ictg_cfp_0;
create index ictg_cfp_0 on tctg_cfp_cat_full_paths (
        cfp_category_id,
        cfp_next_version_id,
        cfp_version_id,
        cfp_company_id
)   ALLOW REVERSE SCANS;

drop index ictg_cfp_1;
create index ictg_cfp_1 on tctg_cfp_cat_full_paths (
        cfp_cat_tree_id,
        cfp_full_path_type,
        cfp_next_version_id,
        cfp_version_id
)   ALLOW REVERSE SCANS;

select count(1) from tctg_cfp_cat_full_paths;
select count(1) from cfp_copy;
commit;

--- NOH ---
rename table tctg_noh_node_hierarchy to noh_copy;
create table tctg_noh_node_hierarchy
( 
    noh_company_id                integer not null,
    noh_spec_id                   integer not null,
    noh_parent_node_id            integer,
    noh_child_node_id             integer,
    noh_order                     integer not null,
    noh_version_id                integer,
    noh_next_version_id           integer not null,
    constraint noh_0_pk
        primary key (noh_spec_id, noh_next_version_id, noh_order, noh_company_id)
) 

in users index in indx;


drop synonym noh;
create synonym noh for tctg_noh_node_hierarchy;

insert into tctg_noh_node_hierarchy select * from noh_copy;
commit;

drop index ictg_noh_0;
create index ictg_noh_0 on tctg_noh_node_hierarchy (
        noh_child_node_id,
        noh_next_version_id,
        noh_version_id,
        noh_spec_id,
        noh_company_id
)   ALLOW REVERSE SCANS;

drop index ictg_noh_1;
create index ictg_noh_1 on tctg_noh_node_hierarchy (
        noh_parent_node_id,
        noh_next_version_id,
        noh_version_id,
        noh_spec_id,
        noh_company_id
)   ALLOW REVERSE SCANS;

drop index ictg_noh_2;
create index ictg_noh_2 on tctg_noh_node_hierarchy (
        noh_child_node_id,
        noh_parent_node_id,
        noh_next_version_id,
        noh_version_id,
        noh_spec_id,
        noh_company_id
)   ALLOW REVERSE SCANS;
select count(1) from tctg_noh_node_hierarchy;
select count(1) from noh_copy;
commit;

--- NOD ---
rename table tctg_nod_node to nod_copy;
create table tctg_nod_node
( 
    nod_company_id                integer not null,
    nod_spec_id                   integer not null,
    nod_id                        integer not null,
    nod_type                      varchar(100),
    nod_name                      varchar(800),
    nod_version_id                integer,
    nod_next_version_id           integer not null,
    constraint nod_0_pk
        primary key (nod_id, nod_next_version_id, nod_spec_id, nod_company_id)
) 

in users index in indx;


drop synonym nod;
create synonym nod for tctg_nod_node;

insert into tctg_nod_node select * from nod_copy;
commit;

drop index ictg_nod_0;
create index ictg_nod_0 on tctg_nod_node (
        nod_company_id
)   ALLOW REVERSE SCANS;

drop index ictg_nod_1;
create index ictg_nod_1 on tctg_nod_node (
        nod_spec_id,
        nod_next_version_id,
        nod_version_id,
        nod_company_id
)   ALLOW REVERSE SCANS;

drop index ictg_nod_2;
create index ictg_nod_2 on tctg_nod_node (
        nod_name,
        nod_spec_id,
        nod_next_version_id,
        nod_version_id,
        nod_company_id,
        nod_id
)   ALLOW REVERSE SCANS;
select count(1) from tctg_nod_node;
select count(1) from nod_copy;
commit;

--- WFE ---
rename table twfl_wfe_workflow_event to wfe_copy;
create table twfl_wfe_workflow_event
( 
    wfe_cmp_id                    integer,
    wfe_id                        integer not null,
    wfe_event_type                varchar(50),
    wfe_event_status              varchar(50),
    wfe_start_time                timestamp,
    wfe_finish_time               timestamp,
    wfe_acquire_lock              char,
    wfe_collaboration_area_id     integer,
    wfe_comment                   varchar(3000),
    wfe_source_step_at_path_id    integer,
    wfe_dest_step_at_path_ids     varchar(900),
    wfe_prior_step_at_path_id     integer,
    wfe_deadline_step_at_path_id  integer,
    wfe_exit_value                varchar(3000),
    wfe_user_id                   integer,
    wfe_error_message             varchar(3000),
    wfe_row_order                 integer,
    constraint wfe_0_pk
        primary key (wfe_id)
) 

in users index in indx;


drop synonym wfe;
create synonym wfe for twfl_wfe_workflow_event;

insert into twfl_wfe_workflow_event select * from wfe_copy;
commit;

drop index iwfl_wfe_0;
create index iwfl_wfe_0 on twfl_wfe_workflow_event (
        wfe_event_status,
        wfe_id
)   ALLOW REVERSE SCANS;

select count(1) from twfl_wfe_workflow_event;
select count(1) from wfe_copy;

--- FK ---
ALTER TABLE twfl_wee_workflow_event_entry ADD CONSTRAINT wee_fk_0 FOREIGN KEY (wee_event_id) references twfl_wfe_workflow_event (wfe_id);
