Skip to content

feat: support DATABASE_URL_TEST env var override in #[sqlx::test]#4312

Open
compiledbyutkarsh wants to merge 1 commit into
transact-rs:mainfrom
compiledbyutkarsh:feat/sqlx-test-database-url-override
Open

feat: support DATABASE_URL_TEST env var override in #[sqlx::test]#4312
compiledbyutkarsh wants to merge 1 commit into
transact-rs:mainfrom
compiledbyutkarsh:feat/sqlx-test-database-url-override

Conversation

@compiledbyutkarsh

Copy link
Copy Markdown

Summary

Fixes #4302

Changes

  • sqlx-postgres/src/testing/mod.rs: Updated both test_context and cleanup_test_dbs to check DATABASE_URL_TEST first, falling back to DATABASE_URL
  • sqlx-mysql/src/testing/mod.rs: Same change applied for MySQL testing

Motivation

#[sqlx::test] requires a superuser connection to create/drop test databases. Many users want to keep their test credentials separate from development/production credentials following the principle of least privilege.

With this change, users can set DATABASE_URL_TEST to a superuser connection string for tests while keeping DATABASE_URL pointing to a restricted user for normal development.

Behavior

  • If DATABASE_URL_TEST is set, it is used for #[sqlx::test]
  • If DATABASE_URL_TEST is not set, falls back to DATABASE_URL (existing behavior preserved — no breaking change)

Testing

Existing behavior is unchanged when DATABASE_URL_TEST is not set. New behavior activates only when DATABASE_URL_TEST is explicitly provided.

Allow #[sqlx::test] to use DATABASE_URL_TEST env var when set,
falling back to DATABASE_URL if not present. This enables users
to configure a dedicated test database user with elevated permissions
(e.g. superuser for Postgres) while keeping production/dev credentials
separate, following the principle of least privilege.

Fixes transact-rs#4302
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant