-- this script updates the tables TASKANA_SCHEMA_VERSION and HISTORY_EVENTS.

SET SCHEMA %schemaName%;

INSERT INTO TASKANA_SCHEMA_VERSION (VERSION, CREATED) VALUES ('3.0.0', CURRENT_TIMESTAMP);

ALTER TABLE HISTORY_EVENTS DROP COLUMN COMMENT, DROP COLUMN OLD_DATA, DROP COLUMN NEW_DATA, ADD COLUMN DETAILS CLOB;

ALTER TABLE CLASSIFICATION ALTER COLUMN ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE CLASSIFICATION allow read access' );

ALTER TABLE WORKBASKET ALTER COLUMN ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE WORKBASKET allow read access' );

ALTER TABLE TASK ALTER COLUMN ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE TASK allow read access' );

ALTER TABLE TASK ALTER COLUMN WORKBASKET_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE TASK allow read access' );

ALTER TABLE TASK ALTER COLUMN CLASSIFICATION_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE TASK allow read access' );

ALTER TABLE DISTRIBUTION_TARGETS ALTER COLUMN SOURCE_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE DISTRIBUTION_TARGETS allow read access' );

ALTER TABLE DISTRIBUTION_TARGETS ALTER COLUMN TARGET_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE DISTRIBUTION_TARGETS allow read access' );

ALTER TABLE WORKBASKET_ACCESS_LIST ALTER COLUMN ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE WORKBASKET_ACCESS_LIST allow read access' );

ALTER TABLE WORKBASKET_ACCESS_LIST ALTER COLUMN WORKBASKET_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE WORKBASKET_ACCESS_LIST allow read access' );

ALTER TABLE OBJECT_REFERENCE ALTER COLUMN ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE OBJECT_REFERENCE allow read access' );

ALTER TABLE ATTACHMENT ALTER COLUMN ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE ATTACHMENT allow read access' );

ALTER TABLE ATTACHMENT ALTER COLUMN TASK_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE ATTACHMENT allow read access' );

ALTER TABLE ATTACHMENT ALTER COLUMN CLASSIFICATION_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE ATTACHMENT allow read access' );

ALTER TABLE TASK_COMMENT ALTER COLUMN ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE TASK_COMMENT allow read access' );

ALTER TABLE TASK_COMMENT ALTER COLUMN TASK_ID SET DATA TYPE VARCHAR(40);
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE TASK_COMMENT allow read access' );

CREATE TABLE CONFIGURATION (
    ENFORCE_SECURITY BOOLEAN NOT NULL
);

-- TSK-1227 allow null values in OBJECT_REFERENCE#SYSTEM and OBJECT_REFERENCE#SYSTEM_INSTANCE

ALTER TABLE OBJECT_REFERENCE ALTER COLUMN SYSTEM DROP NOT NULL;
ALTER TABLE OBJECT_REFERENCE ALTER COLUMN SYSTEM_INSTANCE DROP NOT NULL;
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE OBJECT_REFERENCE allow read access' );

ALTER TABLE TASK ALTER COLUMN POR_SYSTEM DROP NOT NULL;
ALTER TABLE TASK ALTER COLUMN POR_INSTANCE DROP NOT NULL;
CALL SYSPROC.ADMIN_CMD ( 'REORG TABLE TASK allow read access' );
