{"record":{"id":"d48ae257b6da2edf","repo":"block/buzz","slug":"apply-desired-state-schema","errorCode":null,"errorMessage":"apply desired-state schema","messagePattern":"apply desired-state schema","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/buzz-db/src/store/channel_members.rs","lineNumber":3145,"sourceCode":"        let scratch_name = format!(\"schema_roster_role_{}\", Uuid::new_v4().simple());\n        sqlx::query(sqlx::AssertSqlSafe(format!(\n            \"CREATE DATABASE {scratch_name}\"\n        )))\n        .execute(&admin)\n        .await\n        .expect(\"create desired-schema scratch db\");\n        let base_url = admin_url().await;\n        let slash = base_url.rfind('/').expect(\"database URL has path segment\");\n        let scratch_url = format!(\"{}/{}\", &base_url[..slash], scratch_name);\n        let pool = PgPoolOptions::new()\n            .max_connections(1)\n            .connect(&scratch_url)\n            .await\n            .expect(\"connect desired-schema scratch db\");\n        sqlx::raw_sql(include_str!(\"../../../../schema/schema.sql\"))\n            .execute(&pool)\n            .await\n            .expect(\"apply desired-state schema\");\n\n        let db = Db::from_pool(pool.clone());\n        let community_uuid = Uuid::new_v4();\n        let community = CommunityId::from_uuid(community_uuid);\n        let channel = Uuid::new_v4();\n        let relay_keys = Keys::generate();\n        let owner_keys = Keys::generate();\n        let owner = owner_keys.public_key().to_bytes();\n        seed_community_channel(&pool, community_uuid, channel, &owner_keys).await;\n        let member = Keys::generate().public_key().to_bytes();\n        sqlx::query(\n            \"INSERT INTO channel_members (community_id, channel_id, pubkey, role, invited_by) \\\n             VALUES ($1, $2, $3, 'admin', $4)\",\n        )\n        .bind(community_uuid)\n        .bind(channel)\n        .bind(member.as_slice())\n        .bind(owner.as_slice())","sourceCodeStart":3127,"sourceCodeEnd":3163,"githubUrl":"https://github.com/block/buzz/blob/dad5a33865fc81a2e55b3b60746632f615ec1e3a/crates/buzz-db/src/store/channel_members.rs#L3127-L3163","documentation":"The test applies the desired-state schema by executing schema/schema.sql verbatim with raw_sql on the scratch pool. The expect panics if any statement in schema.sql fails — syntax errors, missing referenced roles/extensions/functions, or statements that assume state pgschema creates separately (e.g. seed DML, special storage params, or pre-existing roles).","triggerScenarios":"Executing schema.sql against a database that lacks prerequisites the schema references: custom roles, extensions (uuid-ossp/pgcrypto/pg_trgm), or where concurrent multi-statement execution hits ordering issues; also any genuine SQL error inside schema.sql.","commonSituations":"schema.sql drifted from migrations so it no longer applies cleanly to an empty DB; running against a scratch DB without required extensions installed; role-based RLS/GRANT statements referencing roles that don't exist on this instance; AGENTS.md notes pgschema-created DBs omit seed DML — conversely schema.sql may assume hand-applied state.","solutions":["Read the sqlx error's positioned statement to find the failing statement in schema.sql","Pre-create referenced roles/extensions on the scratch DB before executing schema.sql","Reconcile schema.sql with migrations so it applies cleanly from empty (pg_dump a known-good desired state)","If the error is CREATE EXTENSION permission, run the scratch test as superuser"],"exampleFix":"// before\nsqlx::raw_sql(include_str!(\"../../../../schema/schema.sql\"))\n    .execute(&pool).await.expect(\"apply desired-state schema\");\n// after\nsqlx::raw_sql(\"CREATE EXTENSION IF NOT EXISTS pgcrypto;\").execute(&pool).await.expect(\"prereq extension\");\nsqlx::raw_sql(include_str!(\"../../../../schema/schema.sql\"))\n    .execute(&pool).await.expect(\"apply desired-state schema: check the failing statement in the sqlx error\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = sqlx::raw_sql(include_str!(\"../../../../schema/schema.sql\")).execute(&pool).await {\n    panic!(\"apply desired-state schema failed: {e}; find the failing statement via e.as_database_error() and reconcile schema.sql with migrations\");\n}","preventionTips":["Keep schema.sql applicable from an empty database; test it in CI on a scratch DB","Pre-install extensions/roles schema.sql references","Re-run scripts/reconcile-schema-after-pgschema.sql after desired-state applies"],"tags":["postgres","sql","schema","test-infrastructure"],"backgroundTag":"schema-validation-failed","analyzedSha":"dad5a33865fc81a2e55b3b60746632f615ec1e3a","analyzedAt":"2026-08-30T13:49:18.474Z","contentChangedAt":"2026-08-30T13:49:18.474Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}