{"record":{"id":"79c07c58eb43c516","repo":"block/buzz","slug":"connect-to-test-db","errorCode":null,"errorMessage":"connect to test DB","messagePattern":"connect to test DB","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/buzz-db/src/store/community.rs","lineNumber":567,"sourceCode":"mod tests {\n    //! Pin the load-bearing contract for `Db::communities_of_channels`:\n    //! a channel id that does NOT exist MUST be absent from the result\n    //! map, never mapped to a default. The relay-side read-row emitter\n    //! relies on this — a missing entry triggers `MissingLookup →\n    //! ImplBug{row_community_lookup_missing} → CoverageBreach`. If this\n    //! helper ever started returning a default/zero entry for unknown\n    //! channels, that fail-closed chain would go blind.\n    use super::*;\n    use sqlx::PgPool;\n\n    const TEST_DB_URL: &str = \"postgres://buzz:buzz_dev@localhost:5432/buzz\"; // sadscan:disable np.postgres.1\n\n    async fn setup_db() -> Db {\n        let database_url =\n            std::env::var(\"TEST_DATABASE_URL\").unwrap_or_else(|_| TEST_DB_URL.into());\n        let pool = PgPool::connect(&database_url)\n            .await\n            .expect(\"connect to test DB\");\n        Db::from_pool(pool)\n    }\n\n    async fn make_community(pool: &PgPool) -> Uuid {\n        let id = Uuid::new_v4();\n        let host = format!(\"communities-of-channels-{}.example\", id.simple());\n        sqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")\n            .bind(id)\n            .bind(host)\n            .execute(pool)\n            .await\n            .expect(\"insert community\");\n        id\n    }\n\n    async fn insert_channel(pool: &PgPool, community_id: Uuid, channel_id: Uuid) {\n        let creator: Vec<u8> = vec![0u8; 32];\n        sqlx::query(","sourceCodeStart":549,"sourceCodeEnd":585,"githubUrl":"https://github.com/block/buzz/blob/eed74bde2f4797714335ac10c56c0b0244c1def4/crates/buzz-db/src/store/community.rs#L549-L585","documentation":"Panic in the shared `setup_db` test helper: `PgPool::connect(&database_url).await.expect(\"connect to test DB\")`. It fires when the test suite cannot establish a Postgres connection using TEST_DATABASE_URL (or the built-in TEST_DB_URL default). Every test in this module calls setup_db, so a connection failure fails them all with this message.","triggerScenarios":"TEST_DATABASE_URL unset and the default URL unreachable; Postgres container not started; wrong host/port/user/password/database name; Postgres up but max_connections exhausted; DNS failure for a non-localhost host.","commonSituations":"Running `cargo test` directly instead of `just test` (which starts the Docker Postgres), switching between worktrees with different .env values, CI missing the Postgres service container, or a stale TEST_DATABASE_URL pointing at a torn-down scratch instance.","solutions":["Start the test infrastructure: `just test` (or docker compose up the Postgres service) before running cargo test.","Export TEST_DATABASE_URL to a reachable Postgres, e.g. postgres://postgres:postgres@localhost:5432/buzz_test.","Verify credentials and database name; create the test database if missing.","If connections are exhausted, close idle pools in tests (drop(pool)) or raise max_connections."],"exampleFix":"// before\nlet database_url = std::env::var(\"TEST_DATABASE_URL\").unwrap_or_else(|_| TEST_DB_URL.into());\nlet pool = PgPool::connect(&database_url).await.expect(\"connect to test DB\");\n// after\nlet database_url = std::env::var(\"TEST_DATABASE_URL\").unwrap_or_else(|_| TEST_DB_URL.into());\nlet pool = PgPool::connect(&database_url).await\n    .unwrap_or_else(|e| panic!(\"connect to test DB at {database_url}: {e}\"));","handlingStrategy":"validation","validationCode":"fn require_test_database_url() -> String {\n    std::env::var(\"TEST_DATABASE_URL\")\n        .unwrap_or_else(|_| TEST_DB_URL.into())\n}\n// pre-flight check before the suite:\nlet url = require_test_database_url();\nPgPool::connect(&url).await.expect(\"TEST_DATABASE_URL unreachable — start Postgres (just test / docker compose up)\");","typeGuard":null,"tryCatchPattern":"let pool = PgPool::connect(&database_url).await\n    .unwrap_or_else(|e| panic!(\"connect to test DB ({database_url}): {e} — is Postgres running and TEST_DATABASE_URL set?\"));","preventionTips":["Always run integration tests through `just test`, which provisions Postgres.","Set TEST_DATABASE_URL in .env and keep it consistent across worktrees.","Add a pre-flight connectivity check before running long suites.","In CI, verify the Postgres service container is healthy before cargo test."],"tags":["rust","test-panic","postgres","connectivity","env-var"],"backgroundTag":"database-connection-refused","analyzedSha":"eed74bde2f4797714335ac10c56c0b0244c1def4","analyzedAt":"2026-08-30T13:49:18.474Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}