sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Fixture relative path

Open sinder38 opened this issue 9 months ago • 0 comments

I have found these related issues/pull requests

This in a way continues this PR: https://github.com/launchbadge/sqlx/pull/2545

Description

fixtures test gets relative file from the file position instead of module. This not a bug since docs mention it, but I really don't like it.

This happens because sqlx-macros uses include_str!() to get file contents.

https://github.com/launchbadge/sqlx/blob/6b2e0247d47d020d91dc6f7402d42e4e6131af11/sqlx-macros-core/src/test_attr.rs#L122

Reproduction:

lib-core/
├── db/
│   └── migrations/
│   └── fixtures/
│     └── seed_test.sql
└── src/
    └── model/
        └── good/
            └── good_type.rs

Both of these will cause an error

#[sqlx::test(
	migrations = "./db/migrations",
	fixtures(path = "./db/fixtures", scripts("seed_test"))
)]
async fn test_list_interface(pool: Db) -> Result<()> {todo!()}

#[sqlx::test(
	migrations = "./db/migrations",
	fixtures(path = "./db/fixtures", scripts("seed_test"))
)]
async fn test_list_interface(pool: Db) -> Result<()> {todo!()}


These will work... but are very awkward

	#[sqlx::test(
		migrations = "./db/migrations",
		fixtures("../../../db/fixtures/seed_test.sql")
	)]
	async fn test_list_full(pool: Db) -> Result<()> {


	#[sqlx::test(
		migrations = "./db/migrations",
		fixtures(path = "../../../db/fixtures", scripts("seed_test"))
	)]
	async fn test_list_interface(pool: Db) -> Result<()> {

Prefered solution

Option 1 I think in case of relative positions there should be 2 ways to specify a path, both from the file position and module position.

	#[sqlx::test(
		migrations = "./db/migrations",
		fixtures(module_path = "./db/fixtures", scripts("seed_test"))
	)]

Option 2 allow to specify fixture and migration directories in env would be a dream. If it is specified all tests start path from it unless overridden. This does not introduce any new syntax.

Is this a breaking change? Why or why not?

This is not a breaking change since path="to/your/test.sql" functionality is not touched.

sinder38 avatar May 03 '25 05:05 sinder38