Activiti5.22:刪除工做流引擎自動建立的外鍵約束

Activiti 工做流引擎自帶建庫腳本,同時生成了索引、外鍵等信息。爲了下降外鍵約束對數據庫形成的性能影響,咱們須要將外鍵刪除,下文描述刪除外鍵的工程。注:在互聯網系統設計中儘可能保持數據庫表的原子性設計。mysql

1、查找建立外鍵的 sql 語句

在 Activiti 中databaseSchemaUpdate屬性設置建表策略,值爲 true 是,若是沒有表,自動建立表,不然不自動建立表。sql

在 IntelliJ IDEA 中經過 find usages命令發現,在org.activiti.engine.impl.db.DbSqlSession類中使用了該屬性,代碼以下:數據庫

public void performSchemaOperationsProcessEngineBuild() {
    String databaseSchemaUpdate = Context.getProcessEngineConfiguration().getDatabaseSchemaUpdate();
    if (ProcessEngineConfigurationImpl.DB_SCHEMA_UPDATE_DROP_CREATE.equals(databaseSchemaUpdate)) {
        try {
            dbSchemaDrop();
        } catch (RuntimeException e) {
            // ignore
        }
    }
    if (org.activiti.engine.ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP.equals(databaseSchemaUpdate)
            || ProcessEngineConfigurationImpl.DB_SCHEMA_UPDATE_DROP_CREATE.equals(databaseSchemaUpdate)
            || ProcessEngineConfigurationImpl.DB_SCHEMA_UPDATE_CREATE.equals(databaseSchemaUpdate)
            ) {
        dbSchemaCreate();

    } else if (org.activiti.engine.ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE.equals(databaseSchemaUpdate)) {
        dbSchemaCheckVersion();

    } else if (ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE.equals(databaseSchemaUpdate)) {
        dbSchemaUpdate();
    }
}

在上面代碼中,咱們找到了dbSchemaCreate();方法,該方法負責建立數據庫表,代碼以下:app

public void dbSchemaCreate() {
    if (isEngineTablePresent()) {
        String dbVersion = getDbVersion();
        if (!ProcessEngine.VERSION.equals(dbVersion)) {
            throw new ActivitiWrongDbException(ProcessEngine.VERSION, dbVersion);
        }
    } else {
        dbSchemaCreateEngine();
    }

    if (dbSqlSessionFactory.isDbHistoryUsed()) {
        dbSchemaCreateHistory();
    }

    if (dbSqlSessionFactory.isDbIdentityUsed()) {
        dbSchemaCreateIdentity();
    }
}

經過上面代碼能夠發現,engine表、history表、identity 表三部份內容是分別進行建立的。接着咱們跟蹤 dbSchemaCreateEngine()方法,代碼入下:框架

protected void dbSchemaCreateEngine() {
    executeMandatorySchemaResource("create", "engine");
}

該方法調用executeMandatorySchemaResource()方法,代碼以下:ide

public void executeMandatorySchemaResource(String operation, String component) {
    executeSchemaResource(operation, component, getResourceForDbOperation(operation, operation, component), false);
  }

該方法調用getResourceForDbOperation()方法,此方法用來查找sql文件的位置,代碼以下:性能

public String getResourceForDbOperation(String directory, String operation, String component) {
    String databaseType = dbSqlSessionFactory.getDatabaseType();
    return "org/activiti/db/" + directory + "/activiti." + databaseType + "." + operation + "." + component + ".sql";
}

前面傳入的參數值:operation="create", component="engine", directory="create",所以該方法的返回值爲org/activiti/db/create/activiti.mysql.create.engine.sql(以 Mysql數據庫爲例),該sql文件存放在 activiti-engine-5.22.0.jar 中。ui

爲了便於理解,咱們分析一下org/activiti/db/中的內容,以下圖所示: 
這裏寫圖片描述 
這裏寫圖片描述 
這裏寫圖片描述spa

簡要說明: 
1. 在 org.activiti.db 包中存放了與數據庫相關的內容。 
2. create目錄:存放建立數據庫表的 sql 語句。 
3. drop目錄:存放刪除數據庫表的 sql 語句。 
4. upgrade目錄:存放升級數據庫表的 sql 語句,好比將Activiti 從 5.10版本升級至 5.22版本,經過此目錄下的 sql 文件進行數據庫升級。 
5. mapping目錄:存放 Mybatis 框架使用的 Mapper配置文件。設計

