Files
Neo-ZQYY/db/zqyy_app/migrations/20260501__runtime_context_sandbox.sql
Neo caf179a5da feat: 2026-04-15~05-02 累积变更基线 — AI 重构 + Runtime Context + DWS 修复
涵盖(每条对应已存的审计记录):
- AI 模块拆分:apps/backend/app/ai/apps -> prompts/(8 个 APP + app2a 派生)
  audit: 2026-04-20__ai-module-complete.md
- admin-web AI 管理套件:AIDashboard / AIOperations / AIRunLogs / AITriggers / TriggerManager
  audit: 2026-04-21__admin-web-ai-management-suite.md
- App2 财务洞察 prompt v3 -> v5.1 + 小程序 AI 接入(chat / board-finance)
  audit: 2026-04-22__app2_prompt_v5_1_and_miniprogram_ai_insight.md
- App2 prewarm 全过滤器 + AI 触发器 cron reschedule
  audit: 2026-04-21__app2-finance-prewarm-all-filters.md
  migration: 20260420_ai_trigger_jobs_and_app2_prewarm.sql / 20260421_app2_prewarm_cron_reschedule.sql
- AppType 联合类型对齐 + adminAiAppTypes.test.ts
  audit: 2026-04-30__admin_web_ai_app_type_alignment.md
- DashScope tokens_used 提取修复
  audit: 2026-04-30__backend_dashscope_tokens_used_extraction.md
- App3 线索完整详情 prompt
  audit: 2026-05-01__backend_app3_full_detail_prompt.md
- Runtime Context 沙箱(5-1~5-2 主线):
  - 后端 schema/service + admin_runtime_context / xcx_runtime_clock 两个 router
  - admin-web RuntimeContext.tsx + miniprogram runtime-clock.ts
  - migration: 20260501__runtime_context_sandbox.sql
  - tools/db/verify_admin_web_sandbox.py + verify_sandbox_end_to_end.py
  - database/changes: 7 份 sandbox_* 验证报告
- 飞球 DWS 修复:finance_area_daily 区域汇总 + task_engine 调整
  + RLS 视图业务日上界(migration 20260502 + scripts/ops/gen_rls_business_date_migration.py)

合规:
- .gitignore 启用 tmp/ 排除
- 不入仓:apps/etl/connectors/feiqiu/.env(API_TOKEN secret,本地修改保留)

待验证清单:
- docs/audit/changes/2026-05-04__cumulative_baseline_pending_verification.md
  每个主题的功能完整性 / 上线验证几乎都未收口,按优先级 P0~P3 逐一处理
2026-05-04 02:30:19 +08:00

118 lines
6.6 KiB
PL/PgSQL
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.
-- 2026-05-01
-- 业务运行上下文与沙箱隔离。
BEGIN;
CREATE TABLE IF NOT EXISTS biz.site_runtime_context (
site_id bigint PRIMARY KEY,
mode character varying(20) NOT NULL DEFAULT 'live',
sandbox_date date,
sandbox_instance_id character varying(64),
ai_mode character varying(20) NOT NULL DEFAULT 'live',
status character varying(20) NOT NULL DEFAULT 'active',
reason text,
updated_by bigint,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT site_runtime_context_site_id_fkey
FOREIGN KEY (site_id) REFERENCES biz.sites(site_id),
CONSTRAINT site_runtime_context_mode_check
CHECK (mode IN ('live', 'sandbox')),
CONSTRAINT site_runtime_context_ai_mode_check
CHECK (ai_mode IN ('live')),
CONSTRAINT site_runtime_context_sandbox_check
CHECK (
(mode = 'live' AND sandbox_date IS NULL AND sandbox_instance_id IS NULL)
OR
(mode = 'sandbox' AND sandbox_date IS NOT NULL AND sandbox_instance_id IS NOT NULL)
)
);
COMMENT ON TABLE biz.site_runtime_context IS '门店业务运行上下文live 使用真实日期sandbox 使用指定业务日期并按实例隔离写入。';
COMMENT ON COLUMN biz.site_runtime_context.mode IS '运行模式live / sandbox。';
COMMENT ON COLUMN biz.site_runtime_context.sandbox_date IS 'sandbox 模式下系统假设的业务日期。';
COMMENT ON COLUMN biz.site_runtime_context.sandbox_instance_id IS 'sandbox 模式写入隔离实例 ID。';
COMMENT ON COLUMN biz.site_runtime_context.ai_mode IS 'AI 调用模式;当前固定 live沙箱也真实调用 DashScope。';
ALTER TABLE biz.coach_tasks
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.coach_task_transfer_log
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.recall_events
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.coach_task_history
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.ai_cache
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.ai_run_logs
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.ai_trigger_jobs
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
UPDATE biz.coach_tasks SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.coach_task_transfer_log SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.recall_events SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.coach_task_history SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.ai_cache SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.ai_run_logs SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.ai_trigger_jobs SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
DROP INDEX IF EXISTS biz.idx_coach_tasks_site_assistant_member_type;
CREATE UNIQUE INDEX IF NOT EXISTS idx_coach_tasks_runtime_unique_active
ON biz.coach_tasks (site_id, assistant_id, member_id, task_type, runtime_mode, sandbox_instance_id)
WHERE status = 'active';
DROP INDEX IF EXISTS biz.idx_recall_events_site_assistant_member_day;
CREATE UNIQUE INDEX IF NOT EXISTS idx_recall_events_runtime_site_assistant_member_day
ON biz.recall_events (
site_id,
assistant_id,
member_id,
runtime_mode,
sandbox_instance_id,
(date_trunc('day', pay_time AT TIME ZONE 'Asia/Shanghai'))
);
CREATE INDEX IF NOT EXISTS idx_coach_tasks_runtime_assistant_status
ON biz.coach_tasks (site_id, runtime_mode, sandbox_instance_id, assistant_id, status);
CREATE INDEX IF NOT EXISTS idx_ai_cache_runtime_lookup
ON biz.ai_cache (cache_type, site_id, runtime_mode, sandbox_instance_id, target_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_ai_trigger_jobs_runtime_site
ON biz.ai_trigger_jobs (site_id, runtime_mode, sandbox_instance_id, event_type, status);
COMMIT;
-- 回滚参考:
-- BEGIN;
-- DROP INDEX IF EXISTS biz.idx_ai_trigger_jobs_runtime_site;
-- DROP INDEX IF EXISTS biz.idx_ai_cache_runtime_lookup;
-- DROP INDEX IF EXISTS biz.idx_coach_tasks_runtime_assistant_status;
-- DROP INDEX IF EXISTS biz.idx_recall_events_runtime_site_assistant_member_day;
-- CREATE UNIQUE INDEX idx_recall_events_site_assistant_member_day ON biz.recall_events USING btree (site_id, assistant_id, member_id, (date_trunc('day', pay_time AT TIME ZONE 'Asia/Shanghai')));
-- DROP INDEX IF EXISTS biz.idx_coach_tasks_runtime_unique_active;
-- CREATE UNIQUE INDEX idx_coach_tasks_site_assistant_member_type ON biz.coach_tasks USING btree (site_id, assistant_id, member_id, task_type) WHERE status = 'active';
-- ALTER TABLE biz.ai_trigger_jobs DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.ai_run_logs DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.ai_cache DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.coach_task_history DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.recall_events DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.coach_task_transfer_log DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.coach_tasks DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- DROP TABLE IF EXISTS biz.site_runtime_context;
-- COMMIT;