- 迁移 043 一次到位:runtime_instance/environment_binding 并入 browser_env(账号即环境)、 credential_reference 并入 social_account、work_source/metric_plan 并入 creator_work、 sync 租约并入 creator_collection_checkpoint(kind='sync')、audit_event 瘦列、 creator_settings 去转写列、TRUNCATE 账号域(竞品采集数据无损保留) - 统一迁移 runner:environment.Open 串起 001 账号基座 + hub 链 + 038 creator 版本回填 (v+1000) + creator 1017-1042 + 043;account/creator Open 不再迁移 - 三域 store SQL 全量适配:CreateBoundEnv 就绪门禁、SetRuntimeCleanupPending fence 语义(未知代 MissingRuntimeID 允许登记并释放活跃实例)、metric_plan 列并入 work、 checkpoint sync 租约、deletion 链路重写 - api 适配:envView 去 runtime_instance_id、audit filter 去任务列、browser_env 单表查询 - 测试:迁移 043 形态探针、迁移不可变映射新路径、legacy 迁移重放子测试重写、 fence/出口生命周期/单来源 upsert 语义覆盖;CREATORHUB_POSTGRES_TEST_URL 全绿, environment 70%/creator 67.5% 覆盖率,tsc 通过
119 lines
6.6 KiB
SQL
119 lines
6.6 KiB
SQL
-- 043 Schema 收敛(42 → 19 张表):砍空转链路表、账号域清数据、同域合并、双迁移系统收尾。
|
||
-- 决策依据:docs/plans/2026-09-29-schema-consolidation-and-numeric-id.md
|
||
-- 账号域历史数据直接清空(账号即环境重构前置);竞品采集数据无损保留。
|
||
|
||
-- 1) 空转链路表:任务发布链(content_draft/confirmation/operation_task/execution_attempt)、
|
||
-- 自动运营链(creator_event/strategy/operation/conversation/message/cooldown/listener_*)、
|
||
-- 素材链(creator_material_job)、双租约冗余(runtime_use_lease)、主小号关系(creator_relation)。
|
||
DROP TABLE IF EXISTS runtime_use_lease CASCADE;
|
||
DROP TABLE IF EXISTS creator_event_strategy_trace CASCADE;
|
||
DROP TABLE IF EXISTS creator_material_job CASCADE;
|
||
DROP TABLE IF EXISTS creator_cooldown CASCADE;
|
||
DROP TABLE IF EXISTS creator_listener_boundary CASCADE;
|
||
DROP TABLE IF EXISTS creator_listener_state CASCADE;
|
||
DROP TABLE IF EXISTS creator_message CASCADE;
|
||
DROP TABLE IF EXISTS creator_conversation CASCADE;
|
||
DROP TABLE IF EXISTS creator_operation CASCADE;
|
||
DROP TABLE IF EXISTS creator_event CASCADE;
|
||
DROP TABLE IF EXISTS creator_strategy CASCADE;
|
||
DROP TABLE IF EXISTS creator_relation CASCADE;
|
||
DROP TABLE IF EXISTS execution_attempt CASCADE;
|
||
DROP TABLE IF EXISTS operation_task CASCADE;
|
||
DROP TABLE IF EXISTS confirmation CASCADE;
|
||
DROP TABLE IF EXISTS content_draft CASCADE;
|
||
|
||
-- 2) audit_event 瘦身:任务/草稿/确认列与运行时实例列随链路删除。
|
||
ALTER TABLE audit_event
|
||
DROP CONSTRAINT IF EXISTS audit_event_attempt_id_fkey,
|
||
DROP CONSTRAINT IF EXISTS audit_event_confirmation_id_fkey,
|
||
DROP CONSTRAINT IF EXISTS audit_event_task_id_fkey,
|
||
DROP CONSTRAINT IF EXISTS audit_event_runtime_instance_id_fkey;
|
||
ALTER TABLE audit_event
|
||
DROP COLUMN IF EXISTS confirmation_id,
|
||
DROP COLUMN IF EXISTS confirmation_version,
|
||
DROP COLUMN IF EXISTS attempt_id,
|
||
DROP COLUMN IF EXISTS task_id,
|
||
DROP COLUMN IF EXISTS runtime_instance_id;
|
||
|
||
-- 3) 账号域清数据:账号、凭据引用、环境、绑定、运行时实例全部清空重建;
|
||
-- CASCADE 链上的 creator_account_profile / creator_account_password / creator_account_metric
|
||
-- 与 audit_event 一并清空。gateway / network_exit / 竞品采集数据不受影响。
|
||
TRUNCATE runtime_instance, environment_binding, browser_env, social_account, credential_reference
|
||
RESTART IDENTITY CASCADE;
|
||
|
||
-- 4) environment_binding + runtime_instance 并入 browser_env(1:1 绑定 + 1:1 活跃实例)。
|
||
-- 绑定列:account_id(UNIQUE,账号即环境)、network_exit_id(version 绑定代由 003 引入,复用);
|
||
-- 清理 fence 列:runtime_cleanup_*;活跃实例列:runtime_id/runtime_lease_until/runtime_network_id/runtime_node_id。
|
||
ALTER TABLE browser_env
|
||
ADD COLUMN account_id text NOT NULL UNIQUE REFERENCES social_account(id) ON DELETE CASCADE,
|
||
ADD COLUMN network_exit_id text REFERENCES network_exit(id),
|
||
ADD COLUMN runtime_cleanup_pending boolean NOT NULL DEFAULT false,
|
||
ADD COLUMN runtime_cleanup_binding_version bigint,
|
||
ADD COLUMN runtime_cleanup_runtime_id text,
|
||
ADD COLUMN runtime_cleanup_network_id text,
|
||
ADD COLUMN runtime_id text,
|
||
ADD COLUMN runtime_lease_until timestamptz,
|
||
ADD COLUMN runtime_network_id text,
|
||
ADD COLUMN runtime_node_id text,
|
||
ADD COLUMN updated_at timestamptz NOT NULL DEFAULT now();
|
||
DROP TABLE runtime_instance;
|
||
DROP TABLE environment_binding;
|
||
|
||
-- 5) credential_reference 并入 social_account(provider + key 两列)。
|
||
ALTER TABLE social_account
|
||
ADD COLUMN credential_provider text NOT NULL DEFAULT 'os_keyring'
|
||
CHECK (credential_provider IN ('os_keyring', 'secret_manager')),
|
||
ADD COLUMN credential_key text NOT NULL DEFAULT '';
|
||
ALTER TABLE social_account DROP COLUMN credential_reference_id;
|
||
DROP TABLE credential_reference;
|
||
|
||
-- 6) creator_work_source 并入 creator_work:work 已有 source_type/source_id 内联列,
|
||
-- 来源表是双份状态;删除后来源切换仍由 upsert 覆盖内联列完成。
|
||
DROP TABLE creator_work_source;
|
||
|
||
-- 7) creator_metric_plan 并入 creator_work:调度列(发布/窗口/节奏/停点)并入作品行,
|
||
-- work 原有的 next_metric_at / metric_stop_reason 继续作为调度投影。
|
||
ALTER TABLE creator_work
|
||
ADD COLUMN metric_plan_published_at timestamptz,
|
||
ADD COLUMN metric_monitoring_end timestamptz,
|
||
ADD COLUMN metric_plan_next_at timestamptz,
|
||
ADD COLUMN metric_plan_interval_seconds bigint,
|
||
ADD COLUMN metric_plan_multiplier double precision,
|
||
ADD COLUMN metric_plan_max_interval_seconds bigint,
|
||
ADD COLUMN metric_plan_point_index integer NOT NULL DEFAULT 0 CHECK (metric_plan_point_index >= 0),
|
||
ADD COLUMN metric_plan_stopped boolean NOT NULL DEFAULT false;
|
||
UPDATE creator_work w SET
|
||
metric_plan_published_at = p.published_at,
|
||
metric_monitoring_end = p.monitoring_end,
|
||
metric_plan_next_at = p.next_plan_at,
|
||
metric_plan_interval_seconds = p.interval_seconds,
|
||
metric_plan_multiplier = p.multiplier,
|
||
metric_plan_max_interval_seconds = p.max_interval_seconds,
|
||
metric_plan_point_index = p.point_index,
|
||
metric_plan_stopped = p.stopped
|
||
FROM creator_metric_plan p WHERE p.work_id = w.id;
|
||
DROP TABLE creator_metric_plan;
|
||
|
||
-- 8) creator_source_sync_lease 并入 creator_collection_checkpoint(统一租约模式):
|
||
-- 账号/竞品同步互斥租约成为 checkpoint 的一种 collection_kind='sync',与 works/comments 同写法。
|
||
ALTER TABLE creator_collection_checkpoint DROP CONSTRAINT creator_collection_checkpoint_collection_kind_check;
|
||
ALTER TABLE creator_collection_checkpoint
|
||
ADD CONSTRAINT creator_collection_checkpoint_collection_kind_check
|
||
CHECK (collection_kind IN ('works', 'comments', 'sync'));
|
||
INSERT INTO creator_collection_checkpoint
|
||
(id, source_type, source_id, collection_kind, cursor, window_start, window_end, status, lease_token, lease_until, last_error)
|
||
SELECT 'sync:' || source_type || ':' || source_id, source_type, source_id, 'sync', '', now(), now(), 'idle', lease_token, lease_until, ''
|
||
FROM creator_source_sync_lease
|
||
ON CONFLICT (source_type, source_id, collection_kind) DO UPDATE SET
|
||
lease_token = EXCLUDED.lease_token, lease_until = EXCLUDED.lease_until;
|
||
DROP TABLE creator_source_sync_lease;
|
||
|
||
-- 9) creator_settings 瘦身:转写配置列属于已砍素材链路。
|
||
ALTER TABLE creator_settings
|
||
DROP COLUMN IF EXISTS transcription_provider,
|
||
DROP COLUMN IF EXISTS transcription_model,
|
||
DROP COLUMN IF EXISTS transcription_configured;
|
||
|
||
-- 10) 双迁移系统收尾:creator 域登记已折算进 schema_migration(038),登记表删除。
|
||
DROP TABLE IF EXISTS creator_schema_migration;
|