{"record":{"id":"aa9f2550bcd83107","repo":"block/buzz","slug":"insert-community-aa9f25","errorCode":null,"errorMessage":"insert community","messagePattern":"insert community","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/buzz-db/src/store/community.rs","lineNumber":681,"sourceCode":"    use sqlx::PgPool;\n\n    async fn setup_db() -> Db {\n        let database_url = crate::test_support::database_url();\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(\n            r#\"\n            INSERT INTO channels\n                (id, community_id, name, channel_type, visibility, created_by)\n            VALUES\n                ($1, $2, $3, 'stream'::channel_type, 'open'::channel_visibility, $4)\n            \"#,\n        )\n        .bind(channel_id)\n        .bind(community_id)\n        .bind(format!(\"ch-{}\", channel_id.simple()))\n        .bind(&creator)\n        .execute(pool)","sourceCodeStart":663,"sourceCodeEnd":699,"githubUrl":"https://github.com/block/buzz/blob/dad5a33865fc81a2e55b3b60746632f615ec1e3a/crates/buzz-db/src/store/community.rs#L663-L699","documentation":"Panic in the `make_community` fixture: `sqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\").execute(pool).await.expect(\"insert community\")`. The expect fires when the INSERT fails — most commonly a unique-constraint violation on communities.host or a foreign-key/schema problem. The test generates a random host per call, so a duplicate means a collided or pre-existing row.","triggerScenarios":"INSERT returns Err when the host value already exists (unique index), the communities table/columns don't exist (migrations not applied), the pool connection breaks mid-test, or a generated UUID/host collides with a leftover row from a previous crashed run.","commonSituations":"Stale rows in a shared test database from an earlier run that didn't clean up, migrations skipped, or tests run against a persistent (non-scratch) DB where the deterministic host format was reused.","solutions":["Reset the scratch/test database (drop and re-run migrations) to remove leftover rows blocking the unique host index.","Include the random UUID in the host (it already does via id.simple()) — verify the collision is with an old row, not this test.","Run migrations against TEST_DATABASE_URL before the suite.","Log the underlying sqlx error (unwrap_or_else panic with {e}) to distinguish constraint violation from connection failure."],"exampleFix":"// before\nsqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")\n    .bind(id).bind(host).execute(pool).await.expect(\"insert community\");\n// after\nsqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")\n    .bind(id).bind(host).execute(pool).await\n    .unwrap_or_else(|e| panic!(\"insert community {id} host {host}: {e}\"));","handlingStrategy":"try-catch","validationCode":"// ensure a clean slate for the fixture\nsqlx::query(\"DELETE FROM communities WHERE host = $1\")\n    .bind(&host)\n    .execute(pool)\n    .await\n    .expect(\"clean stale community rows\");","typeGuard":null,"tryCatchPattern":"sqlx::query(\"INSERT INTO communities (id, host) VALUES ($1, $2)\")\n    .bind(id).bind(host).execute(pool).await\n    .unwrap_or_else(|e| panic!(\"insert community {id} host {host}: {e}\"));","preventionTips":["Make fixture hosts unique with a fresh UUID each run (already using id.simple() — keep it).","Reset the shared test database before suites to remove stale rows.","Surface the sqlx error text, not just a label, to distinguish constraint vs connection issues.","Keep migrations applied to the test DB in setup."],"tags":["rust","test-panic","sqlx","unique-constraint"],"backgroundTag":"unique-constraint-violation","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"}