CMVR-IOT/sql/workflow_release_version.sql
lixiaolong 6e01f6888b feat(workflow): 实现工作流版本管理功能
- 将检测项流程数据存储分离为草稿和发布版本两个概念
- 添加 TeDetectionItemVersion 实体用于存储不可变的工作流发布版本
- 实现工作流草稿保存、版本发布、版本回退等功能
- 在任务执行时验证检测项是否已发布,防止使用未发布流程
- 添加 TeTaskInstFlowVersion 表记录任务实例关联的具体版本清单
- 重构 TeFlowController 控制器接口,支持草稿保存和版本管理操作
- 更新数据库表结构,添加版本控制相关字段和约束
2026-08-20 14:04:21 +08:00

73 lines
3.2 KiB
SQL

-- Drafts remain in te_detection_item. Published versions are immutable snapshots.
alter table te_detection_item
add column published_version_id varchar(32) null comment 'Current published workflow version' after flow_data,
add column published_version_no int null comment 'Current published version number' after published_version_id,
add column draft_revision int not null default 0 comment 'Optimistic draft revision' after published_version_no,
add column draft_hash varchar(64) null comment 'Draft SHA-256' after draft_revision,
add column published_draft_hash varchar(64) null comment 'Last published draft SHA-256' after draft_hash,
add index idx_detection_item_published_version (published_version_id);
create table te_detection_item_version (
id varchar(32) not null,
detection_item_id varchar(32) not null,
version_no int not null,
flow_data longtext not null,
config longtext null,
schema_version int not null default 1,
content_hash varchar(64) not null,
source_version_id varchar(32) null,
publish_note varchar(500) null,
create_by varchar(64) null,
create_time datetime null,
update_by varchar(64) null,
update_time datetime null,
remark varchar(500) null,
primary key (id),
unique key uk_detection_item_version (detection_item_id, version_no),
key idx_detection_item_version_item (detection_item_id),
key idx_detection_item_version_source (source_version_id)
) comment='Immutable workflow release history';
-- Every existing non-empty workflow becomes V1. Older releases did not reliably
-- maintain is_deploy, while formal Aima/inspection tasks already executed these definitions.
insert into te_detection_item_version
(id, detection_item_id, version_no, flow_data, config, schema_version,
content_hash, publish_note, create_by, create_time)
select replace(uuid(), '-', ''), id, 1, flow_data, config, 1,
sha2(concat(ifnull(flow_data, ''), '\n', ifnull(config, '')), 256),
'历史流程迁移', create_by, ifnull(update_time, create_time)
from te_detection_item
where flow_data is not null and flow_data <> '';
update te_detection_item item
join te_detection_item_version version
on version.detection_item_id = item.id and version.version_no = 1
set item.published_version_id = version.id,
item.published_version_no = 1,
item.draft_hash = version.content_hash,
item.published_draft_hash = version.content_hash,
item.is_deploy = '0';
update te_detection_item
set draft_hash = sha2(concat(ifnull(flow_data, ''), '\n', ifnull(config, '')), 256)
where draft_hash is null and flow_data is not null;
create table te_task_inst_flow_version (
id varchar(32) not null,
inst_id varchar(32) not null,
item_id varchar(32) not null,
item_order int not null,
item_occurrence int not null,
version_id varchar(32) not null,
version_no int not null,
create_by varchar(64) null,
create_time datetime null,
update_by varchar(64) null,
update_time datetime null,
remark varchar(500) null,
primary key (id),
unique key uk_task_inst_flow_item (inst_id, item_order),
key idx_task_inst_flow_inst (inst_id),
key idx_task_inst_flow_version (version_id)
) comment='Workflow release manifest frozen for a task instance';