--
-- Title:      Remove all JBPM related data from the database
-- Database:   PostgreSQL
-- Since:      V5.2
-- Author:     Stefan Kopf
--
-- Please contact support@alfresco.com if you need assistance with the upgrade.
--


-- Drop all foreign keys

ALTER TABLE JBPM_ACTION DROP CONSTRAINT fk_action_actndel; -- (optional)
ALTER TABLE JBPM_ACTION DROP CONSTRAINT FK_ACTION_EVENT; -- (optional)
ALTER TABLE JBPM_ACTION DROP CONSTRAINT FK_ACTION_EXPTHDL; -- (optional)
ALTER TABLE JBPM_ACTION DROP CONSTRAINT FK_ACTION_PROCDEF; -- (optional)
ALTER TABLE JBPM_ACTION DROP CONSTRAINT FK_ACTION_REFACT; -- (optional)
ALTER TABLE JBPM_ACTION DROP CONSTRAINT fk_crtetimeract_ta; -- (optional)
ALTER TABLE JBPM_BYTEARRAY DROP CONSTRAINT FK_BYTEARR_FILDEF; -- (optional)
ALTER TABLE JBPM_BYTEBLOCK DROP CONSTRAINT FK_BYTEBLOCK_FILE; -- (optional)
ALTER TABLE JBPM_COMMENT DROP CONSTRAINT FK_COMMENT_TOKEN; -- (optional)
ALTER TABLE JBPM_COMMENT DROP CONSTRAINT FK_COMMENT_TSK; -- (optional)
ALTER TABLE JBPM_DECISIONCONDITIONS DROP CONSTRAINT FK_DECCOND_DEC; -- (optional)
ALTER TABLE JBPM_DELEGATION DROP CONSTRAINT FK_DELEGATION_PRCD; -- (optional)
ALTER TABLE JBPM_EVENT DROP CONSTRAINT FK_EVENT_NODE; -- (optional)
ALTER TABLE JBPM_EVENT DROP CONSTRAINT FK_EVENT_PROCDEF; -- (optional)
ALTER TABLE JBPM_EVENT DROP CONSTRAINT FK_EVENT_TASK; -- (optional)
ALTER TABLE JBPM_EVENT DROP CONSTRAINT FK_EVENT_TRANS; -- (optional)
ALTER TABLE JBPM_JOB DROP CONSTRAINT FK_JOB_ACTION; -- (optional)
ALTER TABLE JBPM_JOB DROP CONSTRAINT FK_JOB_NODE; -- (optional)
ALTER TABLE JBPM_JOB DROP CONSTRAINT FK_JOB_PRINST; -- (optional)
ALTER TABLE JBPM_JOB DROP CONSTRAINT FK_JOB_TOKEN; -- (optional)
ALTER TABLE JBPM_JOB DROP CONSTRAINT FK_JOB_TSKINST; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_ACTION; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_CHILDTOKEN; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_DESTNODE; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_NEWBYTES; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_NODE; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_OLDBYTES; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_PARENT; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_TASKINST; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_TOKEN; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_TRANSITION; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_SOURCENODE; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_SWIMINST; -- (optional)
ALTER TABLE JBPM_LOG DROP CONSTRAINT FK_LOG_VARINST; -- (optional)
ALTER TABLE JBPM_MODULEDEFINITION DROP CONSTRAINT FK_MODDEF_PROCDEF; -- (optional)
ALTER TABLE JBPM_MODULEDEFINITION DROP CONSTRAINT FK_TSKDEF_START; -- (optional)
ALTER TABLE JBPM_MODULEINSTANCE DROP CONSTRAINT FK_MODINST_PRCINST; -- (optional)
ALTER TABLE JBPM_MODULEINSTANCE DROP CONSTRAINT FK_TASKMGTINST_TMD; -- (optional)
ALTER TABLE JBPM_NODE DROP CONSTRAINT FK_NODE_ACTION; -- (optional)
ALTER TABLE JBPM_NODE DROP CONSTRAINT FK_DECISION_DELEG; -- (optional)
ALTER TABLE JBPM_NODE DROP CONSTRAINT FK_NODE_PROCDEF; -- (optional)
ALTER TABLE JBPM_NODE DROP CONSTRAINT FK_NODE_SCRIPT; -- (optional)
ALTER TABLE JBPM_NODE DROP CONSTRAINT FK_NODE_SUPERSTATE; -- (optional)
ALTER TABLE JBPM_NODE DROP CONSTRAINT FK_PROCST_SBPRCDEF; -- (optional)
ALTER TABLE JBPM_POOLEDACTOR DROP CONSTRAINT FK_POOLEDACTOR_SLI; -- (optional)
ALTER TABLE JBPM_PROCESSDEFINITION DROP CONSTRAINT FK_PROCDEF_STRTSTA; -- (optional)
ALTER TABLE JBPM_PROCESSINSTANCE DROP CONSTRAINT FK_PROCIN_PROCDEF; -- (optional)
ALTER TABLE JBPM_PROCESSINSTANCE DROP CONSTRAINT FK_PROCIN_ROOTTKN; -- (optional)
ALTER TABLE JBPM_PROCESSINSTANCE DROP CONSTRAINT FK_PROCIN_SPROCTKN; -- (optional)
ALTER TABLE JBPM_RUNTIMEACTION DROP CONSTRAINT FK_RTACTN_ACTION; -- (optional)
ALTER TABLE JBPM_RUNTIMEACTION DROP CONSTRAINT FK_RTACTN_PROCINST; -- (optional)
ALTER TABLE JBPM_SWIMLANE DROP CONSTRAINT FK_SWL_ASSDEL; -- (optional)
ALTER TABLE JBPM_SWIMLANE DROP CONSTRAINT FK_SWL_TSKMGMTDEF; -- (optional)
ALTER TABLE JBPM_SWIMLANEINSTANCE DROP CONSTRAINT FK_SWIMLANEINST_SL; -- (optional)
ALTER TABLE JBPM_SWIMLANEINSTANCE DROP CONSTRAINT FK_SWIMLANEINST_TM; -- (optional)
ALTER TABLE JBPM_TASK DROP CONSTRAINT FK_TASK_ASSDEL; -- (optional)
ALTER TABLE JBPM_TASK DROP CONSTRAINT FK_TASK_PROCDEF; -- (optional)
ALTER TABLE JBPM_TASK DROP CONSTRAINT FK_TASK_TASKMGTDEF; -- (optional)
ALTER TABLE JBPM_TASK DROP CONSTRAINT FK_TSK_TSKCTRL; -- (optional)
ALTER TABLE JBPM_TASK DROP CONSTRAINT FK_TASK_TASKNODE; -- (optional)
ALTER TABLE JBPM_TASK DROP CONSTRAINT FK_TASK_STARTST; -- (optional)
ALTER TABLE JBPM_TASK DROP CONSTRAINT FK_TASK_SWIMLANE; -- (optional)
ALTER TABLE JBPM_TASKACTORPOOL DROP CONSTRAINT FK_TSKACTPOL_PLACT; -- (optional)
ALTER TABLE JBPM_TASKACTORPOOL DROP CONSTRAINT FK_TASKACTPL_TSKI; -- (optional)
ALTER TABLE JBPM_TASKCONTROLLER DROP CONSTRAINT FK_TSKCTRL_DELEG; -- (optional)
ALTER TABLE JBPM_TASKINSTANCE DROP CONSTRAINT FK_TSKINS_PRCINS; -- (optional)
ALTER TABLE JBPM_TASKINSTANCE DROP CONSTRAINT FK_TASKINST_TASK; -- (optional)
ALTER TABLE JBPM_TASKINSTANCE DROP CONSTRAINT FK_TASKINST_TMINST; -- (optional)
ALTER TABLE JBPM_TASKINSTANCE DROP CONSTRAINT FK_TASKINST_SLINST; -- (optional)
ALTER TABLE JBPM_TASKINSTANCE DROP CONSTRAINT FK_TASKINST_TOKEN; -- (optional)
ALTER TABLE JBPM_TOKEN DROP CONSTRAINT FK_TOKEN_NODE; -- (optional)
ALTER TABLE JBPM_TOKEN DROP CONSTRAINT FK_TOKEN_PARENT; -- (optional)
ALTER TABLE JBPM_TOKEN DROP CONSTRAINT FK_TOKEN_PROCINST; -- (optional)
ALTER TABLE JBPM_TOKEN DROP CONSTRAINT FK_TOKEN_SUBPI; -- (optional)
ALTER TABLE JBPM_TOKENVARIABLEMAP DROP CONSTRAINT FK_TKVARMAP_CTXT; -- (optional)
ALTER TABLE JBPM_TOKENVARIABLEMAP DROP CONSTRAINT FK_TKVARMAP_TOKEN; -- (optional)
ALTER TABLE JBPM_TRANSITION DROP CONSTRAINT FK_TRANSITION_FROM; -- (optional)
ALTER TABLE JBPM_TRANSITION DROP CONSTRAINT FK_TRANS_PROCDEF; -- (optional)
ALTER TABLE JBPM_TRANSITION DROP CONSTRAINT FK_TRANSITION_TO; -- (optional)
ALTER TABLE JBPM_VARIABLEACCESS DROP CONSTRAINT FK_VARACC_PROCST; -- (optional)
ALTER TABLE JBPM_VARIABLEACCESS DROP CONSTRAINT FK_VARACC_TSKCTRL; -- (optional)
ALTER TABLE JBPM_VARIABLEACCESS DROP CONSTRAINT FK_VARACC_SCRIPT; -- (optional)
ALTER TABLE JBPM_VARIABLEINSTANCE DROP CONSTRAINT FK_BYTEINST_ARRAY; -- (optional)
ALTER TABLE JBPM_VARIABLEINSTANCE DROP CONSTRAINT FK_VARINST_PRCINST; -- (optional)
ALTER TABLE JBPM_VARIABLEINSTANCE DROP CONSTRAINT FK_VAR_TSKINST; -- (optional)
ALTER TABLE JBPM_VARIABLEINSTANCE DROP CONSTRAINT FK_VARINST_TK; -- (optional)
ALTER TABLE JBPM_VARIABLEINSTANCE DROP CONSTRAINT FK_VARINST_TKVARMP; -- (optional)


-- Drop all tables

DROP TABLE JBPM_ACTION; -- (optional)
DROP TABLE JBPM_BYTEARRAY; -- (optional)
DROP TABLE JBPM_BYTEBLOCK; -- (optional)
DROP TABLE JBPM_COMMENT; -- (optional)
DROP TABLE JBPM_DECISIONCONDITIONS; -- (optional)
DROP TABLE JBPM_DELEGATION; -- (optional)
DROP TABLE JBPM_EVENT; -- (optional)
DROP TABLE JBPM_EXCEPTIONHANDLER; -- (optional)
DROP TABLE JBPM_JOB; -- (optional)
DROP TABLE JBPM_LOG; -- (optional)
DROP TABLE JBPM_MODULEDEFINITION; -- (optional)
DROP TABLE JBPM_MODULEINSTANCE; -- (optional)
DROP TABLE JBPM_NODE; -- (optional)
DROP TABLE JBPM_POOLEDACTOR; -- (optional)
DROP TABLE JBPM_PROCESSDEFINITION; -- (optional)
DROP TABLE JBPM_PROCESSINSTANCE; -- (optional)
DROP TABLE JBPM_RUNTIMEACTION; -- (optional)
DROP TABLE JBPM_SWIMLANE; -- (optional)
DROP TABLE JBPM_SWIMLANEINSTANCE; -- (optional)
DROP TABLE JBPM_TASK; -- (optional)
DROP TABLE JBPM_TASKACTORPOOL; -- (optional)
DROP TABLE JBPM_TASKCONTROLLER; -- (optional)
DROP TABLE JBPM_TASKINSTANCE; -- (optional)
DROP TABLE JBPM_TOKEN; -- (optional)
DROP TABLE JBPM_TOKENVARIABLEMAP; -- (optional)
DROP TABLE JBPM_TRANSITION; -- (optional)
DROP TABLE JBPM_VARIABLEACCESS; -- (optional)
DROP TABLE JBPM_VARIABLEINSTANCE; -- (optional)


--
-- Record script finish
--
DELETE FROM alf_applied_patch WHERE id = 'patch.db-V5.2-remove-jbpm-tables-from-db';
INSERT INTO alf_applied_patch
  (id, description, fixes_from_schema, fixes_to_schema, applied_to_schema, target_schema, applied_on_date, applied_to_server, was_executed, succeeded, report)
  VALUES
  (
    'patch.db-V5.2-remove-jbpm-tables-from-db', 'Removes all JBPM related tables from the database.',
    0, 10051, -1, 10052, null, 'UNKNOWN', ${TRUE}, ${TRUE}, 'Script completed'
  );