cli(migrate): add --recursive flag to include sub-directory migrations; optional recursive resolution in core #4055
+158
−75
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.
Motivation
In my recent project, I had to organize database migrations by feature or module, using sub-folders under
migrations/
. Until now,sqlx migrate run
only looked at top-level migration files, which made those nested setups hard to use directly.This PR adds optional recursive migration discovery, so developers can structure migrations cleanly without changing the default behavior.
New Behavior (Opt-in)
Default: No change — existing behavior stays the same (non-recursive).
New flag:
--recursive
When provided,
sqlx
will walk through sub-directories inside the migration folder and apply all valid migrations in version order.Implementation Details
sqlx-core/src/migrate/source.rs
recursive: bool
toResolveConfig
with aset_recursive(bool)
method.resolve_blocking_with_config(...)
to use a directory walker helper.recursive
istrue
, sub-folders are traversed; otherwise, only top-level files are read.sqlx-cli/src/opt.rs
--recursive
.set_recursive(self.recursive)
before building the migrator.sqlx-cli/README.md
--recursive
.sqlx-core/Cargo.toml
tempfile
dev-dependency for migration-related tests.Tests
Added two unit tests in
sqlx-core/src/migrate/source.rs
:✅ All workspace tests pass:
test result: ok. 29 passed; 0 failed; 31 ignored; …
Backwards Compatibility
Performance Notes
Future Work
sqlx.toml
(project-level default).Happy to add that in a follow-up PR if the maintainers are interested!
Files Touched
sqlx-cli/src/opt.rs
sqlx-core/src/migrate/source.rs
sqlx-cli/README.md
sqlx-core/Cargo.toml
Thanks
Appreciate your time reviewing this! 🙏
Let me know if you’d like me to include a
sqlx.toml
config toggle or add an integration test for a nested migration directory — I can update the PR accordingly.