{"record":{"id":"a543e82bb4aafd18","repo":"block/buzz","slug":"insert-community","errorCode":null,"errorMessage":"insert community","messagePattern":"insert community","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/buzz-db/src/runtime/tests.rs","lineNumber":1679,"sourceCode":"/// fresh proved entry ⇒ replica (bounded arm); over-budget entry ⇒\n/// writer. Divergent membership rows prove which pool answered.\n#[tokio::test]\n#[ignore = \"requires Postgres\"]\nasync fn is_relay_member_is_bounded_routed_and_fails_closed() {\n    let admin = PgPool::connect(&admin_url().await)\n        .await\n        .expect(\"connect admin\");\n    let (writer, wname) = create_scratch_db(&admin, \"mem_w\").await;\n    let (replica, rname) = create_scratch_db(&admin, \"mem_r\").await;\n\n    let community = Uuid::new_v4();\n    for pool in [&writer, &replica] {\n        sqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")\n            .bind(community)\n            .bind(format!(\"member-routing-{}.example\", community.simple()))\n            .execute(pool)\n            .await\n            .expect(\"insert community\");\n    }\n    let cid = CommunityId::from_uuid(community);\n    let writer_only = \"aa\".repeat(32);\n    let replica_only = \"bb\".repeat(32);\n    relay_members::add_relay_member(&writer, cid, &writer_only, \"member\", None)\n        .await\n        .expect(\"seed writer member\");\n    relay_members::add_relay_member(&replica, cid, &replica_only, \"member\", None)\n        .await\n        .expect(\"seed replica member\");\n\n    let mut db = Db::from_pools(writer.clone(), replica.clone());\n    db.fence().force_open_for_tests(chrono::Utc::now());\n\n    // Budget unset ⇒ bounded arm disabled ⇒ writer.\n    assert!(\n        db.is_relay_member(cid, &writer_only)\n            .await","sourceCodeStart":1661,"sourceCodeEnd":1697,"githubUrl":"https://github.com/block/buzz/blob/dad5a33865fc81a2e55b3b60746632f615ec1e3a/crates/buzz-db/src/runtime/tests.rs#L1661-L1697","documentation":"This is `.expect(\"insert community\")` on a raw `sqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")` executed against both writer and replica scratch pools. It panics if the INSERT fails — most often because the `communities` table does not exist in the freshly created scratch database (migrations were never applied), or because a duplicate community id/host violates a constraint on the second pool.","triggerScenarios":"Running `is_relay_member_is_bounded_routed_and_fails_closed` where `create_scratch_db` produced empty (unmigrated) databases; the host string collides with an existing row via a unique constraint; the connection to one pool dropped.","commonSituations":"Test harness that creates scratch DBs from `template0`/`template1` without running buzz-db migrations; leftover scratch DB with the same host from a crashed prior run; SQL syntax/type drift after a schema change.","solutions":["Apply buzz-db migrations to each scratch DB immediately after `create_scratch_db` before seeding.","Drop leftover `mem_w`/`mem_r` scratch databases from previous failed runs.","Print the sqlx error (`.unwrap_or_else(|e| panic!(\"insert community: {e}\"))`) to see constraint vs missing-table.","Confirm the `communities` schema still matches this test's INSERT columns after schema changes.","Check both pool inserts succeed — the loop runs on writer and replica; the label doesn't say which failed."],"exampleFix":"// before\nsqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")\n    .bind(community).bind(host).execute(pool).await.expect(\"insert community\");\n// after\nsqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")\n    .bind(community).bind(host).execute(pool).await\n    .unwrap_or_else(|e| panic!(\"insert community on {} failed: {e}\", pool_name));","handlingStrategy":"validation","validationCode":"// Confirm the table exists before seeding\nlet ok: bool = sqlx::query_scalar(\n    \"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name='communities')\")\n    .fetch_one(pool).await.unwrap_or(false);\nassert!(ok, \"scratch DB not migrated — communities table missing\");","typeGuard":"async fn table_exists(pool: &sqlx::PgPool, name: &str) -> bool {\n    sqlx::query_scalar(\"SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name=$1)\")\n        .bind(name).fetch_one(pool).await.unwrap_or(false)\n}","tryCatchPattern":"match insert.execute(pool).await {\n    Ok(_) => {}\n    Err(e) => panic!(\"insert community failed (migrated? unique host?): {e}\"),\n}","preventionTips":["Run buzz-db migrations on every scratch DB after creation","Use unique per-run community ids/hosts to avoid unique constraint hits","Clean up leftover scratch DBs (drop_scratch_db in drop guards)","Update the INSERT when the communities schema changes"],"tags":["postgres","sqlx","test-panic","missing-migration"],"backgroundTag":"postgres-insert-failed","analyzedSha":"dad5a33865fc81a2e55b3b60746632f615ec1e3a","analyzedAt":"2026-08-30T13:49:18.474Z","contentChangedAt":"2026-08-30T13:49:18.474Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}