經過查看如下三個文件來查找建立外鍵的 sql 語句。

  • org/activiti/db/create/activiti.mysql.create.engine.sql
  • org/activiti/db/create/activiti.mysql.create.history.sql
  • org/activiti/db/create/activiti.mysql.create.identity.sql

engine 表相關

alter table ACT_GE_BYTEARRAY
    add constraint ACT_FK_BYTEARR_DEPL 
    foreign key (DEPLOYMENT_ID_) 
    references ACT_RE_DEPLOYMENT (ID_);

alter table ACT_RE_PROCDEF
    add constraint ACT_UNIQ_PROCDEF
    unique (KEY_,VERSION_, TENANT_ID_); #惟一索引,無需刪除

alter table ACT_RU_EXECUTION
    add constraint ACT_FK_EXE_PROCINST 
    foreign key (PROC_INST_ID_) 
    references ACT_RU_EXECUTION (ID_) on delete cascade on update cascade;

alter table ACT_RU_EXECUTION
    add constraint ACT_FK_EXE_PARENT 
    foreign key (PARENT_ID_) 
    references ACT_RU_EXECUTION (ID_);

alter table ACT_RU_EXECUTION
    add constraint ACT_FK_EXE_SUPER 
    foreign key (SUPER_EXEC_) 
    references ACT_RU_EXECUTION (ID_);

alter table ACT_RU_EXECUTION
    add constraint ACT_FK_EXE_PROCDEF 
    foreign key (PROC_DEF_ID_) 
    references ACT_RE_PROCDEF (ID_);

alter table ACT_RU_IDENTITYLINK
    add constraint ACT_FK_TSKASS_TASK 
    foreign key (TASK_ID_) 
    references ACT_RU_TASK (ID_);

alter table ACT_RU_IDENTITYLINK
    add constraint ACT_FK_ATHRZ_PROCEDEF 
    foreign key (PROC_DEF_ID_) 
    references ACT_RE_PROCDEF(ID_);

alter table ACT_RU_IDENTITYLINK
    add constraint ACT_FK_IDL_PROCINST
    foreign key (PROC_INST_ID_) 
    references ACT_RU_EXECUTION (ID_);       

alter table ACT_RU_TASK
    add constraint ACT_FK_TASK_EXE
    foreign key (EXECUTION_ID_)
    references ACT_RU_EXECUTION (ID_);

alter table ACT_RU_TASK
    add constraint ACT_FK_TASK_PROCINST
    foreign key (PROC_INST_ID_)
    references ACT_RU_EXECUTION (ID_);

alter table ACT_RU_TASK
    add constraint ACT_FK_TASK_PROCDEF
    foreign key (PROC_DEF_ID_)
    references ACT_RE_PROCDEF (ID_);

alter table ACT_RU_VARIABLE 
    add constraint ACT_FK_VAR_EXE 
    foreign key (EXECUTION_ID_) 
    references ACT_RU_EXECUTION (ID_);

alter table ACT_RU_VARIABLE
    add constraint ACT_FK_VAR_PROCINST
    foreign key (PROC_INST_ID_)
    references ACT_RU_EXECUTION(ID_);

alter table ACT_RU_VARIABLE 
    add constraint ACT_FK_VAR_BYTEARRAY 
    foreign key (BYTEARRAY_ID_) 
    references ACT_GE_BYTEARRAY (ID_);

alter table ACT_RU_JOB 
    add constraint ACT_FK_JOB_EXCEPTION 
    foreign key (EXCEPTION_STACK_ID_) 
    references ACT_GE_BYTEARRAY (ID_);

alter table ACT_RU_EVENT_SUBSCR
    add constraint ACT_FK_EVENT_EXEC
    foreign key (EXECUTION_ID_)
    references ACT_RU_EXECUTION(ID_);

alter table ACT_RE_MODEL 
    add constraint ACT_FK_MODEL_SOURCE 
    foreign key (EDITOR_SOURCE_VALUE_ID_) 
    references ACT_GE_BYTEARRAY (ID_);

alter table ACT_RE_MODEL 
    add constraint ACT_FK_MODEL_SOURCE_EXTRA 
    foreign key (EDITOR_SOURCE_EXTRA_VALUE_ID_) 
    references ACT_GE_BYTEARRAY (ID_);

