deer-flow/backend/tests/test_persistence_bootstrap_url.py
Ryker_Feng 063d62c3c3
feat(persistence): support custom postgres schema (#3442)
* feat(persistence): support custom postgres schema

* fix(persistence): address CI lint/test failures and review feedback

- Map missing psycopg import to actionable POSTGRES_INSTALL guidance in
  sync/async schema-creation helpers
- Accept SQLAlchemy compound DSN schemes (postgresql+asyncpg) when
  injecting search_path, normalizing to a libpq-consumable DSN
- Guard keyword-DSN tests with importorskip so they skip without psycopg
- Set database=None in sync checkpointer none-fix test to avoid MagicMock
  backend resolution
- Apply ruff import sort and format

* fix(persistence): address pg-schema review feedback

- Restrict postgres_schema regex to lowercase-only so the quoted CREATE
  SCHEMA matches the unquoted search_path (PG case-folds it), fixing the
  mixed-case bug where tables silently fell back to public.
- Replace shlex.join/split with libpq-correct backslash escaping for the
  options parameter so values containing spaces survive intact.
- Add normalize_libpq_dsn() and route the async checkpointer pool through
  dsn_with_search_path() so a +asyncpg suffix is stripped and existing DSN
  options (e.g. statement_timeout) are merged instead of overridden.
- Extract shared ensure_postgres_schema()/ensure_postgres_schema_async()
  helpers (mapping missing psycopg to the install hint) used by all four
  provider sites.
- Tests: reject mixed-case schemas, preserve space-containing libpq option,
  cover normalize_libpq_dsn, and assert pool search_path via DSN.

* fix(persistence): align pg-schema test with merged store API

The main merge moved the sync Store factory to the single-path
_resolve_store_config/_sync_store_cm design, dropping the PR's
_sync_store_from_database helper. The integration test still imported
the removed symbol, breaking test collection (backend-unit-tests).
Resolve the store config from a DatabaseConfig and drive it through
_sync_store_cm instead.

* fix(persistence): address pg-schema review feedback

- reject trailing/leading whitespace in postgres_schema via re.fullmatch
  (a $-anchored re.match let "deerflow\n" through, silently landing tables
  in public)
- re-escape all whitespace (TAB/CR/LF) when re-joining libpq options so a
  caller's pre-existing options value round-trips losslessly
- re-validate the identifier inside create_schema_sql as defense-in-depth
  at the SQL-emitting boundary
- accept the postgres:// short scheme in the alembic search_path injection
- close the sync psycopg connection explicitly (psycopg3 __exit__ does not
  close()), mirroring the async path
- drop the partial checkpointer/store reset on a database config change;
  database is restart-required and the ORM engine is not rebuilt, so a
  partial reset would half-migrate the deployment

* docs(config): complete the postgres_schema migration checklist

Address PR review (P1): the documented `public`->schema migration only
moved runs, run_events, threads_meta, feedback, and users. That strands
every other DeerFlow-owned table -- the four channel_* tables, both
scheduled_* tables, agents, and (critically) alembic_version -- in
`public`. On restart bootstrap treats the partially-populated target
schema as unversioned, re-baselines it, and replays migrations while the
real rows stay invisible in `public`.

List the full owned set explicitly, call out alembic_version as required,
and keep the "discover the rest" query for version-drift safety.

* refactor(checkpointer): drop test-only _sync_checkpointer_from_database

Address PR review: the helper was only reached by the env-gated
integration test and re-implemented the DatabaseConfig->CheckpointerConfig
backend resolution that _resolve_checkpointer_config already owns, so a
future backend added there would silently miss this path. Mirror the store
side of the same test, which reuses the production path directly:
_resolve_checkpointer_config(...) + _sync_checkpointer_cm(...).
2026-07-30 13:51:17 +08:00

103 lines
4.9 KiB
Python

"""Tests for the Postgres URL / ConfigParser pitfalls in ``bootstrap``.
Two failure modes the ``_alembic_safe_url`` helper exists to prevent:
1. ``str(engine.url)`` (and the default ``URL.render_as_string()``) masks the
password as ``***``. The live engine would still work because it carries
the password in-memory, but alembic ``stamp`` / ``upgrade`` (which open
their own connection from the URL we pass in) would authenticate with
garbage and fail at runtime.
2. ``alembic.config.Config.set_main_option`` forwards to ``ConfigParser.set``,
which performs ``%(name)s``-style interpolation on the value. A URL-encoded
password containing ``%`` (e.g. ``p%40ss`` for ``p@ss``) raises
``InterpolationSyntaxError``. Every literal ``%`` must be doubled.
"""
from __future__ import annotations
from types import SimpleNamespace
from sqlalchemy.engine.url import make_url
from deerflow.persistence.bootstrap import _alembic_safe_url, _escape_url_for_alembic, _get_alembic_config
def _fake_engine(url: str) -> SimpleNamespace:
"""Build a minimal stand-in for ``AsyncEngine`` so we don't need a real
driver (e.g. asyncpg) installed just to exercise the URL path."""
return SimpleNamespace(url=make_url(url))
def test_safe_url_preserves_password_for_postgres() -> None:
engine = _fake_engine("postgresql://alice:s3cret@db.example.com/app")
safe = _alembic_safe_url(engine)
assert "s3cret" in safe, "password got masked: alembic would auth with garbage"
assert "***" not in safe
def test_safe_url_escapes_percent_for_configparser() -> None:
# URL-encoded ``@`` in password -> raw ``%40`` in URL -> ConfigParser
# would treat it as an interpolation marker.
engine = _fake_engine("postgresql://alice:p%40ss@db.example.com/app")
safe = _alembic_safe_url(engine)
assert "p%%40ss" in safe, f"percent not doubled, ConfigParser will fail: {safe}"
def test_alembic_config_accepts_url_with_percent_and_round_trips() -> None:
# The whole point: build_config should not raise, and the URL alembic
# reads back should match the original (single ``%``, real password).
original = "postgresql://alice:p%40ss@db.example.com/app"
engine = _fake_engine(original)
cfg = _get_alembic_config(engine)
roundtrip = cfg.get_main_option("sqlalchemy.url")
assert roundtrip == original, f"alembic sees a different URL than we set: {roundtrip}"
def test_sqlite_url_does_not_double_percent_unnecessarily() -> None:
# No percent in the URL -> no escaping needed -> output equals input.
engine = _fake_engine("sqlite+aiosqlite:///tmp/db.sqlite")
safe = _alembic_safe_url(engine)
assert safe == "sqlite+aiosqlite:///tmp/db.sqlite"
def test_escape_url_for_alembic_doubles_only_percent_signs() -> None:
# Shared helper used by both ``bootstrap._alembic_safe_url`` and
# ``scripts/_autogen_revision._alembic_config`` -- pins the round-trip
# rule so any future URL/ConfigParser corner case is fixed in one place.
assert _escape_url_for_alembic("postgresql://a:p%40ss@h/d") == "postgresql://a:p%%40ss@h/d"
assert _escape_url_for_alembic("sqlite:///x.db") == "sqlite:///x.db"
# Idempotency is intentionally NOT a property -- doubling is one-way;
# callers must escape exactly once on the way into set_main_option.
assert _escape_url_for_alembic("a%%b") == "a%%%%b"
def test_alembic_config_forwards_postgres_schema_option() -> None:
# The custom schema must reach env.py so the alembic-spawned engine can
# pin its search_path; otherwise alembic_version + migration DDL land in
# ``public`` while the ORM tables land in the custom schema.
engine = _fake_engine("postgresql://a:b@h/d")
cfg = _get_alembic_config(engine, postgres_schema="deerflow")
assert cfg.get_main_option("deerflow_pg_schema") == "deerflow"
def test_alembic_config_omits_schema_option_when_unset() -> None:
engine = _fake_engine("postgresql://a:b@h/d")
cfg = _get_alembic_config(engine)
assert cfg.get_main_option("deerflow_pg_schema") is None
def test_env_module_pins_search_path_from_schema_option() -> None:
"""env.py must read ``deerflow_pg_schema`` and pin the alembic-spawned
engine's search_path. That engine is built from the bare URL and does not
inherit the app engine's asyncpg ``server_settings``, so without this both
``alembic_version`` and migration DDL would land in ``public`` while the
ORM tables land in the custom schema. Source-parity check mirroring
``test_env_module_wires_busy_timeout_for_sqlite``.
"""
from pathlib import Path
env_path = Path(__file__).resolve().parents[1] / "packages/harness/deerflow/persistence/migrations/env.py"
src = env_path.read_text(encoding="utf-8")
assert 'get_main_option("deerflow_pg_schema")' in src, "env.py must read the deerflow_pg_schema option set by _get_alembic_config"
assert "build_asyncpg_connect_args" in src, "env.py must pin the alembic engine's search_path via build_asyncpg_connect_args"