[offline] Change prepare to one-file-per-query #2363
Merged
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.
Description
Copy of #2110.
Continuation of/supersedes #1770 (comment) and #1183.
Overhauls offline query metadata from being saved in a single
sqlx-data.json
file, to every query being saved separately in a.sqlx
directory (either in the package or workspace root depending on the--workspace
flag).E.g.
Changed
Many of the changes are from #1770 and #1183. I updated the previous work on this with the latest changes from the
main
branch, implemented outstanding things likecargo sqlx prepare --check
, and fixed several bugs.sqlx prepare
to save queries individually in a.sqlx
directory.--merged
flag to--workspace
forsqlx prepare
.Related issues
Closes #570
#1005Edit: this PR is no longer related to MSSQL.Supersedes #2322
Type of change
Breaking change. Developers will need to install the latest
sqlx-cli
and re-runcargo sqlx prepare
in their projects.How does it work?
Saving offline queries
cargo sqlx prepare
, the command will executecargo check
with theSQLX_OFFLINE_DIR
environment variable set to<package or workspace>/.sqlx
, depending on the--workspace
flag.expand_with_data
insqlx-macros
saves the query metadata to a temporary file, before moving it to theSQLX_OFFLINE_DIR
path (the move/rename operation should be atomic).SQLX_OFFLINE_DIR
is not set (either manually or bysqlx prepare
), should avoid any race conditions with IDEs building at the same time like cargo sqlx prepare picks up queries from another crate #2049.Note that
SQLX_OFFLINE_DIR
can be set manually socargo check
withoutcargo sqlx prepare
can generate query files---useful for the CI---but the environment variable doesn't override the directories used bysqlx prepare
orsqlx prepare --check
, should it?I'm concerned about data loss if it's set to another directory andEdit: changed removal behaviour so onlysqlx prepare
tries to delete it.query-*.json
files are deleted.Loading offline queries
When loading query metadata offline,
expand_input
insqlx-macros
will check if the query is saved inSQLX_OFFLINE_DIR
if set, then<package>/.sqlx
, then<workspace>/.sqlx
.Checking offline queries
Running
cargo sqlx prepare --check
will generate queries and place them intarget/sqlx-prepare-check
and compare the contents to<package or workspace>/.sqlx
, depending on the--workspace
flag.If there are queries in
target/sqlx-prepare-check
that aren't in<package or workspace>/.sqlx
(indicating new queries were probably added), the command will fail with an error:prepare check failed: .sqlx is missing one or more queries; you should re-run sqlx prepare
.If there are unused queries in
<package or workspace>/.sqlx
that aren't intarget/sqlx-prepare-check
(indicating old queries were probably removed), the command will succeed with a warning:potentially unused queries found in .sqlx; you may want to re-run sqlx prepare
.Finally, it will compare the JSON contents of files in both directories to ensure they match.