alter table ACT_RE_MODEL 
    add constraint ACT_FK_MODEL_DEPLOYMENT 
    foreign key (DEPLOYMENT_ID_) 
    references ACT_RE_DEPLOYMENT (ID_);        

alter table ACT_PROCDEF_INFO 
    add constraint ACT_FK_INFO_JSON_BA 
    foreign key (INFO_JSON_ID_) 
    references ACT_GE_BYTEARRAY (ID_);

alter table ACT_PROCDEF_INFO 
    add constraint ACT_FK_INFO_PROCDEF 
    foreign key (PROC_DEF_ID_) 
    references ACT_RE_PROCDEF (ID_);

alter table ACT_PROCDEF_INFO
    add constraint ACT_UNIQ_INFO_PROCDEF
    unique (PROC_DEF_ID_); #惟一索引,無需刪除

history 表相關

沒有建立外鍵。

identity 表相關

alter table ACT_ID_MEMBERSHIP 
    add constraint ACT_FK_MEMB_GROUP 
    foreign key (GROUP_ID_) 
    references ACT_ID_GROUP (ID_);

alter table ACT_ID_MEMBERSHIP 
    add constraint ACT_FK_MEMB_USER 
    foreign key (USER_ID_) 
    references ACT_ID_USER (ID_);

咱們找到了建立外鍵的 sql 語句,接下來建立刪除外鍵的 sql 語句。

2、建立刪除外鍵的 sql 語句

engine 表相關

alter table ACT_GE_BYTEARRAY drop foreign key ACT_FK_BYTEARR_DEPL;
alter table ACT_RE_PROCDEF drop index ACT_UNIQ_PROCDEF;  
alter table ACT_RU_EXECUTION drop foreign key ACT_FK_EXE_PROCINST;
alter table ACT_RU_EXECUTION drop foreign key ACT_FK_EXE_PARENT;
alter table ACT_RU_EXECUTION drop foreign key ACT_FK_EXE_SUPER;
alter table ACT_RU_EXECUTION drop foreign key ACT_FK_EXE_PROCDEF;
alter table ACT_RU_IDENTITYLINK drop foreign key ACT_FK_TSKASS_TASK;
alter table ACT_RU_IDENTITYLINK drop foreign key ACT_FK_ATHRZ_PROCEDEF;
alter table ACT_RU_IDENTITYLINK drop foreign key ACT_FK_IDL_PROCINST;
alter table ACT_RU_TASK drop foreign key ACT_FK_TASK_EXE;
alter table ACT_RU_TASK drop foreign key ACT_FK_TASK_PROCINST;
alter table ACT_RU_TASK drop foreign key ACT_FK_TASK_PROCDEF;
alter table ACT_RU_VARIABLE drop foreign key ACT_FK_VAR_EXE;
alter table ACT_RU_VARIABLE drop foreign key ACT_FK_VAR_PROCINST;
alter table ACT_RU_VARIABLE drop foreign key ACT_FK_VAR_BYTEARRAY;   
alter table ACT_RU_JOB drop foreign key ACT_FK_JOB_EXCEPTION;      
alter table ACT_RU_EVENT_SUBSCR drop foreign key ACT_FK_EVENT_EXEC;      
alter table ACT_RE_MODEL drop foreign key ACT_FK_MODEL_SOURCE;      
alter table ACT_RE_MODEL drop foreign key ACT_FK_MODEL_SOURCE_EXTRA;      
alter table ACT_RE_MODEL drop foreign key ACT_FK_MODEL_DEPLOYMENT;      
alter table ACT_PROCDEF_INFO drop foreign key ACT_FK_INFO_JSON_BA;      
alter table ACT_PROCDEF_INFO drop foreign key ACT_FK_INFO_PROCDEF;      
alter table ACT_PROCDEF_INFO drop index ACT_UNIQ_INFO_PROCDEF;

history 表相關

該部分沒有建立外鍵,所以無需刪除外鍵。

identity 表相關

alter table ACT_ID_MEMBERSHIP drop foreign key ACT_FK_MEMB_GROUP;  
alter table ACT_ID_MEMBERSHIP drop foreign key ACT_FK_MEMB_USER;

刪除外鍵的 sql 語句建立完畢,咱們能夠經過執行上面的 sql 語句來刪除外鍵。固然也能夠在程序中調用 sql 腳原本刪除外鍵。

相關文章
相關標籤/搜索