Files
codex-proxy-rs/backend/migrations
ZyyandClaude Fable 5 d199141a91 feat(store): materialize quota_limit_reached for single-source limit status
Dashboard reads the materialized column instead of parsing raw quota
JSON (design §3.3/§5.3): Provider computes snapshot-level limit_reached
on quota writes, store persists it, Admin account_status and Dashboard
both consume the same value.

- QuotaObservation gains limit_reached; openai writes fact.exhausted()
  (429 always true), xai writes used_percent>=100
- compare_and_swap_provider_quota updates the column; get_quotas and
  ACCOUNT_SELECT read it back
- ProviderAccountSummary carries quota_limit_reached; admin_account_status
  derives RateLimited from it (Unknown -> Invalid)
- Dashboard provider_account_metrics reads quota_limit_reached instead of
  provider_quota_json->'rate_limit'
- Migration 0003 adds the column with backfill (coalesce for NULL when
  rate_limit key is absent); 0004 fixes NULL backfill for already-applied
  environments

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-03 03:32:17 +08:00
..

数据库迁移

sqlx 在服务启动时(serve 监听之前)按编号顺序执行本目录的迁移,并把每个 文件的 checksum 记入 _sqlx_migrations。此后每次启动都会重新校验:已应用 迁移的文件字节与记录不一致时,服务直接拒绝启动。

冻结规则

  • 同一大版本内,已合入 main 的迁移文件永久冻结,一个字节都不能改——包括注释和 空白。任何改动都会让该大版本的已部署实例在下次重启时因 checksum 失配而无法启动, 且没有自动恢复路径。
  • 同一大版本内的 schema 变更一律新增编号迁移(0002_...、0003_...),永远不回改 旧文件。切换大版本时允许把已完成升级的 schema 折叠成新的 0001 基线,因为跨大版本 不支持在线升级,目标版本必须使用全新数据目录部署。
  • .frozen-sha256 是冻结清单(sha256sum 格式)。CI 会校验:清单内文件 字节未变,且目录里每个 *.sql 都已入册。新增迁移时在同一提交里执行 sha256sum 000N_xxx.sql >> .frozen-sha256 入册。
  • 若同一大版本内确需修正已冻结迁移里的错误,用新迁移做补偿性变更(alter / 回填), 不要动原文件。

本地测试库

gateway-store 的 PG/Redis 集成测试需要以下环境变量,未设置时在本地 静默跳过(CI 缺失则直接失败):

export CPR_TEST_DATABASE_URL='postgres://<user>:<password>@127.0.0.1:5432/<db>'
export CPR_TEST_REDIS_URL='redis://:<password>@127.0.0.1:6379'

测试自建随机 schema / key 前缀做隔离,可安全指向开发库;凭据按本地部署 实际值填写(docker inspect 对应容器可查)。