import type { Kysely } from "kysely"; import { execDdl } from "../src/migration-helpers.ts"; // 数据库性能与可观测(docs/runbook/storage.md「数据库性能」): // - scheduled_job_run_harvest_idx:调度器每 15 秒回填巡检结果(scheduler.harvest),原来每次全表扫运行历史; // 部分索引只含「有 agent_run 且尚未回填」的行,通常为 0~几行; // - 删除 content_unit_air_date_idx:没有任何查询按 air_date 过滤或排序(订阅日历在内存里按日期筛), // 按作品取单元走 content_unit_entity_numbering_idx; // - pg_stat_statements:镜像自带时才建(库级共享对象,放 public;down 不卸载)。要真正采集还需要 // postgres 以 shared_preload_libraries=pg_stat_statements 启动(docker-compose.yml),没有预加载时只是查询会报错, // 状态页按「未启用」处理。没有该扩展或权限不够时跳过,不让迁移失败。 // biome-ignore lint/suspicious/noExplicitAny: 迁移不依赖应用表类型 export async function up(db: Kysely): Promise { await execDdl( db, ` create index if not exists scheduled_job_run_harvest_idx on scheduled_job_run (agent_run_id) where agent_status is null and agent_run_id is not null; drop index if exists content_unit_air_date_idx; do $$ begin if exists (select 1 from pg_available_extensions where name = 'pg_stat_statements') then begin create extension if not exists pg_stat_statements with schema public; exception when others then raise notice 'pg_stat_statements 未安装:%', sqlerrm; end; end if; end $$; `, ); } // biome-ignore lint/suspicious/noExplicitAny: 同上 export async function down(db: Kysely): Promise { await execDdl( db, ` drop index if exists scheduled_job_run_harvest_idx; create index if not exists content_unit_air_date_idx on content_unit (media_entity_id, air_date) where air_date is not null; -- 扩展为库级共享对象,down 不卸载 `, ); }