Files
workspace/code/fms/sql/fms_delete_rule.sql
T
2026-09-17 21:29:08 +08:00

47 lines
2.7 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ============================================================================
删除规则一期:s_relation 加删除连带列 + 新建规则表 s_rule
----------------------------------------------------------------------------
背景:见 FMS删除规则引擎设计.md(一期只落配置,后端引擎二期做)。
本脚本可重复执行:
- s_relation.b_on_delete 不存在时才加(存量行回填 'none');
- s_rule 直接重建(drop + create)——表里还没有规则数据时才可执行,
库中已有配置数据时请勿运行,改为手工迁移。
执行:fms-api 目录下
java -cp "tools/migration/mssql-jdbc-13.4.0.jre11.jar" tools/migration/RunSqlFile.java \
../sql/fms_delete_rule.sql --dry-run
确认分批正确后去掉 --dry-run 实跑(按 GO 分批、单事务、失败整体回滚)。
============================================================================ */
if col_length('dbo.s_relation', 'b_on_delete') is null
begin
alter table dbo.s_relation add
b_on_delete varchar(20) not null default 'none'; -- none不连带 / cascade连带删除 / archive连带归档
end
GO
drop table if exists dbo.s_rule;
GO
/* 删除规则表:挂在模块(业务条件)或模块关系(存在引用即拒绝删除)上。
规则类型不单独存列,由 b_predicate 推断:空=引用规则;JSON=勾条件;其余=SQL 文本。
主键用雪花 id(新增时前端取号,与业务表一致):规则没有天然业务键,也不要求 IT 填编码。 */
create table dbo.s_rule (
b_id bigint not null, -- 雪花主键(新增时前端经 /data/nextid 取号)
b_scope_type varchar(20) not null, -- module / relation(挂载范围)
b_scope_id varchar(250) not null, -- module=模块编码;relation=源模块|源字段|目标模块|目标字段
b_hook varchar(30) not null, -- delete.pre(一期只做这个),save.pre 留位
b_predicate nvarchar(max) null, -- 空=引用规则;JSON={"kind":"dsl","expr":…}=勾条件;其余=SQL 文本
b_message nvarchar(500) null, -- 提示文案(写 SQL 必填;勾条件可空,引擎按条件自动生成)
b_canuse tinyint not null default 1, -- 是否启用(0/1)
b_xh int not null default 0, -- 执行顺序(并列按 b_xh、b_id)
b_created_by varchar(50) null, -- 审计列由服务端维护,配置界面不提交
b_created_at datetime2 null,
b_updated_by varchar(50) null,
b_updated_at datetime2 null,
primary key (b_id)
);
GO
create index ix_s_rule_scope
on dbo.s_rule (b_scope_type, b_scope_id, b_hook, b_canuse, b_xh);
GO