{"record":{"id":"8b5a79c1e33e9c65","repo":"transact-rs/sqlx","slug":"cleanup-test-invoked-outside-sqlx-test-8b5a79","errorCode":null,"errorMessage":"cleanup_test() invoked outside `#[sqlx::test]`","messagePattern":"cleanup_test\\(\\) invoked outside `#\\[sqlx::test\\]`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sqlx-postgres/src/testing/mod.rs","lineNumber":34,"sourceCode":"use crate::{PgConnectOptions, PgConnection, Postgres};\n\npub(crate) use sqlx_core::testing::*;\n\n// Using a blocking `OnceLock` here because the critical sections are short.\nstatic MASTER_POOL: OnceLock<Pool<Postgres>> = OnceLock::new();\n// Automatically delete any databases created before the start of the test binary.\n\nimpl TestSupport for Postgres {\n    fn test_context(\n        args: &TestArgs,\n    ) -> impl Future<Output = Result<TestContext<Self>, Error>> + Send + '_ {\n        test_context(args)\n    }\n\n    async fn cleanup_test(db_name: &str) -> Result<(), Error> {\n        let mut conn = MASTER_POOL\n            .get()\n            .expect(\"cleanup_test() invoked outside `#[sqlx::test]`\")\n            .acquire()\n            .await?;\n\n        do_cleanup(&mut conn, db_name).await\n    }\n\n    async fn cleanup_test_dbs() -> Result<Option<usize>, Error> {\n        let url = dotenvy::var(\"DATABASE_URL\").expect(\"DATABASE_URL must be set\");\n\n        let mut conn = PgConnection::connect(&url).await?;\n\n        let delete_db_names: Vec<String> = query_scalar(\"select db_name from _sqlx_test.databases\")\n            .fetch_all(&mut conn)\n            .await?;\n\n        if delete_db_names.is_empty() {\n            return Ok(None);\n        }","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/transact-rs/sqlx/blob/03af8bcc5711a1935580a54bea249c219a0c217d/sqlx-postgres/src/testing/mod.rs#L16-L52","documentation":"Postgres's testing support `cleanup_test` acquires from `MASTER_POOL`, a global pool initialized only inside `#[sqlx::test]`. `.get()` on the uninitialized Option panics with this message when cleanup is called outside the macro's context.","triggerScenarios":"Calling the testing API's `cleanup_test(db_name)` from a non-`#[sqlx::test]` test or ordinary async code, so MASTER_POOL was never set.","commonSituations":"Refactoring tests away from `#[sqlx::test]` while retaining cleanup calls; invoking cleanup in helpers, `Drop` impls, or a separate binary; ordering issues where cleanup runs before any sqlx test initialized the pool.","solutions":["Run the test under `#[sqlx::test]` so MASTER_POOL is initialized; let the macro handle cleanup.","Delete explicit `cleanup_test` calls from non-macro tests.","For bespoke cleanup, create your own PgPool from DATABASE_URL and run the cleanup SQL yourself."],"exampleFix":"// before\n#[tokio::test]\nasync fn t() { cleanup_test(\"mydb\").await.unwrap(); }\n// after\n#[sqlx::test]\nasync fn t(pool: PgPool) { /* automatic cleanup */ }","handlingStrategy":"validation","validationCode":"// cleanup_test requires the #[sqlx::test] context; enforce by convention:\n// prefer:\n#[sqlx::test]\nasync fn my_test(pool: sqlx::PgPool) { /* ... */ }\n// and delete any manual calls to cleanup_test","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Let #[sqlx::test] own pool setup and teardown","Do not invoke testing-module helpers from non-macro tests or binaries","Grep for `cleanup_test(` in reviews to catch out-of-context calls"],"tags":["sqlx","postgres","testing","panic","misuse"],"backgroundTag":"test-context-misuse","analyzedSha":"03af8bcc5711a1935580a54bea249c219a0c217d","analyzedAt":"2026-09-03T15:01:28.752Z","contentChangedAt":"2026-09-03T15:01:28.752Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}