fix(aihr): harden legacy schema migrations

This commit is contained in:
2026-07-14 16:13:49 +08:00
parent 638f231d0c
commit 52af9b6e4c
5 changed files with 51 additions and 11 deletions
@@ -1,5 +1,18 @@
-- AIHR 组织快照入职日期迁移(MySQL 8.x)
-- 仅补齐结构,不写入业务数据;可重复执行。
SET @aihr_org_snapshot_table_exists := (
SELECT COUNT(*)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'aihr_org_snapshot'
);
SET @aihr_employment_status_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'aihr_org_snapshot'
AND COLUMN_NAME = 'employment_status'
);
SET @aihr_hire_date_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
@@ -8,8 +21,12 @@ SET @aihr_hire_date_exists := (
AND COLUMN_NAME = 'hire_date'
);
SET @aihr_hire_date_ddl := IF(
@aihr_hire_date_exists = 0,
'ALTER TABLE aihr_org_snapshot ADD COLUMN `hire_date` date DEFAULT NULL COMMENT ''入职日期(用于新员工三个月资格判断)'' AFTER `employment_status`',
@aihr_org_snapshot_table_exists = 1 AND @aihr_hire_date_exists = 0,
IF(
@aihr_employment_status_exists = 1,
'ALTER TABLE aihr_org_snapshot ADD COLUMN `hire_date` date DEFAULT NULL COMMENT ''入职日期(用于新员工三个月资格判断)'' AFTER `employment_status`',
'ALTER TABLE aihr_org_snapshot ADD COLUMN `hire_date` date DEFAULT NULL COMMENT ''入职日期(用于新员工三个月资格判断)'''
),
'SELECT 1'
);
PREPARE aihr_hire_date_stmt FROM @aihr_hire_date_ddl;
@@ -7,6 +7,22 @@ SET @aihr_sop_review_table_exists := (
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'aihr_sop_answer_review'
);
SET @aihr_sop_source_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'aihr_sop_answer_review'
AND COLUMN_NAME = 'source'
);
SET @aihr_sop_source_ddl := IF(
@aihr_sop_review_table_exists = 1 AND @aihr_sop_source_exists = 0,
'ALTER TABLE aihr_sop_answer_review ADD COLUMN `source` varchar(50) DEFAULT ''knowledge_search'' COMMENT ''来源''',
'SELECT 1'
);
PREPARE aihr_sop_source_stmt FROM @aihr_sop_source_ddl;
EXECUTE aihr_sop_source_stmt;
DEALLOCATE PREPARE aihr_sop_source_stmt;
SET @aihr_sop_requester_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
@@ -16,7 +32,7 @@ SET @aihr_sop_requester_exists := (
);
SET @aihr_sop_requester_ddl := IF(
@aihr_sop_review_table_exists = 1 AND @aihr_sop_requester_exists = 0,
'ALTER TABLE aihr_sop_answer_review ADD COLUMN `requester_ext_party_id` varchar(100) DEFAULT NULL COMMENT ''提问员工外部主体ID'' AFTER `source`',
'ALTER TABLE aihr_sop_answer_review ADD COLUMN `requester_ext_party_id` varchar(100) DEFAULT NULL COMMENT ''提问员工外部主体ID''',
'SELECT 1'
);
PREPARE aihr_sop_requester_stmt FROM @aihr_sop_requester_ddl;