[codex] Add Postgres/Neon DATABASE_URL support#77
Conversation
ZeroPointSix
left a comment
There was a problem hiding this comment.
审查结果(针对 Postgres/Neon DATABASE_URL 支持):
发现 2 个需要修复的阻断级问题:
-
Postgres 模式初始化会被
sqlite_master查询卡住。
outlook_web/db.py里的temp_email_messages迁移段会执行SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'temp_email_messages'。新增的兼容适配器目前只特殊处理了PRAGMA table_info(...)/PRAGMA index_list(...)等,未拦截或翻译sqlite_master查询,所以这条 SQL 会原样发给 PostgreSQL,并触发类似relation "sqlite_master" does not exist的错误。结果是设置DATABASE_URL=postgresql://...后,应用初始化数据库时会失败。建议要么在适配器里模拟这类 schema 查询,要么把这段迁移改成基于information_schema/pg_constraint的可移植唯一约束检测。 -
SQLite
strftime默认值没有完整翻译,初始化会遇到 PostgreSQL 语法错误。
现有 schema 创建telegram_push_log时使用pushed_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S', 'now'))。当前translate_sqlite_sql()只覆盖了strftime('%s','now')和unixepoch('now'),没有覆盖这个日期格式;PostgreSQL 不支持 SQLite 的strftime(...),因此创建该表时会失败。建议覆盖该格式,或把 schema 默认值改成 PostgreSQL 可接受的CURRENT_TIMESTAMP/to_char(...)等表达式。
建议补一个覆盖真实启动路径的测试:在 DATABASE_URL=postgresql://... 下执行 init_db() 到完成(可以用 fake psycopg 捕获最终 SQL,或使用临时 Postgres 容器)。当前新增测试主要验证 SQL 翻译片段,还没有覆盖 init_db() 的完整迁移路径,所以这些阻断问题没有被测出来。
|
总体结论:本轮复核看到前一次 review 提到的两个阻断点已经有针对性修复: 关键发现:
优先级建议:
后续建议:
|
|
CodeXWeb 本轮自动巡检已认领,正在处理本 PR 的 review 修改意见。 本轮只会处理这一项:#77。接下来会先对照 PR diff、review 评论、相关 Issue/Workspace 记录和现有实现约束,只做最小必要修复;完成后会在本 PR 回写改动、验证结果和风险,不会自动合并。 |
|
CodeXWeb 本轮自动巡检处理完成,本轮只处理 PR #77。 处理对象:review 评论 4670508474 提到的 fake psycopg 启动路径补测。 提交:2c026314dc974517dc75a1b4f963e84922ba8e47,message 为 test: cover postgres init path。 改动:补齐 email 相关 SQLite 函数到 PostgreSQL POSITION 和 SPLIT_PART 的翻译;新增完整 DATABASE_URL 到 init_db 的 fake psycopg 回归测试;更新 WORKSPACE 记录。 验证:Postgres compat 单测 21 passed;py_compile passed;git diff check passed;完整 pytest 在沙箱补装 pytest 和 Playwright、启动本地服务后运行,结果为 1544 passed, 7 skipped, 31 subtests passed, 4 failed。4 个失败均为真实 Cloudflare Worker E2E,上游返回 HTTP 400 和 UPSTREAM_BAD_PAYLOAD,和本次 Postgres Neon SQL 翻译及启动路径补测不相邻。 剩余风险:本轮没有连接真实 Neon 或 PostgreSQL 实例做 smoke test;review 建议的 fake psycopg 启动路径回归已补齐并通过。 |
|
ZeroPointSix
left a comment
There was a problem hiding this comment.
总体结论:本轮针对最新提交 2c026314dc974517dc75a1b4f963e84922ba8e47 复核新增的 Postgres 兼容层补测和 SQL 翻译改动,未发现新的阻塞问题;上一轮要求的 fake psycopg 启动路径覆盖已经补上。
关键发现:
- 已核对新增
test_postgres_shim_runs_full_init_db_without_sqlite_only_sql,它会在DATABASE_URL=postgresql://...下安装 shim 并直接执行db.init_db(),覆盖了前次 review 要求的从配置到初始化的完整路径,比单独测试 SQL 片段更能防止 SQLite-only 语句漏到 PostgreSQL。 - 已核对新增的
instr(email, '@')/substr(...)/LOWER(SUBSTR(...))翻译,当前覆盖了初始化路径中出现的 email prefix/domain 维护语句,并在测试中断言最终 SQL 不再包含instr(/substr(等 PostgreSQL 不支持片段。 - 已核对
sqlite_master、PRAGMA、strftime、INSERT OR REPLACE、INSERT OR IGNORE、AUTOINCREMENT、COLLATE NOCASE、unixepoch等风险片段的启动路径断言,能覆盖前两轮审查中指出的主要兼容性缺口。 - 仍需注意:本轮测试使用 fake psycopg 证明应用初始化会发出 PostgreSQL 风格 SQL,但不能替代真实 Neon/PostgreSQL 实例上的 DDL/约束/事务行为 smoke test。
优先级建议:
- 阻塞:无。
- 中:合并前如条件允许,仍建议用真实 PostgreSQL 或 Neon 临时库跑一次启动 smoke,确认 psycopg 连接、DDL、唯一约束和 upsert 行为在真实数据库上完整通过。
- 低:后续如果继续新增 SQLite 方言语句,建议同步扩展这个 init_db 路径测试的禁止片段列表,避免兼容层只覆盖旧 schema。
后续建议:
- 当前补测已经回应前次 review 的高优先级建议;剩余风险主要是真实数据库环境验证,而不是当前 fake 初始化路径覆盖不足。



What changed
Fixes #23 by adding a first-stage PostgreSQL/Neon database mode while keeping SQLite as the default.
DATABASE_PATHbehavior whenDATABASE_URLis empty or SQLite-like.DATABASE_URL=postgresql://.../postgres://...detection at package startup.sqlite3.connectcompatibility adapter backed bypsycopg, so existing code can keep using sqlite-style rows,?placeholders,BEGIN IMMEDIATE,PRAGMA table_info,INSERT OR IGNORE, and the current settings/temp-message upsert patterns.sqlite_mastertable-schema lookup and ISOstrftime(..., 'now')default expression.psycopg[binary]to runtime dependencies.CI/CD validation
Latest PR head:
521b8fe109bfd94fe1264c8fbef04cad3fba649fLocal validation in the workspace:
python -m py_compile outlook_web/db_postgres_compat.py tests/test_db_postgres_compat.pytests.test_db_postgres_compatpassed with a dummy app module, because the workspace checkout only contains the files needed for this PR.Notes / follow-up
This does not perform SQLite-to-Postgres data migration. Production users should export or migrate existing SQLite data separately before switching traffic to
DATABASE_URL.A later larger refactor can replace this compatibility bridge with a formal migration layer such as SQLAlchemy/Alembic if the project wants deeper multi-database support.