From 5e9413e080b82741e7e3992e534e5e93863d40e6 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Fri, 16 Oct 2026 17:11:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A6=E6=9C=AC=EF=BC=9A=E6=8C=89=E6=9D=A5?= =?UTF-8?q?=E6=BA=90=E8=BF=90=E8=A1=8C=E6=89=BE=E5=87=BA=E5=85=B7=E8=BF=87?= =?UTF-8?q?=E7=9A=84=E6=AD=A3=E5=BC=8F=E8=BF=90=E8=A1=8C=E3=80=81=E5=AF=B9?= =?UTF-8?q?=E8=BF=87=E7=9A=84=E8=B4=A6=E6=94=B9=E5=9C=A8=20SQL=20=E9=87=8C?= =?UTF-8?q?=E6=8C=89=20origin.source=5Frun=20=E7=AD=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为什么:GET 账本每次都要列这两样,原来把全部 ledger_issue / reconcile 运行读出来在 Python 里筛,出具多了就是整表扫。改成 origin_kind(有索引)加 json_extract(origin, "$.source_run"),写法同 auth/keys 认幂等键。 --- backend/app/ledger/issue.py | 9 ++++++--- backend/app/ledger/reconcile.py | 8 ++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/app/ledger/issue.py b/backend/app/ledger/issue.py index 43815e09..b56ed9f1 100644 --- a/backend/app/ledger/issue.py +++ b/backend/app/ledger/issue.py @@ -144,9 +144,12 @@ async def issued_today(session: Any) -> int: async def issued_runs(session: Any, run_id: str) -> list[Run]: - """这次运行按口径出具过的正式运行(origin.source_run 是它),新的在前。origin_kind 有索引,先按它筛再看 origin。""" - rows = (await session.execute(select(Run).where(Run.origin_kind == ORIGIN).order_by(Run.created_at.desc()))).scalars() - return [r for r in rows if isinstance(r.origin, dict) and r.origin.get("source_run") == run_id] + """这次运行按口径出具过的正式运行(origin.source_run 是它),新的在前。origin_kind 有索引,先按它筛再看 origin + (json_extract,同 auth/keys 认幂等键的写法)。""" + rows = (await session.execute(select(Run).where( + Run.origin_kind == ORIGIN, func.json_extract(Run.origin, "$.source_run") == run_id) + .order_by(Run.created_at.desc()))).scalars() + return list(rows) def issued_view(run: Run) -> dict[str, Any]: diff --git a/backend/app/ledger/reconcile.py b/backend/app/ledger/reconcile.py index 4e9daf26..722573bb 100644 --- a/backend/app/ledger/reconcile.py +++ b/backend/app/ledger/reconcile.py @@ -131,8 +131,12 @@ def actions_of(content: dict[str, Any], names: dict[str, str], hide: Any = None) async def reconciled_runs(session: Any, run_id: str) -> list[Run]: """对这次运行对过的账(origin.source_run 是它),新的在前。""" - rows = (await session.execute(select(Run).where(Run.origin_kind == ORIGIN).order_by(Run.created_at.desc()))).scalars() - return [r for r in rows if isinstance(r.origin, dict) and r.origin.get("source_run") == run_id] + from sqlalchemy import func + + rows = (await session.execute(select(Run).where( + Run.origin_kind == ORIGIN, func.json_extract(Run.origin, "$.source_run") == run_id) + .order_by(Run.created_at.desc()))).scalars() + return list(rows) def reconciled_view(run: Run) -> dict[str, Any]: