From eae01901845829fefa8195741dc73ecc6d9f50d9 Mon Sep 17 00:00:00 2001 From: 1445043649 <> Date: Sun, 10 May 2026 22:55:19 +0800 Subject: [PATCH] feat(wizard): add Postgres backend question to setup wizard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- scripts/setup_wizard.py | 30 ++++++++++++++++++++++++++++++ scripts/wizard/writer.py | 17 ++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/scripts/setup_wizard.py b/scripts/setup_wizard.py index 647d90a0..04b62b97 100644 --- a/scripts/setup_wizard.py +++ b/scripts/setup_wizard.py @@ -76,6 +76,33 @@ def main() -> int: 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") write_config_yaml( @@ -97,6 +124,7 @@ def main() -> int: allow_host_bash=execution.allow_host_bash, include_bash_tool=execution.include_bash_tool, include_write_tools=execution.include_write_tools, + database_backend=database_backend, ) 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 if fetch_api_key and fetch_provider and fetch_provider.env_var: env_pairs[fetch_provider.env_var] = fetch_api_key + if database_url: + env_pairs["DATABASE_URL"] = database_url if env_pairs: write_env_file(env_path, env_pairs) diff --git a/scripts/wizard/writer.py b/scripts/wizard/writer.py index e2324340..19fc20aa 100644 --- a/scripts/wizard/writer.py +++ b/scripts/wizard/writer.py @@ -172,8 +172,14 @@ def build_minimal_config( include_write_tools: bool = True, config_version: int = 5, base_config: dict[str, Any] | None = None, + database_backend: str = "sqlite", ) -> 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 today = date.today().isoformat() @@ -220,6 +226,13 @@ def build_minimal_config( sandbox_config.pop("allow_host_bash", None) 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 = ( f"# DeerFlow Configuration\n" f"# Generated by 'make setup' on {today}\n" @@ -250,6 +263,7 @@ def write_config_yaml( allow_host_bash: bool = False, include_bash_tool: bool = False, include_write_tools: bool = True, + database_backend: str = "sqlite", ) -> None: """Write (or overwrite) config.yaml with a minimal working configuration.""" # Read config_version from config.example.yaml if present @@ -286,5 +300,6 @@ def write_config_yaml( include_write_tools=include_write_tools, config_version=config_version, base_config=example_defaults, + database_backend=database_backend, ) config_path.write_text(content, encoding="utf-8")