feat(wizard): add Postgres backend question to setup wizard

setup_wizard.py 在 Step 3 (Execution) 之后新增一个 Database 问答:
"Use Postgres? (y = postgres, n = sqlite)",默认 y(Stage 0+ 推荐 PG)。
选 y 时引导填 DATABASE_URL(可留空稍后写 .env);DATABASE_URL 进 .env,
config.yaml 写入 database.backend=postgres + postgres_url=\$DATABASE_URL。

writer.py build_minimal_config 加 database_backend 参数,postgres 时
覆盖 base_config 的 database 段;默认 sqlite 时沿用 base_config 行为
(继承 config.example.yaml 的 sqlite_dir 等)。

minimal pattern:不新建 wizard/steps/database.py,inline 在 main 里加 1
问答 + writer 加 1 参数。后续如果需要更复杂数据库选项再升为完整 step 模块。

Stage 0 PR1 T1.8.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1445043649
2026-05-10 22:55:19 +08:00
parent e6f5ba53bd
commit eae0190184
2 changed files with 46 additions and 1 deletions
+30
View File
@@ -76,6 +76,33 @@ def main() -> int:
execution = run_execution_step(f"Step 3/{total_steps}") execution = run_execution_step(f"Step 3/{total_steps}")
# Database backend (Stage 0+ recommends Postgres; SQLite kept as
# offline dev fallback). Inline question rather than a separate
# wizard step — minimal addition, full step module can come later.
print()
print_header("Database backend")
print("Stage 0+ recommends Postgres for parity with production.")
print("SQLite is kept as an offline dev fallback.")
print()
use_postgres = ask_yes_no("Use Postgres? (y = postgres, n = sqlite)", default=True)
database_backend = "postgres" if use_postgres else "sqlite"
database_url: str | None = None
if use_postgres:
print()
print_info(
"Set DATABASE_URL in your .env file. Example:\n"
" postgresql+asyncpg://deerflow:deerflow_dev@localhost:5432/deerflow\n"
"Or for a remote RDS:\n"
" postgresql+asyncpg://USER:PASS@HOST:5432/DB"
)
print()
try:
raw = input("DATABASE_URL (leave blank to set later in .env): ").strip()
except EOFError:
raw = ""
if raw:
database_url = raw
print_header(f"Step {total_steps}/{total_steps} · Writing configuration") print_header(f"Step {total_steps}/{total_steps} · Writing configuration")
write_config_yaml( write_config_yaml(
@@ -97,6 +124,7 @@ def main() -> int:
allow_host_bash=execution.allow_host_bash, allow_host_bash=execution.allow_host_bash,
include_bash_tool=execution.include_bash_tool, include_bash_tool=execution.include_bash_tool,
include_write_tools=execution.include_write_tools, include_write_tools=execution.include_write_tools,
database_backend=database_backend,
) )
print_success(f"Config written to: {config_path.relative_to(project_root)}") print_success(f"Config written to: {config_path.relative_to(project_root)}")
@@ -113,6 +141,8 @@ def main() -> int:
env_pairs[search_provider.env_var] = search_api_key env_pairs[search_provider.env_var] = search_api_key
if fetch_api_key and fetch_provider and fetch_provider.env_var: if fetch_api_key and fetch_provider and fetch_provider.env_var:
env_pairs[fetch_provider.env_var] = fetch_api_key env_pairs[fetch_provider.env_var] = fetch_api_key
if database_url:
env_pairs["DATABASE_URL"] = database_url
if env_pairs: if env_pairs:
write_env_file(env_path, env_pairs) write_env_file(env_path, env_pairs)
+16 -1
View File
@@ -172,8 +172,14 @@ def build_minimal_config(
include_write_tools: bool = True, include_write_tools: bool = True,
config_version: int = 5, config_version: int = 5,
base_config: dict[str, Any] | None = None, base_config: dict[str, Any] | None = None,
database_backend: str = "sqlite",
) -> str: ) -> str:
"""Build the content of a minimal config.yaml.""" """Build the content of a minimal config.yaml.
``database_backend``: ``"sqlite"`` (default; reuses base_config's sqlite_dir)
or ``"postgres"`` (writes ``backend: postgres`` + ``postgres_url: $DATABASE_URL``;
``DATABASE_URL`` must be set in ``.env``).
"""
from datetime import date from datetime import date
today = date.today().isoformat() today = date.today().isoformat()
@@ -220,6 +226,13 @@ def build_minimal_config(
sandbox_config.pop("allow_host_bash", None) sandbox_config.pop("allow_host_bash", None)
data["sandbox"] = sandbox_config data["sandbox"] = sandbox_config
# Override database backend if user chose postgres in the wizard.
if database_backend == "postgres":
data["database"] = {
"backend": "postgres",
"postgres_url": "$DATABASE_URL",
}
header = ( header = (
f"# DeerFlow Configuration\n" f"# DeerFlow Configuration\n"
f"# Generated by 'make setup' on {today}\n" f"# Generated by 'make setup' on {today}\n"
@@ -250,6 +263,7 @@ def write_config_yaml(
allow_host_bash: bool = False, allow_host_bash: bool = False,
include_bash_tool: bool = False, include_bash_tool: bool = False,
include_write_tools: bool = True, include_write_tools: bool = True,
database_backend: str = "sqlite",
) -> None: ) -> None:
"""Write (or overwrite) config.yaml with a minimal working configuration.""" """Write (or overwrite) config.yaml with a minimal working configuration."""
# Read config_version from config.example.yaml if present # Read config_version from config.example.yaml if present
@@ -286,5 +300,6 @@ def write_config_yaml(
include_write_tools=include_write_tools, include_write_tools=include_write_tools,
config_version=config_version, config_version=config_version,
base_config=example_defaults, base_config=example_defaults,
database_backend=database_backend,
) )
config_path.write_text(content, encoding="utf-8") config_path.write_text(content, encoding="utf-8")