{"record":{"id":"c2b72d79d0305753","repo":"loco-rs/loco","slug":"db-connection-should-success","errorCode":null,"errorMessage":"db connection should success","messagePattern":"db connection should success","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/testing/db.rs","lineNumber":121,"sourceCode":"\n        Ok(Self {\n            root_connection_string: root_url.to_string(),\n            connection_string: test_url.to_string(),\n            schema_name: test_schema_name,\n        })\n    }\n}\n\nimpl TestSupport for PostgresTest {\n    fn get_connection_str(&self) -> &str {\n        &self.connection_string\n    }\n\n    fn init_db<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {\n        Box::pin(async move {\n            let pool = Pool::<Postgres>::connect(&self.root_connection_string)\n                .await\n                .expect(\"db connection should success\");\n            let query = format!(\"CREATE DATABASE {};\", self.schema_name);\n\n            sqlx::query(AssertSqlSafe(query))\n                .execute(&pool)\n                .await\n                .expect(\"create DB schema\");\n        })\n    }\n\n    fn cleanup_db(&self) {\n        let connection_string = self.root_connection_string.clone();\n        let table_name = self.schema_name.clone();\n\n        // Run the drop on a dedicated OS thread with its own runtime (not\n        // `tokio::task::spawn_blocking`, which schedules onto the ambient\n        // runtime and cannot be awaited from a sync/Drop context) and `.join()`\n        // it so cleanup fully completes before `cleanup_db` returns. This is\n        // safe to call from `Drop::drop` since nothing here is `.await`ed on","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/loco-rs/loco/blob/23639d1e360dbc618073642b507d6f8664adbaff/src/testing/db.rs#L103-L139","documentation":"In Loco's test support, the Postgres `init_db` connects to the server using a root/admin connection string before creating the per-test database. `Pool::connect` failing (server unreachable, bad credentials, wrong port) triggers `.expect(\"db connection should success\")`, panicking the test-support boot with no retry.","triggerScenarios":"Running tests when the Postgres server at the configured `root_connection_string` is down, the URL has a wrong host/port/user/password, the database does not accept the root user, or networking/DNS in CI cannot reach the DB.","commonSituations":"Forgetting to start Postgres locally (`docker compose up db`), wrong `DATABASE_URL`-style test env var, CI runner without a Postgres service container, auth errors like `password authentication failed for user postgres`.","solutions":["Verify the test DB connection string (host, port, user, password) and start Postgres: `docker run -p 5432:5432 -e POSTGRES_PASSWORD=... postgres` or `docker compose up -d db`","Check the env var feeding the test config (`LOCO_CONFIG_DATABASE_URI` / test.yaml `database.uri`) resolves correctly in the test environment","Add a Postgres service container in CI before the test step and wait for readiness (healthcheck/pg_isready)","Confirm the root user has CREATEDB privilege so subsequent schema creation can succeed"],"exampleFix":"// before (test.yaml)\ndatabase:\n  uri: \"postgres://loco:secret@localhost:5433/loco_test\"\n// after — match the running server\n# psql -h localhost -p 5432 -U postgres -c 'select 1'  # verify first\ndatabase:\n  uri: \"postgres://postgres:postgres@localhost:5432/loco_test\"","handlingStrategy":"retry","validationCode":"// Readiness probe before running tests\nlet ok = tokio::net::TcpStream::connect(\"127.0.0.1:5432\").await.is_ok();\nassert!(ok, \"Postgres not reachable on 127.0.0.1:5432\");\n// Or: pg_isready -h localhost -p 5432","typeGuard":null,"tryCatchPattern":"// Wrap test-support boot and fail with a readable message\nmatch std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {\n    // boot_test / request::<App,_,_> setup\n})) {\n    Ok(v) => v,\n    Err(_) => panic!(\"test DB unreachable — is Postgres running and is the root URI correct?\"),\n}","preventionTips":["Start Postgres (docker compose) before cargo test in scripts and CI","Verify the test connection string with psql first","Add a CI healthcheck/wait step (pg_isready) before tests","Give the test root user CREATEDB privileges"],"tags":["rust","postgres","database","testing","connection"],"backgroundTag":"connection-refused","analyzedSha":"23639d1e360dbc618073642b507d6f8664adbaff","analyzedAt":"2026-09-12T01:47:20.769Z","contentChangedAt":"2026-09-12T01:47:20.769Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}