Add support for setting a default migrator#4326
Conversation
|
If you rebase it should fix the build error. |
2feab2b to
f8736f8
Compare
|
Thanks, done. |
f8736f8 to
5d1af39
Compare
abonander
left a comment
There was a problem hiding this comment.
Comment plus test please: https://github.com/transact-rs/sqlx/blob/main/tests/postgres/test-attr.rs
| /// Default migrator used for tests. | ||
| pub default_migrator: Option<DefaultMigrator>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| #[cfg_attr( | ||
| feature = "sqlx-toml", | ||
| derive(serde::Deserialize), | ||
| serde(rename_all = "kebab-case", deny_unknown_fields, untagged) | ||
| )] | ||
| pub enum DefaultMigrator { | ||
| /// A single path to a migrator, e.g. `crate::foo::MIGRATOR`. | ||
| Path(String), |
There was a problem hiding this comment.
- Yeah this should be under
test.default-migratorto avoid users thinking this affectssqlx::migrate!() - Why all this instead of a simple
Option<String>?
There was a problem hiding this comment.
Regarding 2., I wanted to support parsing a dictionary of crate names to map to the migrator path. I didn't realize that sqlx.toml can already be set per crate, so this can all be simplified!
This should work as-is. You can specify a different Having a different migrator for each test binary could be as simple as using a local path (and a re-export if necessary). |
|
Is there a way to specify a custom config in tests? I haven't found any mentions of a config in the |
This PR contains an implementation sketch for setting a default migrator for
#[sqlx::test]macros, because repeating all the migrations for each such macro can produce a lot of code bloat and slow compilation times (see #4318 and https://kobzol.github.io/rust/2026/06/21/optimizing-sqlx-test-rebuild-time.html for more details).It is configured in the config:
When a
#[sqlx::test]macro doesn't specify themigrationsnor themigratorfield, and a default migrator is set, it will load it the migrations from it (via a Rust path), rather than inlining the migrations in the expanded#[sqlx::test]output.So it turns from
to
I'm not sure in which config section this makes the most sense, so far I added it to the default
migratesection, but maybe it should have a new dedicatedtestsection?I didn't write tests or document the config option yet, because I first wanted to get a vibe check whether you find the approach reasonable.
TODO:
Does your PR solve an issue?
Fixes: #4318
Is this a breaking change?
No, it is a new opt-in option in the sqlx TOML config.