Activiti5.22:删除工作流引擎自动创建的外键约束

来源:互联网 发布:linux php7环境搭建 编辑:程序博客网 时间:2024/06/14 19:59

Activiti 工作流引擎自带建库脚本,同时生成了索引、外键等信息。为了降低外键约束对数据库造成的性能影响,我们需要将外键删除,下文描述删除外键的工程。注:在互联网系统设计中尽量保持数据库表的原子性设计。

一、查找创建外键的 sql 语句

在 Activiti 中databaseSchemaUpdate属性设置建表策略,值为 true 是,如果没有表,自动创建表,否则不自动创建表。

在 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();方法,该方法负责创建数据库表,代码如下:

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()方法,代码如下:

  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 中。

为了便于理解,我们分析一下org/activiti/db/中的内容,如下图所示:
这里写图片描述
这里写图片描述
这里写图片描述

简要说明:
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 语句。

二、创建删除外键的 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 脚本来删除外键。

原创粉丝点击