V20261118_01__ai_visible_scope.sql 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. -- ============================================================================
  2. -- ops 迁移版本:V20261118_01
  3. -- 源脚本:sql/ai_visible_scope_20261118.sql
  4. -- 说明:由 deploy/ops/db.sh 按版本顺序执行;已执行过的文件请勿再修改。
  5. -- ============================================================================
  6. -- ============================================================
  7. -- AI 记录级数据权限:可见范围(visible_scope)+ 归属部门(owner_dept_id)
  8. --
  9. -- 适用表:ai_entry_agent / ai_expert_agent / ai_skill / ai_scenario_pack / ai_knowledge_base
  10. -- 执行方式:手工在目标库执行(项目无 Flyway/Liquibase),脚本幂等,可重复执行。
  11. --
  12. -- 存量语义保持:
  13. -- * 入口智能体为全局单例,空值统一按「全员」处理;
  14. -- * 其余表原口径为「内置 or 创建人本人」,故内置数据回填 '0'(全员/公开),
  15. -- 非内置数据回填 '2'(仅创建人/本人),避免存量私有数据被意外公开。
  16. -- * 已存在旧 visible_scope 值(如从 bizbuddy 迁移的库)时保留原值,不覆盖。
  17. -- ============================================================
  18. -- ---------- 1. 加列(先不加默认值,便于区分存量空值) ----------
  19. alter table ai_entry_agent add column if not exists owner_dept_id bigint;
  20. alter table ai_entry_agent add column if not exists visible_scope char(1);
  21. alter table ai_expert_agent add column if not exists owner_dept_id bigint;
  22. alter table ai_expert_agent add column if not exists visible_scope char(1);
  23. alter table ai_skill add column if not exists owner_dept_id bigint;
  24. alter table ai_skill add column if not exists visible_scope char(1);
  25. alter table ai_scenario_pack add column if not exists owner_dept_id bigint;
  26. alter table ai_scenario_pack add column if not exists visible_scope char(1);
  27. alter table ai_knowledge_base add column if not exists owner_dept_id bigint;
  28. alter table ai_knowledge_base add column if not exists visible_scope char(1);
  29. -- ---------- 2. 存量数据回填 ----------
  30. -- 入口智能体:全局单例,空值按全员处理
  31. update ai_entry_agent
  32. set visible_scope = '0'
  33. where visible_scope is null or visible_scope = '';
  34. -- 其余四类:内置 -> 全员/公开;非内置 -> 仅创建人/本人
  35. update ai_expert_agent
  36. set visible_scope = case when is_builtin = '1' then '0' else '2' end
  37. where visible_scope is null or visible_scope = '';
  38. update ai_skill
  39. set visible_scope = case when is_builtin = '1' then '0' else '2' end
  40. where visible_scope is null or visible_scope = '';
  41. update ai_scenario_pack
  42. set visible_scope = case when is_builtin = '1' then '0' else '2' end
  43. where visible_scope is null or visible_scope = '';
  44. update ai_knowledge_base
  45. set visible_scope = case when is_builtin = '1' then '0' else '2' end
  46. where visible_scope is null or visible_scope = '';
  47. -- 旧数据已指定「本部门」但归属部门为空:按创建人部门补齐
  48. update ai_entry_agent a
  49. set owner_dept_id = u.dept_id
  50. from perm_user u
  51. where a.owner_dept_id is null
  52. and a.visible_scope = '1'
  53. and a.create_by = u.user_id
  54. and u.dept_id is not null;
  55. update ai_expert_agent a
  56. set owner_dept_id = u.dept_id
  57. from perm_user u
  58. where a.owner_dept_id is null
  59. and a.visible_scope = '1'
  60. and a.create_by = u.user_id
  61. and u.dept_id is not null;
  62. update ai_skill a
  63. set owner_dept_id = u.dept_id
  64. from perm_user u
  65. where a.owner_dept_id is null
  66. and a.visible_scope = '1'
  67. and a.create_by = u.user_id
  68. and u.dept_id is not null;
  69. update ai_scenario_pack a
  70. set owner_dept_id = u.dept_id
  71. from perm_user u
  72. where a.owner_dept_id is null
  73. and a.visible_scope = '1'
  74. and a.create_by = u.user_id
  75. and u.dept_id is not null;
  76. update ai_knowledge_base a
  77. set owner_dept_id = u.dept_id
  78. from perm_user u
  79. where a.owner_dept_id is null
  80. and a.visible_scope = '1'
  81. and a.create_by = u.user_id
  82. and u.dept_id is not null;
  83. -- ---------- 3. 新数据默认值 ----------
  84. alter table ai_entry_agent alter column visible_scope set default '0';
  85. alter table ai_expert_agent alter column visible_scope set default '0';
  86. alter table ai_skill alter column visible_scope set default '0';
  87. alter table ai_scenario_pack alter column visible_scope set default '0';
  88. alter table ai_knowledge_base alter column visible_scope set default '0';
  89. -- ---------- 4. 列注释 ----------
  90. comment on column ai_entry_agent.owner_dept_id is '归属部门(可见范围=本部门时生效)';
  91. comment on column ai_entry_agent.visible_scope is '可见范围(0全员 1本部门 2仅创建人)';
  92. comment on column ai_expert_agent.owner_dept_id is '归属部门(可见范围=本部门时生效)';
  93. comment on column ai_expert_agent.visible_scope is '可见范围(0全员 1本部门 2仅创建人)';
  94. comment on column ai_skill.owner_dept_id is '归属部门(生效范围=本部门时生效)';
  95. comment on column ai_skill.visible_scope is '生效范围(0公开 1本部门 2仅本人)';
  96. comment on column ai_scenario_pack.owner_dept_id is '归属部门(生效范围=本部门时生效)';
  97. comment on column ai_scenario_pack.visible_scope is '生效范围(0公开 1本部门 2仅本人)';
  98. comment on column ai_knowledge_base.owner_dept_id is '归属部门(生效范围=本部门时生效)';
  99. comment on column ai_knowledge_base.visible_scope is '生效范围(0公开 1本部门 2仅本人)';