fix(sqlite): apply drivers.sqlite config from sqlx.toml in #[sqlx::test] - #4414
Open
jdscreations wants to merge 1 commit into
Open
jdscreations wants to merge 1 commit into
jdscreations wants to merge 1 commit into
Conversation
…:test]` `test_context()` built `SqliteConnectOptions` directly instead of going through `SqliteConnectOptions::apply_driver_config()`, so `sqlx.toml`'s `drivers.sqlite` settings (e.g. `unsafe-load-extensions`) were silently ignored for databases created by `#[sqlx::test]`, unlike connections made via `sqlx::query!()` or `sqlx-cli`, which already read this configuration. Extract the two-line fix into `apply_sqlx_toml_config()` so it can be unit tested without requiring an async runtime, and add a regression test using this crate's own `sqlx.toml` (new file, used only by that test). Closes transact-rs#4372 Signed-off-by: SiddharthSanch <111047247+SiddharthSanch@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Does your PR solve an issue?
fixes #4372
Is this a breaking change?
No — this brings
#[sqlx::test]in line with the already-documented behavior ofdrivers.sqliteinsqlx.toml, whichsqlx::query!()andsqlx-clialready apply. Strictly speaking, per Hyrum's Law, a project withdrivers.sqliteconfig in itssqlx.tomlthat happened to rely on#[sqlx::test]not applying it could see a behavior change, but that would be relying on an unintentional gap rather than documented behavior.Summary
#[sqlx::test]didn't applydrivers.sqliteconfig fromsqlx.toml(e.g.unsafe-load-extensions) to the SQLite databases it creates, unlike connections made throughsqlx::query!()orsqlx-cli, which already read this configuration viaSqliteConnectOptions::apply_driver_config(). A project using SQLite extensions in its migrations (as supported since #3917) would have those migrations fail inside#[sqlx::test], because the extension was never loaded.Root cause
test_context()insqlx-sqlite/src/testing/mod.rsbuiltSqliteConnectOptionsdirectly:without ever calling
.apply_driver_config(&config.drivers.sqlite), so anydrivers.sqlitesettings insqlx.tomlwere silently ignored for test databases.Approach
Added
apply_sqlx_toml_config(), which readsConfig::try_from_crate_or_default()and appliesconfig.drivers.sqlitevia the existingapply_driver_config(), and call it when building the options intest_context(). This is the same config-reading path already used bysqlx-sqlite::describe_blocking(forsqlx::query!()/sqlx-cli), just applied to the test-database path too.I considered inlining the two new lines directly into
test_context()(which is what the fix boiled down to), but extracting them into a small synchronous helper made it possible to unit test the config-application logic in isolation, without needing an async runtime.Test evidence
Added
sqlx-sqlite/sqlx.toml(used only by this new test) with a bogusunsafe-load-extensionsmarker entry, and a unit test insqlx-sqlite/src/testing/mod.rs:Before the fix (verified by temporarily reverting
apply_sqlx_toml_config()to a no-op):After the fix:
Also ran, all passing with no regressions:
cargo test -p sqlx-sqlite --features migrate,sqlx-toml --lib(9/9 passed)cargo test --test sqlite-test-attr --no-default-features --features "sqlite,macros,migrate,runtime-tokio,tls-none"— the existing end-to-end#[sqlx::test]suite (5/5 passed), confirming no change in behavior for projects withoutdrivers.sqliteconfig in theirsqlx.tomlcargo clippy -p sqlx-sqlite --features migrate,sqlx-toml --lib --tests— cleancargo fmt -p sqlx-sqlite -- --check— cleanNote: I don't have Docker/Postgres/MySQL available in my environment, so I was only able to run the SQLite-specific suites above; this change only touches
sqlx-sqlite.