应用库可移植到 PostgreSQL:AGENTLAB_DATABASE_URL 设成 postgresql+asyncpg://… 时整个应用库用 PostgreSQL,断点用官方的 langgraph-checkpoint-postgres(同一个库,AGENTLAB_CHECKPOINT_URL 可另给);不设时照旧是数据目录里的 SQLite,行为不变

- 配置:database_url、checkpoint_url(启动时校验写法,只认 PostgreSQL)、连接池四项;启动日志写用的是哪种库(主机、库名、用户,不写密码)
- 引擎(db/base.make_engine):SQLite 照旧 WAL、外键、忙等;PostgreSQL 用连接池、探活、会话时区 UTC,装上方言调整(db/postgres.py):建表时 String(n) 是不限长的 VARCHAR(SQLite 从不查长度,导过来的存量也可能超长)、ORDER BY 里可空列的 NULL 位置照 SQLite 补写(升序在前、降序在后,窗口函数里同样);Integer 列在 PostgreSQL 上是 BIGINT(列挂到表上时换成 with_variant,绑定参数跟着列按 BIGINT 转换);JSON 列两边都存原文(PostgreSQL 的 json,不用 JSONB:JSONB 重排对象里的键,快照的列序、表的先后读回来就变了);JSON 里的 NaN 写成 null、NUL 去掉(PostgreSQL 不收)
- 迁移按方言:已有的列和索引用 inspector 认;PostgreSQL 上补列的类型取模型里这一列的、缺省值取迁移表里的(布尔 0/1 换成 false/true),ADD COLUMN IF NOT EXISTS;去重语句里的布尔按方言写;部分唯一索引补 postgresql_where;init_db 在 PostgreSQL 上拿咨询锁(几个进程同时启动不撞)
- 应用库上的 SQLite 专用写法改成按方言编译的表达式(db/portable.py):json_extract 的十几处(口径召回、出具、对账、个人应用、幂等键、访问控制、数据源变化通知、剖析任务、待办超时)换成 json_eq / json_flag / json_text,json_type 换成 json_is_string(两种库同一套带类型的比较);能力登记的 sqlite insert 换成按会话方言的 insert_ignore;能力使用次数的 json_each 按方言分支;附件「没绑记录」的条件不靠 json 的等号;按报错文字认唯一约束的地方换成 unique_violation(按表和列认,两种库的报错都认得)
- 断点(engine/checkpoints.py):PostgreSQL 时开 psycopg 连接池、在咨询锁里 setup;runner 只认 BaseCheckpointSaver,读断点仍只走 saver 的公开接口
- 依赖:新 extra postgres(asyncpg、psycopg[binary] 3、psycopg-pool、langgraph-checkpoint-postgres 3.1.2);langgraph-checkpoint 卡在 <4.3(断点格式就在这个包里,postgres 包新版要 4.3,不动已装的 4.2.0);锁文件补上新包
- 运行收尾:记 action.unknown、安排核实做完才把运行摘掉(以前先摘掉,等「不在跑了」再读事件的一方在库慢时读不到这一条)
- 测试:AGENTLAB_TEST_DATABASE_URL 时跑在 PostgreSQL 上(每个 xdist 进程建自己的库、按事件循环分的连接池 tests/pgpool.py),不设时盖掉环境里的数据库地址照旧用 SQLite;scripts/test-postgres.sh 起临时的 postgres:16 容器跑全量;sqlite_only 标记;另开连接改库的几个竞态测试、触发器测试改成两种库都能跑(tests/appdb.py);新增 test_db_portable 守着方言调整、JSON 键序、超长字符串、大整数、NULL 排序、时区、PostgreSQL 老库迁移
This commit is contained in:
ThearchyHelios
2026-10-19 09:52:09 +08:00
parent c79f4e8bf7
commit 2e7d6103ea
39 changed files with 1522 additions and 155 deletions
+194 -37
View File
@@ -1,16 +1,23 @@
from __future__ import annotations
import json
import logging
import math
import re
import uuid
from datetime import datetime, timezone
from typing import Any, AsyncIterator
from sqlalchemy import JSON, DateTime, MetaData, event
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy import JSON, BigInteger, Column, DateTime, Integer, MetaData, event
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.pool import NullPool
from sqlalchemy.types import TypeDecorator
from app.core.config import settings
logger = logging.getLogger(__name__)
NAMING_CONVENTION = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
@@ -57,11 +64,31 @@ class UTCDateTime(TypeDecorator):
return value.astimezone(timezone.utc)
#: 应用库的 JSON 列:两种库上都是存原文的 JSON(PostgreSQL 的 json,不是 JSONB)。JSONB 会把对象里的键重新排序:
#: 应用里不少地方按存进去的键的先后出东西(快照的列、表的先后、复核结论各段的先后),换成 JSONB 读回来就变了样,
#: 测试当场抓到好几处。json 的代价是没有相等运算符:== 比较、DISTINCT、GROUP BY、ORDER BY 不能直接作用在整列上
#: (应用里没有这种写法,test_db_portable 守着);按里面某个键筛选走 db/portable 的 json_eq 等,只把取出来的那一小段
#: 转成 JSONB 比较
PortableJSON = JSON()
class Base(DeclarativeBase):
metadata = MetaData(naming_convention=NAMING_CONVENTION)
# 图定义、节点配置、事件负载都是任意形状的 JSON,统一映射到 SQLite 的 JSON 列
type_annotation_map = {dict[str, Any]: JSON, list[Any]: JSON, datetime: UTCDateTime}
# 图定义、节点配置、事件负载都是任意形状的 JSON,统一映射到 JSON 列(见 PortableJSON)
type_annotation_map = {dict[str, Any]: PortableJSON, list[Any]: PortableJSON, datetime: UTCDateTime}
@event.listens_for(Column, "after_parent_attach")
def _portable_integer(column: Column, table: Any) -> None:
"""应用库的 Integer 列在 PostgreSQL 上换成 BIGINT(SQLite 照旧是 INTEGER,主键照旧是 rowid)。
SQLite 的 INTEGER 是 64 位,PostgreSQL 的只有 32 位:行数估计、字节数、累计的计数都可能过 21 亿。在列挂到表上
的那一刻换(定义模型时,任何查询之前):建表是 BIGINT(自增主键是 BIGSERIAL),和这一列比较、写进这一列的绑定
参数也按 BIGINT 转换。只管挂在应用 metadata 上、类型正好是 Integer 的列;业务数据源那边的表不受影响。"""
if getattr(table, "metadata", None) is Base.metadata and type(column.type) is Integer \
and not column.type._variant_mapping:
column.type = Integer().with_variant(BigInteger(), "postgresql")
class TimestampMixin:
@@ -101,16 +128,10 @@ def _stop_aiosqlite_once() -> None:
aiosqlite.Connection.stop = stop # type: ignore[method-assign]
# 应用库是 PostgreSQL 时也照样打:上传表格、手工登记的 SQLite 业务库(data/engine)还是走 aiosqlite 的连接池
_stop_aiosqlite_once()
# pool_pre_ping:取连接前先探一下活。取消异常打进 SQLAlchemy 的清理流程时
# (_close_connection 记完日志会把 CancelledError 原样抛出,跳过了把连接记录
# 清空的那一行),已经关掉的连接会留在池里,下一个取到它的人拿到的是
# "no active connection"。探活失败会被当成断开,换一条新的——SQLite 本地一次
# SELECT 1 的代价,换池子不被污染。
engine = create_async_engine(settings.db_url, echo=False, future=True, pool_pre_ping=True)
@event.listens_for(engine.sync_engine, "connect")
def _set_sqlite_pragma(dbapi_conn, _record): # pragma: no cover - 驱动回调
"""WAL + 外键约束。WAL 让读写不互相阻塞,事件写入和前端轮询能并行。"""
cur = dbapi_conn.cursor()
@@ -120,6 +141,92 @@ def _set_sqlite_pragma(dbapi_conn, _record): # pragma: no cover - 驱动回调
cur.close()
def _finite(value: Any) -> Any:
if isinstance(value, float):
return value if math.isfinite(value) else None
if isinstance(value, dict):
return {k: _finite(v) for k, v in value.items()}
if isinstance(value, (list, tuple)):
return [_finite(v) for v in value]
return value
def _no_nul(value: Any) -> Any:
if isinstance(value, str):
return value.replace("\x00", "")
if isinstance(value, dict):
return {_no_nul(k): _no_nul(v) for k, v in value.items()}
if isinstance(value, list):
return [_no_nul(v) for v in value]
return value
def pg_json_dumps(value: Any) -> str:
"""PostgreSQL 上写 JSON 列。它不收 NaN / Infinity(SQLite 照存);字符串里的 \\u0000 json 列收,但一转成 JSONB
(按键筛选时,db/portable)、一取成文本就报错,那一整条查询跟着失败。
非有限的浮点写成 null(同浏览器的 JSON.stringify),字符串里的 NUL 字符去掉;平常的值一次 dumps 就过,
只有碰上了才多走一遍。"""
try:
text = json.dumps(value, allow_nan=False)
except ValueError:
text = json.dumps(_finite(value), allow_nan=False)
if "\\u0000" in text:
text = json.dumps(_no_nul(json.loads(text)), allow_nan=False)
return text
#: 应用连 PostgreSQL 时报的 application_name。copy-to-postgres 按它认目标库上还有没有服务连着(它自己报的是 agentlab-copy)
APPLICATION_NAME = "agentlab"
def make_engine(url: str | None = None, *, application_name: str = APPLICATION_NAME) -> AsyncEngine:
"""应用库的引擎。SQLite:WAL、外键、忙等,单个文件;PostgreSQL:连接池、方言调整(db/postgres.py)。
copy-to-postgres 建目标库的引擎也走这里,两边的列类型、排序规则是同一套。
"""
url = url or settings.db_url
if url.startswith("sqlite"):
# pool_pre_ping:取连接前先探一下活。取消异常打进 SQLAlchemy 的清理流程时
# (_close_connection 记完日志会把 CancelledError 原样抛出,跳过了把连接记录
# 清空的那一行),已经关掉的连接会留在池里,下一个取到它的人拿到的是
# "no active connection"。探活失败会被当成断开,换一条新的——SQLite 本地一次
# SELECT 1 的代价,换池子不被污染。
made = create_async_engine(url, echo=False, future=True, pool_pre_ping=True)
event.listen(made.sync_engine, "connect", _set_sqlite_pragma)
return made
from app.db import postgres
pool: dict[str, Any] = (
{"poolclass": NullPool} if settings.db_pool_size <= 0 else {
"pool_size": settings.db_pool_size, "max_overflow": max(settings.db_max_overflow, 0),
"pool_timeout": settings.db_pool_timeout, "pool_recycle": settings.db_pool_recycle})
made = create_async_engine(
url, echo=False, future=True, pool_pre_ping=True, json_serializer=pg_json_dumps,
# 会话时区定成 UTC:时间列是不带时区的 UTC(UTCDateTime),库里 now() 之类的也按 UTC 算
connect_args={"server_settings": {"application_name": application_name, "timezone": "UTC"}},
**pool)
postgres.install(made.sync_engine.dialect)
return made
def describe_database() -> str:
"""启动日志里的一句:用的是哪种库、在哪(不写密码)。"""
from app.db.urls import describe
if settings.checkpoint_url:
points = f"断点在 PostgreSQL {describe(settings.checkpoint_url)}"
elif settings.db_dialect == "sqlite":
points = f"断点 {settings.checkpoint_path}"
else:
points = "断点在同一个库"
if settings.db_dialect == "sqlite":
return f"SQLite({settings.db_path},{points})"
return f"PostgreSQL({describe(settings.db_url)},{points})"
engine = make_engine()
SessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
@@ -129,8 +236,10 @@ async def get_session() -> AsyncIterator[AsyncSession]:
yield session
# 轻量迁移:create_all 只建新表,不给已有表加列。SQLite 的 ADD COLUMN
# 便宜且带默认值回填,够用;等真需要改列类型再考虑 alembic。
# 轻量迁移:create_all 只建新表,不给已有表加列。ADD COLUMN 便宜且带默认值回填,够用;等真需要改列类型再考虑 alembic。
# 第三项照 SQLite 写(类型和缺省值的字面量)。PostgreSQL 上类型取模型里这一列的(按应用库的方言调整编译:String(n)
# 是 VARCHAR、Integer 是 BIGINT、JSON 是 json、时间是 TIMESTAMP),缺省值取这里写的,布尔的 0 / 1 换成 false / true
# (_column_ddl)。所以这里加的每一列模型里都得有:test_db_portable 守着
_COLUMN_MIGRATIONS: list[tuple[str, str, str]] = [
("runs", "run_class", "TEXT DEFAULT 'exploratory'"),
("runs", "version", "INTEGER"),
@@ -274,44 +383,92 @@ _INDEX_MIGRATIONS: list[tuple[str, str, str | tuple[str, ...]]] = [
#: 迁移补建的唯一索引:(索引名, 表, 列, 部分索引的条件, 建之前的去重语句)。只在索引还不存在时先去重再建——存量库里可能
#: 已经有并发留下的重复行,直接建会失败。去重的规矩写在各自的语句里:多出的钩子行删掉(只留最早的那一行,它的密钥照旧
#: 能用),多出的助手对话归档(不删,历史还在)
#: 能用),多出的助手对话归档(不删,历史还在)。条件和语句里的 {false} / {true} 按方言换成布尔的字面量(SQLite 是 0 / 1)
_UNIQUE_INDEX_MIGRATIONS: list[tuple[str, str, tuple[str, ...], str | None, str]] = [
("ux_app_hooks_app_name", "app_hooks", ("app_id", "name"), None,
"DELETE FROM app_hooks WHERE id NOT IN (SELECT id FROM (SELECT id, ROW_NUMBER() OVER "
"(PARTITION BY app_id, name ORDER BY created_at, id) AS rn FROM app_hooks) WHERE rn = 1)"),
("ux_conversations_app_owner", "conversations", ("app_id", "created_by"), "kind = 'app' AND archived = 0",
"UPDATE conversations SET archived = 1 WHERE kind = 'app' AND archived = 0 AND created_by IS NOT NULL "
"(PARTITION BY app_id, name ORDER BY created_at, id) AS rn FROM app_hooks) AS ranked WHERE rn = 1)"),
("ux_conversations_app_owner", "conversations", ("app_id", "created_by"), "kind = 'app' AND archived = {false}",
"UPDATE conversations SET archived = {true} WHERE kind = 'app' AND archived = {false} AND created_by IS NOT NULL "
"AND id NOT IN (SELECT id FROM (SELECT id, ROW_NUMBER() OVER (PARTITION BY app_id, created_by "
"ORDER BY last_active_at DESC, id) AS rn FROM conversations WHERE kind = 'app' AND archived = 0 "
"AND created_by IS NOT NULL) WHERE rn = 1)"),
"ORDER BY last_active_at DESC, id) AS rn FROM conversations WHERE kind = 'app' AND archived = {false} "
"AND created_by IS NOT NULL) AS ranked WHERE rn = 1)"),
]
#: 布尔字面量:SQLite 的布尔列存 0 / 1
_BOOL_LITERALS = {"sqlite": {"false": "0", "true": "1"}, "postgresql": {"false": "false", "true": "true"}}
_DEFAULT = re.compile(r"\s+DEFAULT\s+(.+)$", re.I | re.S)
def _column_ddl(dialect: Any, table: str, column: str, ddl: str) -> str:
"""ADD COLUMN 里列名后面那一段。SQLite 原样用迁移表里写的;PostgreSQL 类型取模型里这一列的,缺省值取迁移表里的。"""
if dialect.name == "sqlite":
return ddl
from sqlalchemy import Boolean
from app.db.postgres import AppTypeCompiler
col = Base.metadata.tables[table].c[column]
out = AppTypeCompiler(dialect).process(col.type, type_expression=col)
if match := _DEFAULT.search(ddl):
default = match.group(1).strip()
if isinstance(col.type, Boolean):
default = {"0": "false", "1": "true"}.get(default, default)
out += f" DEFAULT {default}"
return out
def _migrate(conn) -> None: # pragma: no cover - 同步回调
from sqlalchemy import text
"""给老库补列、补索引。已有的列和索引按 inspector 认(两种库同一套),再跑一遍什么都不做。"""
from sqlalchemy import inspect, text
dialect = conn.dialect
literals = _BOOL_LITERALS.get(dialect.name, _BOOL_LITERALS["postgresql"])
inspector = inspect(conn)
tables = set(inspector.get_table_names())
columns = {t: {c["name"] for c in inspector.get_columns(t)} for t in tables}
# IF NOT EXISTS:PostgreSQL 上多个进程同时启动,各自的检查和补列之间别人可能已经补上了(init_db 另外拿着锁)
add = "ADD COLUMN IF NOT EXISTS" if dialect.name == "postgresql" else "ADD COLUMN"
for table, column, ddl in _COLUMN_MIGRATIONS:
cols = {row[1] for row in conn.execute(text(f"PRAGMA table_info({table})"))}
cols = columns.get(table)
if cols and column not in cols:
conn.execute(text(f"ALTER TABLE {table} ADD COLUMN {column} {ddl}"))
for name, table, columns in _INDEX_MIGRATIONS:
wanted = (columns,) if isinstance(columns, str) else columns
cols = {row[1] for row in conn.execute(text(f"PRAGMA table_info({table})"))}
conn.execute(text(f"ALTER TABLE {table} {add} {column} {_column_ddl(dialect, table, column, ddl)}"))
cols.add(column)
for name, table, wanted in _INDEX_MIGRATIONS:
wanted = (wanted,) if isinstance(wanted, str) else wanted
cols = columns.get(table) or set()
if all(c in cols for c in wanted):
conn.execute(text(f"CREATE INDEX IF NOT EXISTS {name} ON {table} ({', '.join(wanted)})"))
for name, table, columns, where, dedupe in _UNIQUE_INDEX_MIGRATIONS:
cols = {row[1] for row in conn.execute(text(f"PRAGMA table_info({table})"))}
exists = conn.execute(text("SELECT 1 FROM sqlite_master WHERE type = 'index' AND name = :n"), {"n": name}).first()
if exists or not all(c in cols for c in columns):
for name, table, wanted, where, dedupe in _UNIQUE_INDEX_MIGRATIONS:
cols = columns.get(table) or set()
if not all(c in cols for c in wanted):
continue
conn.execute(text(dedupe))
conn.execute(text(f"CREATE UNIQUE INDEX {name} ON {table} ({', '.join(columns)})"
+ (f" WHERE {where}" if where else "")))
if any(ix["name"] == name for ix in inspect(conn).get_indexes(table)):
continue
conn.execute(text(dedupe.format(**literals)))
conn.execute(text(f"CREATE UNIQUE INDEX {name} ON {table} ({', '.join(wanted)})"
+ (f" WHERE {where.format(**literals)}" if where else "")))
#: init_db 在 PostgreSQL 上拿的会话级咨询锁的号:几个进程同时启动时,建表和迁移一个一个来(同一个号别处别用)
_INIT_LOCK = 0x4167656E744C6162 # "AgentLab"
async def create_schema(target: AsyncEngine) -> None:
"""建表、补列补索引(create_all 只建新表,老库的新列靠 _migrate)。两种库同一套;copy-to-postgres 建目标库也用它。"""
from app.db import models # noqa: F401 确保模型已注册到 metadata
async with target.begin() as conn:
if conn.dialect.name == "postgresql":
# 事务结束自动放锁。CREATE TABLE IF NOT EXISTS 在 PostgreSQL 上并发执行也会撞(系统表的唯一约束)
from sqlalchemy import text
await conn.execute(text("SELECT pg_advisory_xact_lock(:k)"), {"k": _INIT_LOCK})
await conn.run_sync(Base.metadata.create_all)
await conn.run_sync(_migrate)
async def init_db() -> None:
from app.db import models # noqa: F401 确保模型已注册到 metadata
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
await conn.run_sync(_migrate)
logger.info("应用库:%s", describe_database())
await create_schema(engine)
+6 -3
View File
@@ -362,7 +362,8 @@ class CatalogDraftJob(Base, TimestampMixin):
__table_args__ = (
Index("ix_catalog_draft_jobs_source_created", "source_id", "created_at"),
Index("ux_catalog_draft_jobs_active", "source_id", unique=True,
sqlite_where=text("status IN ('queued', 'running')")),
sqlite_where=text("status IN ('queued', 'running')"),
postgresql_where=text("status IN ('queued', 'running')")),
)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
@@ -450,7 +451,8 @@ class CatalogProfileJob(Base, TimestampMixin):
__table_args__ = (
Index("ix_catalog_profile_jobs_source_created", "source_id", "created_at"),
Index("ux_catalog_profile_jobs_active", "source_id", unique=True,
sqlite_where=text("status IN ('queued', 'running')")),
sqlite_where=text("status IN ('queued', 'running')"),
postgresql_where=text("status IN ('queued', 'running')")),
)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
@@ -904,7 +906,8 @@ class Conversation(Base, TimestampMixin):
# 一个人在一个应用里只有一段(未归档的)助手对话(D-e)。存量库由 db/base.py 的 _UNIQUE_INDEX_MIGRATIONS 补建。
# created_by 为空(关登录又没填署名)的不受约束:SQLite 的唯一索引里 NULL 互不相等,那种情况靠接口里的进程内锁
__table_args__ = (Index("ux_conversations_app_owner", "app_id", "created_by", unique=True,
sqlite_where=text("kind = 'app' AND archived = 0")),)
sqlite_where=text("kind = 'app' AND archived = 0"),
postgresql_where=text("kind = 'app' AND archived = false")),)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
title: Mapped[str] = mapped_column(String(200), default="")
+6 -5
View File
@@ -19,10 +19,10 @@ from __future__ import annotations
from datetime import datetime
from typing import Any
from sqlalchemy import JSON, Boolean, ForeignKey, Index, Integer, String, Text, UniqueConstraint, text
from sqlalchemy import Boolean, ForeignKey, Index, Integer, String, Text, UniqueConstraint, text
from sqlalchemy.orm import Mapped, mapped_column
from app.db.base import Base, UTCDateTime, new_id, utcnow
from app.db.base import Base, PortableJSON, UTCDateTime, new_id, utcnow
class SourceObject(Base):
@@ -92,7 +92,8 @@ class IntrospectJob(Base):
__table_args__ = (
Index("ix_introspect_jobs_source_created", "source_id", "created_at"),
Index("ux_introspect_jobs_active", "source_id", unique=True,
sqlite_where=text("status IN ('queued', 'running')")),
sqlite_where=text("status IN ('queued', 'running')"),
postgresql_where=text("status IN ('queued', 'running')")),
)
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
@@ -219,8 +220,8 @@ class SourceChange(Base):
seen_count: Mapped[int] = mapped_column(Integer, default=1)
detected_by: Mapped[dict[str, Any]] = mapped_column(default=dict)
basis: Mapped[dict[str, Any] | None] = mapped_column(default=None)
before: Mapped[Any] = mapped_column(JSON, nullable=True, default=None)
after: Mapped[Any] = mapped_column(JSON, nullable=True, default=None)
before: Mapped[Any] = mapped_column(PortableJSON, nullable=True, default=None)
after: Mapped[Any] = mapped_column(PortableJSON, nullable=True, default=None)
affects: Mapped[dict[str, Any] | None] = mapped_column(default=None)
struct_version_before: Mapped[int | None] = mapped_column(Integer, default=None)
struct_version_after: Mapped[int | None] = mapped_column(Integer, default=None)
+235
View File
@@ -0,0 +1,235 @@
"""应用库上 SQLite、PostgreSQL 写法不一样的那几样,收成按方言编译的表达式。
只给应用库(agentlab.db / AGENTLAB_DATABASE_URL)用;发给业务数据源的 SQL 不走这里。
**按 JSON 列里的键筛选。** SQLite 用 json_extract:取出来是 SQL 标量(字符串是文本、数字是数字、true 是 1、
没有这个键或值为 null 是 NULL),和 Python 值比是带类型的比较(数字 3 不等于字符串 "3")。PostgreSQL 的 JSON 列是
存原文的 json(db/base.PortableJSON),用 -> / ->> 取,要比较的只把取出来的那一小段转成 JSONB:
- json_eq:PostgreSQL 上按 JSONB 相等比(`(col -> 'k')::jsonb = '3'::jsonb`),同样带类型,和 SQLite 一致;
- json_text:只要文本(字符串原样、数字是它的写法),PostgreSQL 上是 ->>;
- json_flag:标记位「真」(true、非零的数):没有这个键、null、false、0 都算假;
- json_is_string:这个键的值是不是字符串(SQLite 的 json_type 叫 text,PostgreSQL 的 jsonb_typeof 叫 string)。
键名只收标识符(字母、数字、下划线),直接写进 SQL:键都是代码里的常量,写进去语句缓存也按它区分。
**插入,撞了唯一键就什么都不做。** insert_ignore 按会话连着的库挑 sqlite / postgresql 的 insert,两边的
on_conflict_do_nothing 是同一个接口。
**撞的是哪个唯一约束。** unique_violation 认 IntegrityError:SQLite 的报错是「UNIQUE constraint failed: 表.列」,
PostgreSQL(asyncpg)的是 23505,表名、DETAIL 里的「Key (列)=」分别带着。不靠约束名(命名规则改了也认得出)。
"""
from __future__ import annotations
import json
import re
from typing import Any
from sqlalchemy import Boolean, String, bindparam, literal_column
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.functions import FunctionElement
from sqlalchemy.types import TypeDecorator
_KEY = re.compile(r"[A-Za-z_][A-Za-z0-9_]*\Z")
def _key(name: str) -> Any:
if not _KEY.match(name):
raise ValueError(f"JSON 键名只能是标识符:{name!r}")
return literal_column(name)
class _JsonScalar(TypeDecorator):
"""json_eq 右边的值:SQLite 原样绑定(和 json_extract 取出的标量比),PostgreSQL 绑定成 JSON 文本再转 JSONB。"""
impl = String
cache_ok = True
def process_bind_param(self, value: Any, dialect: Any) -> Any:
if dialect.name == "postgresql":
return json.dumps(value, ensure_ascii=False)
return value
class json_text(FunctionElement[str]):
"""JSON 列顶层键的值,当文本用。"""
type = String()
inherit_cache = True
name = "json_text"
def __init__(self, column: Any, key: str) -> None:
super().__init__(column, _key(key))
class _JsonEq(FunctionElement[bool]):
type = Boolean()
inherit_cache = True
name = "json_eq"
def __init__(self, column: Any, key: str, value: Any) -> None:
super().__init__(column, _key(key), bindparam(None, value, type_=_JsonScalar()))
class _JsonNull(FunctionElement[bool]):
type = Boolean()
inherit_cache = True
name = "json_null"
def __init__(self, column: Any, key: str) -> None:
super().__init__(column, _key(key))
def json_eq(column: Any, key: str, value: Any) -> Any:
"""JSON 列顶层键的值等于 value(带类型的比较,见模块说明)。value 为 None 时是「没有这个键或值为 null」。
None 用另一个表达式:语句缓存不按绑定参数的值区分,同一个表达式不能因为值是 None 编译成两样。"""
return _JsonNull(column, key) if value is None else _JsonEq(column, key, value)
class json_flag(FunctionElement[bool]):
"""JSON 列顶层键是「真」的标记位:true 或非零的数(字符串也算真,同 SQLite)。没有这个键、null、false、0 是假。"""
type = Boolean()
inherit_cache = True
name = "json_flag"
def __init__(self, column: Any, key: str) -> None:
super().__init__(column, _key(key))
class json_is_string(FunctionElement[bool]):
"""JSON 列顶层键的值是字符串。"""
type = Boolean()
inherit_cache = True
name = "json_is_string"
def __init__(self, column: Any, key: str) -> None:
super().__init__(column, _key(key))
def _parts(element: Any, compiler: Any, **kw: Any) -> tuple[str, str, list[Any]]:
column, key, *rest = list(element.clauses)
return compiler.process(column, **kw), key.name, rest
# ---- SQLite(缺省)----
@compiles(json_text)
def _json_text_sqlite(element: Any, compiler: Any, **kw: Any) -> str:
col, key, _ = _parts(element, compiler, **kw)
return f"json_extract({col}, '$.{key}')"
@compiles(_JsonEq)
def _json_eq_sqlite(element: Any, compiler: Any, **kw: Any) -> str:
col, key, (value,) = _parts(element, compiler, **kw)
return f"(json_extract({col}, '$.{key}') = {compiler.process(value, **kw)})"
@compiles(_JsonNull)
def _json_null_sqlite(element: Any, compiler: Any, **kw: Any) -> str:
col, key, _ = _parts(element, compiler, **kw)
return f"(json_extract({col}, '$.{key}') IS NULL)"
@compiles(json_flag)
def _json_flag_sqlite(element: Any, compiler: Any, **kw: Any) -> str:
col, key, _ = _parts(element, compiler, **kw)
return f"(coalesce(json_extract({col}, '$.{key}'), 0) != 0)"
@compiles(json_is_string)
def _json_is_string_sqlite(element: Any, compiler: Any, **kw: Any) -> str:
col, key, _ = _parts(element, compiler, **kw)
return f"(json_type({col}, '$.{key}') = 'text')"
# ---- PostgreSQL(应用库的 JSON 列是 json)----
def _got(col: str, key: str) -> str:
"""取出的那一段转成 JSONB:json 没有相等运算符。列本身是 JSONB 也照样成立。"""
return f"CAST(({col} -> '{key}') AS JSONB)"
@compiles(json_text, "postgresql")
def _json_text_pg(element: Any, compiler: Any, **kw: Any) -> str:
col, key, _ = _parts(element, compiler, **kw)
return f"({col} ->> '{key}')"
@compiles(_JsonEq, "postgresql")
def _json_eq_pg(element: Any, compiler: Any, **kw: Any) -> str:
col, key, (value,) = _parts(element, compiler, **kw)
return f"({_got(col, key)} = CAST({compiler.process(value, **kw)} AS JSONB))"
@compiles(_JsonNull, "postgresql")
def _json_null_pg(element: Any, compiler: Any, **kw: Any) -> str:
# json_extract 对「没有这个键」和「值为 null」都给 NULL;-> 对后者给的是 JSON 的 null
col, key, _ = _parts(element, compiler, **kw)
return f"(coalesce({_got(col, key)}, 'null'::jsonb) = 'null'::jsonb)"
@compiles(json_flag, "postgresql")
def _json_flag_pg(element: Any, compiler: Any, **kw: Any) -> str:
col, key, _ = _parts(element, compiler, **kw)
return f"(coalesce({_got(col, key)}, 'null'::jsonb) NOT IN ('null'::jsonb, 'false'::jsonb, '0'::jsonb))"
@compiles(json_is_string, "postgresql")
def _json_is_string_pg(element: Any, compiler: Any, **kw: Any) -> str:
col, key, _ = _parts(element, compiler, **kw)
return f"(jsonb_typeof({_got(col, key)}) = 'string')"
# ---- 插入,撞了唯一键就跳过 ----
def dialect_of(session: Any) -> str:
"""会话(AsyncSession / Session)连着的库的方言名。"""
return session.get_bind().dialect.name
def insert_ignore(session: Any, table: Any) -> Any:
"""按会话连着的库挑方言的 insert,调用方接着 .values(...).on_conflict_do_nothing(index_elements=[...])。"""
if dialect_of(session) == "postgresql":
from sqlalchemy.dialects.postgresql import insert
else:
from sqlalchemy.dialects.sqlite import insert
return insert(table)
# ---- 撞的是哪个唯一约束 ----
_SQLITE_UNIQUE = "UNIQUE constraint failed:"
_PG_KEY = re.compile(r"Key \(([^)]*)\)=")
def _driver_error(error: BaseException) -> Any:
"""SQLAlchemy 的异常 → asyncpg 自己的异常(在 orig 的 __cause__ 上;orig 只抄了 sqlstate,没有表名和 DETAIL)。"""
seen: Any = getattr(error, "orig", None) or error
for _ in range(4):
if getattr(seen, "sqlstate", None) and hasattr(seen, "table_name"):
return seen
seen = getattr(seen, "__cause__", None)
if seen is None:
break
return None
def unique_violation(error: BaseException, table: str, *columns: str) -> bool:
"""这个 IntegrityError 是不是撞了 table 上正好这几列的唯一约束(主键、唯一索引都算)。"""
text = str(getattr(error, "orig", None) or error)
if _SQLITE_UNIQUE in text:
failed = text.split(_SQLITE_UNIQUE, 1)[1].splitlines()[0]
return {part.strip() for part in failed.split(",")} == {f"{table}.{c}" for c in columns}
cause = _driver_error(error)
if cause is None or getattr(cause, "sqlstate", None) != "23505" or getattr(cause, "table_name", None) != table:
return False
match = _PG_KEY.search(getattr(cause, "detail", None) or "")
return bool(match) and [c.strip().strip('"') for c in match.group(1).split(",")] == list(columns)
+76
View File
@@ -0,0 +1,76 @@
"""应用库是 PostgreSQL 时,让它在 SQLite 上写成的那些假设下照样成立。
只装在应用库那个引擎的方言实例上(db/base.make_engine),业务数据源的 PostgreSQL 引擎不受影响。
**建表的列类型。** 应用是在 SQLite 上写成的:SQLite 不查 VARCHAR 的长度,INTEGER 是 64 位。PostgreSQL 上
VARCHAR(n) 超长直接报错、INTEGER 只有 32 位。模型里的 String(n) 是写给人看的「大致多长」,从来没有被库约束过:
逐列核对只核得到测试走到的值,漏一处就是线上的写入失败,从 SQLite 导过来的存量数据里也可能已经有超长的。所以
应用库上 String(n) 建成不限长的 VARCHAR(PostgreSQL 里 varchar、varchar(n)、text 存储和性能一样,限长只多一次
检查)。CAST(x AS VARCHAR(n)) 这种查询里的写法不动——那是截断的意思。Integer 列在 PostgreSQL 上是 BIGINT:
那是在模型这一层换的(db/base._portable_integer),绑定参数的类型转换跟着列走,大数不在半路溢出。
**ORDER BY 里 NULL 的位置。** SQLite 升序 NULL 在前、降序 NULL 在后,PostgreSQL 正好相反。应用里两百多处排序,
可空的列(开始时间、到期时间、发布时间……)排出来的先后要和 SQLite 一致:可空列上没写 nulls_first / nulls_last
的 ASC 补 NULLS FIRST、DESC 补 NULLS LAST,ORDER BY 里直接写列名(隐含 ASC)的同样补上。不可空的列不动——
补了会让索引用不上,又没有 NULL 可排。窗口函数 OVER (ORDER BY …) 里同样补。原生 text() 里写的 ORDER BY 不经过
这里,要自己写明(db/base 的去重语句里就写了)。
"""
from __future__ import annotations
from typing import Any
from sqlalchemy import Column
from sqlalchemy.dialects.postgresql.asyncpg import PGCompiler_asyncpg
from sqlalchemy.dialects.postgresql.base import PGTypeCompiler
from sqlalchemy.sql import operators
from sqlalchemy.sql.elements import ClauseList, UnaryExpression
#: 外层已经写了 nulls_first / nulls_last:里面那一层 ASC / DESC 不再补
_EXPLICIT = "_agentlab_explicit_nulls"
def _nullable(element: Any) -> bool:
return isinstance(element, Column) and bool(element.nullable) and not element.primary_key
class AppTypeCompiler(PGTypeCompiler):
def visit_string(self, type_: Any, **kw: Any) -> str:
if type_.length and isinstance(kw.get("type_expression"), Column):
return "VARCHAR"
return super().visit_string(type_, **kw)
def _ordered(clauses: Any) -> list[Any]:
"""ORDER BY 里直接写的可空列补成 ASC(visit_unary 再给它补 NULLS FIRST)。"""
return [c.asc() if _nullable(c) else c for c in clauses]
class AppCompiler(PGCompiler_asyncpg):
def order_by_clause(self, select: Any, **kw: Any) -> str:
order_by = self._generate_delimited_list(_ordered(select._order_by_clauses), ", ", **kw)
return " ORDER BY " + order_by if order_by else ""
def visit_over(self, over: Any, **kw: Any) -> str:
clauses = list(over.order_by) if over.order_by is not None else []
if any(_nullable(c) for c in clauses):
# 在副本上换:语句对象可能被缓存、被别处共用,不能改原件
over = over._clone()
over.order_by = ClauseList(*_ordered(clauses))
return super().visit_over(over, **kw)
def visit_unary(self, unary: UnaryExpression, **kw: Any) -> str:
modifier = unary.modifier
if modifier in (operators.nulls_first_op, operators.nulls_last_op):
return super().visit_unary(unary, **{**kw, _EXPLICIT: True})
explicit = kw.pop(_EXPLICIT, False)
text = super().visit_unary(unary, **kw)
if not explicit and modifier in (operators.asc_op, operators.desc_op) and _nullable(unary.element):
text += " NULLS FIRST" if modifier is operators.asc_op else " NULLS LAST"
return text
def install(dialect: Any) -> None:
"""装到应用库引擎的方言实例上。"""
dialect.statement_compiler = AppCompiler
dialect.type_compiler_instance = dialect.type_compiler = AppTypeCompiler(dialect)
+57
View File
@@ -0,0 +1,57 @@
"""应用库、断点库的地址:规范写法、换算成 psycopg 用的连接串、写进日志的样子(不带密码)。
不依赖 app.core.config:配置的校验器要调这里,这里再导 config 就成了循环导入。
"""
from __future__ import annotations
from sqlalchemy.engine import URL, make_url
from sqlalchemy.exc import ArgumentError
#: 应用库认的写法。asyncpg 是 SQLAlchemy 这边的驱动;postgresql:// / postgres:// 是常见的通用写法,一律换成 asyncpg
_APP_SCHEMES = {"postgresql+asyncpg", "postgresql", "postgres"}
#: 断点库认的写法:psycopg 只认 libpq 的 postgresql:// / postgres://;带驱动名的 SQLAlchemy 写法也收,去掉驱动名
_CHECKPOINT_SCHEMES = {"postgresql", "postgres", "postgresql+psycopg", "postgresql+asyncpg"}
#: asyncpg 和 libpq 对 SSL 的参数名不一样:asyncpg 是 ssl=require,libpq 是 sslmode=require(取值同一套)
_ASYNCPG_TO_LIBPQ = {"ssl": "sslmode"}
def _parse(raw: str, schemes: set[str], example: str) -> URL:
try:
url = make_url(raw)
except ArgumentError:
raise ValueError(f"认不出这个地址,应当写成 {example}") from None
if url.drivername not in schemes:
raise ValueError(f"只认 PostgreSQL({example}),不认 {url.drivername}://;用 SQLite 就不要设这一项")
if not url.database:
raise ValueError(f"地址里要写库名,例如 {example}")
return url
def normalize_database_url(raw: str) -> str:
"""AGENTLAB_DATABASE_URL → postgresql+asyncpg://…(密码原样保留)。认不出的写法抛 ValueError。"""
url = _parse(raw, _APP_SCHEMES, "postgresql+asyncpg://用户:密码@主机:5432/库名")
return url.set(drivername="postgresql+asyncpg").render_as_string(hide_password=False)
def normalize_checkpoint_url(raw: str) -> str:
"""AGENTLAB_CHECKPOINT_URL → libpq 认的 postgresql://…。认不出的写法抛 ValueError。"""
url = _parse(raw, _CHECKPOINT_SCHEMES, "postgresql://用户:密码@主机:5432/库名")
return libpq_conninfo(url.render_as_string(hide_password=False))
def libpq_conninfo(database_url: str) -> str:
"""SQLAlchemy 的地址 → psycopg 的连接串:去掉驱动名,asyncpg 写法的参数换成 libpq 的。"""
url = make_url(database_url)
query = {_ASYNCPG_TO_LIBPQ.get(k, k): v for k, v in url.query.items()}
return url.set(drivername="postgresql", query=query).render_as_string(hide_password=False)
def describe(database_url: str) -> str:
"""写进日志的样子:主机、端口、库名、用户,不写密码。"""
url = make_url(database_url)
host = url.host or "本机套接字"
port = f":{url.port}" if url.port else ""
user = f",用户 {url.username}" if url.username else ""
return f"{host}{port}/{url.database}{user}"