ai_images/web-api/sql/dept-ark-config-migrate.sql

40 lines
1.5 KiB
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.

-- =============================================================================
-- 部门火山配置迁表ai_dept_ark_config 唯一约束、数据搬迁、sys_dept 删除三列
-- 部署顺序:在已有 ai_dept_ark_config 与 sys_dept 火山列的前提下执行;
-- 可重复执行 INSERT 段NOT EXISTS 跳过已搬迁行)。
-- =============================================================================
-- 1) 一部门一行(若已存在同名索引请跳过本步)
ALTER TABLE `ai_dept_ark_config`
ADD UNIQUE INDEX `uk_dept_id` (`dept_id`);
-- 2) 从 sys_dept 搬迁至 ai_dept_ark_config仅一级、二级部门且三字段至少其一非空
INSERT INTO `ai_dept_ark_config` (`dept_id`, `model_parm`, `project`, `byte_api_key`, `create_time`)
SELECT
d.`dept_id`,
d.`model_parm`,
d.`project`,
d.`byte_api_key`,
NOW()
FROM `sys_dept` d
WHERE d.`del_flag` = '0'
AND d.`ancestors` IS NOT NULL
AND (
(d.`parent_id` = 0 AND d.`ancestors` = '0')
OR (CHAR_LENGTH(d.`ancestors`) - CHAR_LENGTH(REPLACE(d.`ancestors`, ',', ''))) = 1
)
AND (
NULLIF(TRIM(d.`byte_api_key`), '') IS NOT NULL
OR NULLIF(TRIM(d.`project`), '') IS NOT NULL
OR NULLIF(TRIM(d.`model_parm`), '') IS NOT NULL
)
AND NOT EXISTS (
SELECT 1 FROM `ai_dept_ark_config` a WHERE a.`dept_id` = d.`dept_id`
);
-- 3) 删除 sys_dept 上已迁移的火山列(搬迁完成后再执行)
ALTER TABLE `sys_dept`
DROP COLUMN `byte_api_key`,
DROP COLUMN `project`,
DROP COLUMN `model_parm`;