20260919211702
This commit is contained in:
1 parent
f56e70b050
commit
f4bfc1ddfc
97 files changed
+6059
-1712
No files matched your search
@@ -1,47 +1,100 @@
|
||||
/* ============================================================================
|
||||
删除规则一期:s_relation 加删除连带列 + 新建规则表 s_rule
|
||||
删除策略重构:s_relation 删除处置三值 + 新建删除 SQL 规则表 s_delete_rule
|
||||
----------------------------------------------------------------------------
|
||||
背景:见 FMS删除规则引擎设计.md(一期只落配置,后端引擎二期做)。
|
||||
依据:FMS删除策略重构设计.md v1.2
|
||||
第一阶段元数据脚本,只改结构,不含初始化数据。
|
||||
本脚本可重复执行:
|
||||
- s_relation.b_on_delete 不存在时才加(存量行回填 'none');
|
||||
- s_rule 直接重建(drop + create)——表里还没有规则数据时才可执行,
|
||||
库中已有配置数据时请勿运行,改为手工迁移。
|
||||
- s_relation.b_on_delete 不存在时才加,且默认值取 'restrict';
|
||||
- 存量关系的 b_on_delete 不批量回填(设计 §3.1:避免上线后突然改变存量删除行为);
|
||||
仅把历史遗留的 'archive' 归一到 'none'(阶段一不支持归档,避免执行器遇到未知值);
|
||||
- s_delete_rule 不存在才建;旧的 s_rule 已废弃,按需手工 drop(见文件末尾说明)。
|
||||
执行: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 分批、单事务、失败整体回滚)。
|
||||
============================================================================ */
|
||||
|
||||
/* 1. b_on_delete:补列并设定新语义的默认值。
|
||||
新值域 restrict / cascade / none;默认 restrict 只作用于新建行,
|
||||
存量行保持原值(历史为 'none'),由单独迁移决定是否升级。 */
|
||||
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连带归档
|
||||
b_on_delete varchar(20) not null
|
||||
constraint df_s_relation_on_delete default 'restrict';
|
||||
end
|
||||
GO
|
||||
|
||||
drop table if exists dbo.s_rule;
|
||||
/* 2. 列已存在时,只改默认值约束,不动存量数据。 */
|
||||
if col_length('dbo.s_relation', 'b_on_delete') is not null
|
||||
begin
|
||||
declare @constraint sysname;
|
||||
select @constraint = dc.name
|
||||
from sys.default_constraints dc
|
||||
join sys.columns c
|
||||
on c.object_id = dc.parent_object_id
|
||||
and c.column_id = dc.parent_column_id
|
||||
where dc.parent_object_id = object_id('dbo.s_relation')
|
||||
and c.name = 'b_on_delete';
|
||||
|
||||
if @constraint is not null
|
||||
begin
|
||||
declare @dropSql nvarchar(400) =
|
||||
N'alter table dbo.s_relation drop constraint ' + quotename(@constraint);
|
||||
exec sp_executesql @dropSql;
|
||||
end
|
||||
|
||||
alter table dbo.s_relation add
|
||||
constraint df_s_relation_on_delete default 'restrict' for b_on_delete;
|
||||
end
|
||||
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)
|
||||
);
|
||||
/* 3. 历史 'archive' 阶段一不支持:归一为 'none'(不处理、不保护),
|
||||
避免删除执行器读到未知处置值。空值与非法值同样归一,保证值域收敛。 */
|
||||
update dbo.s_relation
|
||||
set b_on_delete = 'none'
|
||||
where b_on_delete is null
|
||||
or b_on_delete not in ('restrict', 'cascade', 'none');
|
||||
GO
|
||||
|
||||
create index ix_s_rule_scope
|
||||
on dbo.s_rule (b_scope_type, b_scope_id, b_hook, b_canuse, b_xh);
|
||||
GO
|
||||
/* 4. 删除 SQL 规则表。 */
|
||||
if object_id('dbo.s_delete_rule', 'U') is null
|
||||
begin
|
||||
create table dbo.s_delete_rule (
|
||||
b_id bigint not null, -- 雪花主键(新增时前端经 /data/nextid 取号)
|
||||
b_module_id varchar(50) not null, -- 规则所属数据模块编码(对应 s_module.b_id)
|
||||
b_sql nvarchar(max) not null, -- 只读查询 SQL,用 :ids 标记本次待删 ID 集合
|
||||
b_message nvarchar(500) null, -- 删除失败时的提示文案(第一阶段固定文案)
|
||||
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)
|
||||
);
|
||||
end
|
||||
GO
|
||||
|
||||
if not exists (
|
||||
select 1 from sys.indexes
|
||||
where name = 'ix_s_delete_rule_module'
|
||||
and object_id = object_id('dbo.s_delete_rule')
|
||||
)
|
||||
begin
|
||||
create index ix_s_delete_rule_module
|
||||
on dbo.s_delete_rule (b_module_id, b_canuse, b_xh);
|
||||
end
|
||||
GO
|
||||
|
||||
/* 5. 旧表 s_rule 的处置。
|
||||
已确认库内 s_rule 无配置数据,可直接重建;此处刻意不 drop,
|
||||
避免脚本误伤可能存在的历史配置。确认无数据后手工执行:
|
||||
|
||||
drop table if exists dbo.s_rule;
|
||||
|
||||
若库中确实存在规则数据,需先迁移:
|
||||
- b_scope_type='module' 且 b_predicate 为 SQL 文本 → 写入 s_delete_rule
|
||||
(b_module_id = b_scope_id,b_sql = b_predicate);
|
||||
- b_scope_type='relation'(引用规则)→ 置该关系 s_relation.b_on_delete='restrict';
|
||||
- b_predicate 为 JSON 勾条件的数据无法自动转换,需人工改写为等价 SQL。 */
|
||||
Reference in new issue
Block a user