#3947·sqlx

Support for specifying database URLs per test via an environment variable option in sqlx::test macro

Author: simple-randomCreated Jul 23, 2025Updated Sep 30, 2026
Labelsenhancement

I have found these related issues/pull requests

Relates to issues #2220, #3022

Description

sqlx::test currently relies on a hardcoded DATABASE_URL environment variable as the database URL to which connections are made when spinning up isolated test databases. However, I am working on an application that stores multiple database URLs in separate environment variables, each database having a separate set of migrations to be run.

Preferred solution

It would be useful to have a db_env_var option on the #[sqlx::test] macro that allows specifying which environment variable to use for the database URL for each test, defaulting to DATABASE_URL if not specified:

rust
#[sqlx::test(db_env_var = "SOME_DATABASE_URL")]
async fn some_test(pool: PgPool) {
    // test code goes here
}

Is this a breaking change? Why or why not?

This should not be a breaking change, since sqlx::test without an explicit db_env_var would continue to default to DATABASE_URL as it does in the current implementation.