fix(generator): 修复 create_record 必填字段生成流程

This commit is contained in:
王鹏
2026-07-15 17:28:49 +08:00
parent 8247c86862
commit 84d3c8fc05
15 changed files with 515 additions and 52 deletions

View File

@@ -0,0 +1,135 @@
-- Align managed flow/database Prompts with deterministic create_record action inputs.
-- Idempotent: each Prompt is versioned only when its current release lacks the marker.
DELIMITER $$
DROP PROCEDURE IF EXISTS upgrade_create_record_action_input_prompts_20260715$$
CREATE PROCEDURE upgrade_create_record_action_input_prompts_20260715()
BEGIN
DECLARE v_done INT DEFAULT 0;
DECLARE v_template_id BIGINT;
DECLARE v_current_version_id BIGINT;
DECLARE v_new_version_id BIGINT;
DECLARE v_version_no INT;
DECLARE v_version_label VARCHAR(50);
DECLARE v_generate_type VARCHAR(50);
DECLARE v_prompt_code VARCHAR(100);
DECLARE v_system_prompt LONGTEXT;
DECLARE v_user_prompt_contract VARCHAR(255);
DECLARE v_user_prompt_template LONGTEXT;
DECLARE v_target_system_prompt LONGTEXT;
DECLARE v_target_user_prompt LONGTEXT;
DECLARE v_provider_code VARCHAR(50);
DECLARE v_model VARCHAR(100);
DECLARE v_pipeline_release_code VARCHAR(100);
DECLARE v_content_hash CHAR(64);
DECLARE v_marker VARCHAR(100);
DECLARE v_rule LONGTEXT;
DECLARE prompt_cursor CURSOR FOR
SELECT t.prompt_template_id, t.current_version_id, t.generate_type, t.prompt_code,
v.system_prompt, v.user_prompt_contract, v.user_prompt_template,
v.provider_code, v.model, v.pipeline_release_code
FROM factory_prompt_template t
JOIN factory_prompt_version v ON v.prompt_version_id = t.current_version_id
WHERE t.prompt_code IN ('front.flow_config', 'front.database')
ORDER BY t.prompt_template_id;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = 1;
START TRANSACTION;
OPEN prompt_cursor;
prompt_loop: LOOP
FETCH prompt_cursor INTO v_template_id, v_current_version_id, v_generate_type, v_prompt_code,
v_system_prompt, v_user_prompt_contract, v_user_prompt_template,
v_provider_code, v_model, v_pipeline_release_code;
IF v_done = 1 THEN
LEAVE prompt_loop;
END IF;
SET v_target_system_prompt = v_system_prompt;
SET v_target_user_prompt = v_user_prompt_template;
IF v_prompt_code = 'front.flow_config' THEN
SET v_marker = 'CREATE_RECORD_ACTION_INPUT_V1';
SET v_rule = CONCAT(
'13. CREATE_RECORD_ACTION_INPUT_V1: Rule 7 coverage applies to values derivable ',
'from currentUserId, now, ownerTable record fields, or constants before the database ',
'schema is finalized. Meaningful business fields that must be entered when the action ',
'runs may be omitted from values; after schema finalization every omitted non-generated ',
'required column becomes a required typed action input. Never invent record.x unless x ',
'is a real ownerTable field.');
IF LOCATE(v_marker, v_system_prompt) > 0 THEN
ITERATE prompt_loop;
END IF;
SET v_target_system_prompt = CONCAT(v_system_prompt, CHAR(10), v_rule);
ELSE
SET v_marker = 'CREATE_RECORD_NULLABILITY_V1';
SET v_rule = CONCAT(
'CREATE_RECORD_NULLABILITY_V1: Required create_record target columns omitted from ',
'flowConfig values are intentional typed action inputs; keep meaningful columns required ',
'instead of weakening them to nullable. When a create_record value maps record.source_field ',
'into a target column with isRequired=1, the ownerTable source_field must also use ',
'isRequired=1 so the generated insert cannot propagate null.');
IF LOCATE(v_marker, v_user_prompt_template) > 0 THEN
ITERATE prompt_loop;
END IF;
SET v_target_user_prompt = CONCAT(v_user_prompt_template, CHAR(10), v_rule);
END IF;
SELECT COALESCE(MAX(version_no), 0) + 1
INTO v_version_no
FROM factory_prompt_version
WHERE prompt_template_id = v_template_id
FOR UPDATE;
SET v_version_label = CONCAT('V', v_version_no);
SET v_content_hash = SHA2(CONCAT(
'{"generateType":', JSON_QUOTE(v_generate_type),
',"promptCode":', JSON_QUOTE(v_prompt_code),
',"systemPrompt":', JSON_QUOTE(v_target_system_prompt),
',"userPromptContract":', JSON_QUOTE(v_user_prompt_contract),
',"userPromptTemplate":', JSON_QUOTE(v_target_user_prompt),
',"version":', JSON_QUOTE(v_version_label), '}'), 256);
INSERT INTO factory_prompt_version
(prompt_template_id, version_no, version_label, system_prompt,
user_prompt_contract, user_prompt_template, provider_code, model,
content_hash, status, parent_version_id, pipeline_release_code,
published_by, published_at, create_by, create_time, remark)
VALUES
(v_template_id, v_version_no, v_version_label, v_target_system_prompt,
v_user_prompt_contract, v_target_user_prompt, v_provider_code, v_model,
v_content_hash, 'DRAFT', v_current_version_id, v_pipeline_release_code,
'', NULL, 'system-upgrade', NOW(),
'Align create_record required fields with typed action inputs');
SET v_new_version_id = LAST_INSERT_ID();
UPDATE factory_prompt_version
SET status = 'RETIRED'
WHERE prompt_template_id = v_template_id
AND status = 'PUBLISHED'
AND prompt_version_id <> v_new_version_id;
UPDATE factory_prompt_version
SET status = 'PUBLISHED', published_by = 'system-upgrade', published_at = NOW()
WHERE prompt_version_id = v_new_version_id AND status = 'DRAFT';
UPDATE factory_prompt_template
SET current_version_id = v_new_version_id,
update_by = 'system-upgrade', update_time = NOW()
WHERE prompt_template_id = v_template_id;
END LOOP;
CLOSE prompt_cursor;
COMMIT;
END$$
CALL upgrade_create_record_action_input_prompts_20260715()$$
DROP PROCEDURE upgrade_create_record_action_input_prompts_20260715$$
DELIMITER ;