-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support running macros in "decoupled" mode #60
Comments
I initially thought
We probably need to introduce a
Yes.
It seems without say a force-rebuild option in cargo we would need to use the env variable and
I'd call that out-of-scope for the first iteration of this. But I would say we should do this at some point. Definitely with a way to disable it (via More questions I thought of:
|
To force recompilation, in our macro shims we can do const _: Option<&str> = env!("SQLX_PREPARE_OUT"); which informs rustc of the dependency on that env var. |
I'll add that this effects containerized cross compilation - currently sqlx doesn't work with cross and a local postgres instance because you would need to port forward the postgres instance (which afaik cross doesn't support).
If we integrate (re-)generation with cargo build I'd ask that we include a environment variable so that I can disable it for cross compilation (SQLX_OFFLINE?). Otherwise (re-)generation in |
To further @D1plo1d, this would also 'fix' builds in tools such as bazel as well (or any other isolated build systems such as docker). Having the schema-derived-metadata checked into source control for a given database state seems absolutely necesarry. It would also be great to be able to take this generic metadata and convert it back into the schema definitions that generated it, making sqlx a potent solution for tracking database changes (and potentially migrations) and placing it in a nice spot between raw sql and full blown ORMs To provide input on your question @mehcode about regenerating during cargo build automatically, I personally would like to make sure that the file was always up-to-date for the sake of reproducibility so that I can go back through old commits / tags and guarantee identical behaviour but I don't think automatically rebuilding it is the answer (as we want migration points to be deliberate). In the long term, I would like to see this project break the build time dependency on the database entirely and take an offline-first approach (ie, one that only uses
Then, in my docker containers (or in rust cross, or bazel, etc.), I just compile, and it uses the |
This should now be available in master. See instructions for testing: #249 (comment) |
This was a popular request in the original Reddit announcement: https://www.reddit.com/r/rust/comments/egpw7g/announcing_sqlx_a_fully_asynchronous_pure_rust/fc8ldpw/?context=1000
The idea is to allow expansion of
query!()
, et al, without having to connect directly to the database by running thePREPARE
s for all queries in a separate step and caching the result.The solution in my head looks something like this:
cargo-sqlx
, which linkssqlx-core
internally.cargo sqlx prepare
in their project which invokescargo check
withQUERY_PREPARE_OUT=$OUT_DIR/sqlx
set in the environmentcargo
they're using so we build with the right toolchain, e.g. if the user runscargo +nightly sqlx prepare
when their default toolchain isstable
query!()
and friends seeQUERY_PREPARE_OUT
in the environment they don't perform normal expansion:PREPARE
on their query strings and output the hash of the query and the prepare result as randomly named JSON files toQUERY_PREPARE_OUT
QUERY_PREPARE_OUT
and writes them to a single JSON file in a predetermined location in the project (TBD)PREPARE
data is the same.query!()
is later expanded normally, the macro checks in the file for the hash of its query string and uses that data instead of connecting to the database.cargo sqlx prepare
again.cargo sqlx prepare --check
which does the opposite; it rerunsPREPARE
for the cached query strings and asserts that the result is the same.We can then later extend the
cargo-sqlx
command to add migrations. With migrations in place we have a stronger guarantee that the schema we compiled and cached thePREPARE
data against is the same as the one we are running against.Questions:
prepare
the right name for the sub-subcommand?sqlx.toml
to change the location and possibly other behaviors?QUERY_PREPARE_OUT
or look at acfg
flag? I'm thinking the former might fail to trigger rebuilds when set if Cargo doesn't know to watch for it but the latter might trigger a rebuild of the whole dep graph.touch src/
to trigger a rebuild, no?PREPARE
data we saved still matches what we get back from the database server?The text was updated successfully, but these errors were encountered: