import type { Kysely } from "kysely"; import { dropTables, execDdl } from "../src/migration-helpers.ts"; // 统一元数据缓存(docs/runbook/cache.md): // - kv_cache.fresh_until:新鲜期截止。过了 fresh_until、还没到 expires_at 的条目是「旧值」: // MetadataCache 先返回旧值再后台刷新(stale-while-revalidate);PgCache.get 只返回新鲜条目。null = 没有宽限期。 // - kv_cache.pinned:关注中作品的条目,写入时按条数上限淘汰(LRU)跳过它们;到期仍会过期。 // - cache_stat:按天 × region 汇总各进程的命中统计(进程内计数,定期把增量加进来)。 // biome-ignore lint/suspicious/noExplicitAny: 迁移不依赖应用表类型 export async function up(db: Kysely): Promise { await execDdl( db, ` alter table kv_cache add column fresh_until timestamptz; alter table kv_cache add column pinned boolean not null default false; create table cache_stat ( day date not null, region text not null, l1_hits bigint not null default 0, l2_hits bigint not null default 0, stale_hits bigint not null default 0, misses bigint not null default 0, failures bigint not null default 0, refreshes bigint not null default 0, updated_at timestamptz not null default now(), primary key (day, region) ); `, ); } // biome-ignore lint/suspicious/noExplicitAny: 同上 export async function down(db: Kysely): Promise { await execDdl( db, `${dropTables(["cache_stat"])} alter table kv_cache drop column if exists pinned; alter table kv_cache drop column if exists fresh_until;`, ); }