-
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
feat: make sqlx work with multiple different databases in the same crate #3397
feat: make sqlx work with multiple different databases in the same crate #3397
Conversation
I've actually already started working on this, but going in a slightly different direction. The idea is based on #3383; instead of passing configuration options to the macro directly, you'd give it the path to a different TOML file, and that would let you override all kinds of things. It would also configure the pub mod some_db {
sqlx::sqlx_macros::database_macros!(env = "DATABASE_URL_SOME_DB");
}
fn test() {
some_db::query!("Select a from b").fetch_all(...);
} This unfortunately wouldn't work within the same crate. Macros only use path-based resolution when they're defined in a different crate. Within the same crate, you have to put |
Which part wouldn't work within the same crate? For the tomls would a different sqlx toml be dedicated to each DB configuration? Would loading a toml disable using sqlx env variables so that the configs aren't coming from multiple sources? Might be a bit heavy handed to load a toml when only a URL change is needed... Could both options be supported? Personally I've only needed URL overrides so far |
You cannot refer to a macro by path within the same crate: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=dee33ddf878f6ec5cff9baa725f7781c If you mark the macro with That macro can be referenced by path, but only by other crates. This was a quality-of-life improvement that came with the 2018 edition. If you put Yes, it's incredibly confusing. The Rust macro system is a bunch of hacks piled on top of more hacks. Macros 2.0 was supposed to fix that, but that's been stalled since forever.
Yes.
No. There's only a couple env vars of consequence,
I thought about this, but when you have multiple ways to do something it just ends up confusing the user and being a bigger maintenance burden. It's better if everything just goes in the one file, then you always know where to look. Chances are, you might end up needing to tweak a few other things later anyway. |
Makes sense, thank you for walking me through that! |
Instead of the (edit: Ah, just now saw that this only works from 2018 upwards.) mod after_foo {
pub fn uses_bar() {
super::foo::bar!();
}
}
mod foo {
macro_rules! bar {
() => {
println!("Hello, world!")
};
}
pub(crate) use bar;
}
fn main() {
foo::bar!();
after_foo::uses_bar();
} I'm not seeing any unexpected behavior from this in the project were we are using the forked branch atm. Happy to hear that you are working on the TOML solution though. |
Thank you for this fork 🙏🏼 just tried it out in a quick dummy project and seems to work as expected. Might implement this in the short term... depending on the status of sqlx.toml I also have a fork that starts creating the sqlx.toml @abonander -- it isn't very far along and seeing your PR I am hesitant to keep investing in it. Is it a priority for you? Would you be open to the outside contribution on this? |
Closing in favor of #3383, which I'm working on as fast as I can. I appreciate the effort here, but it's unfortunately using an old solution that's been superceded by |
This PR provides a working solution for allowing sqlx users to utilize multiple different databases during compile/prepare steps.
The solutions is loosely based on the proposals around this topics in the "solved issues"
Three changes have been implemented:
expand_query!
procmacro now takes an additional optional keydb_url_env
- if this key was passed the given enviroment variable will be used to find the DB URL instead of the default one.(e.g.
db_url_env = DATABASE_URL_DB_A
-> query save path will be.../.sqlx/DATABASE_URL_DB_A/sqlx-query...
)This makes sure that the same queries on different databases never collide.
The new macro can be used as follows:
Code completion using rust analyzer with VsCode and Neovim works without a problem.
I am a little bit unhappy with the macro itself, as the entire macro has a full copy of all the query macros.
see
sqlx-macros-core/src/database_macro/expand.rs
If anyone has a suggestion here I'd love to hear it.
One alternative Idea I had was to also generate the macros in
src/macros/mod.rs
with this macro as well.However I am not sure how well this will work with docs.
Does your PR solve an issue?
fixes #916
fixes #224
fixes #121