Skip to content

add diesel_async attribute to multiconnection macro#5099

Merged
weiznich merged 2 commits into
diesel-rs:mainfrom
jusax23:dev-async_multiconnection
Jul 10, 2026
Merged

add diesel_async attribute to multiconnection macro#5099
weiznich merged 2 commits into
diesel-rs:mainfrom
jusax23:dev-async_multiconnection

Conversation

@jusax23

@jusax23 jusax23 commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Adds and implements a diesel_async attribute to multiconnection macro.
This change depends on: diesel-rs/diesel_async#295

Prior Discussion: diesel-rs/diesel_async#108

I ported the test pointed out in the discussion in https://github.com/jusax23/diesel_async/tree/dev-async_multi_connection-test
This test in diesel_async is depended on this macro change.
Should I add this test to the existing pr in diesel_async, or open a new one after it is merged?

I added a feature async_pool, so diesel_async can control the implementation of PoolableConnection.


  • I checked for similar changes and make sure to reference them
  • I included a changelog entry for relevant new features or changes

@jusax23
jusax23 force-pushed the dev-async_multiconnection branch from e7203d0 to 723a3e8 Compare June 30, 2026 20:25
@weiznich
weiznich requested a review from a team July 1, 2026 09:20

@weiznich weiznich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this Pr.

Overall that's the right way to go. I have quite a few comments about the details of the implementation. We can move forward with this as soon as they are addressed.

Comment thread diesel_derives/Cargo.toml Outdated
chrono = []
time = []
numeric = []
async_pool = []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding new feature flags to diesel_derives is something we need to avoid at all cost. Feature flags are broken for proc-macro crate, we recently moved away all the code generation from being depended on feature flags. See #5063 for details. A similar approach needs to be taken here, which likely means:

  • diesel-async needs to export a conditional expand macro for this feature as #[doc(hidden)] item
  • The expanded code needs to use that macro in the generated code to conditionally generate the right code.

Comment thread diesel/Cargo.toml Outdated
]
std = ["serde_json?/std"]
hashbrown = ["dep:hashbrown"]
async_pool = ["diesel_derives/async_pool"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be required as well

}
impl<'a> diesel::query_builder::BindCollector<'a, MultiBackend>
for MultiBindCollector<'a> {
type Buffer = multi_connection_impl::bind_collector::BindValue<'a>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather keep the fully qualified name here, as proc-macro code sometimes is used in wired places, so rather make it as robust as possible.

{
let out = {
let out = multi_connection_impl::bind_collector::BindValue::default();
let out = BindValue::default();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the qualified path here as well

Comment on lines +25 to +26
Pg(PgConnection),
Sqlite(diesel::SqliteConnection),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the real diesel-async connection types here? That's only cosmetic, but helps a bit to judge the generated code.

Comment on lines +113 to +123
let await_token = if is_async {
quote::quote! { .await }
} else {
quote::quote! {}
};

let async_token = if is_async {
quote::quote! { async }
} else {
quote::quote! {}
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be Option<TokenStream> values

Comment thread diesel_derives/src/multiconnection.rs Outdated
let load_impl = connection_types.iter().map(|c| {
let variant_ident = c.name;
let ty = &c.ty;
let (impl_execute_returning_count, impl_execute_returning_count_async) = if is_async {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should likely not be a if else with two return values, where each branch only generate one

Comment thread diesel_derives/src/multiconnection.rs Outdated
Comment on lines +227 to +232
let query = SerializedQuery {
inner: source.as_query(),
backend: MultiBackend::#variant_ident(Default::default()),
query_builder: super::query_builder::MultiQueryBuilder::#variant_ident(Default::default()),
p: std::marker::PhantomData::<#ty>,
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to deduplicate this part with the sync counter part below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would be possible. The only difference is the .as_query().
I did not do this, because i do not think the code is better readable that way.
If you disagree I will dedpulicate this.

Comment thread diesel_derives/src/multiconnection.rs Outdated
Comment on lines +505 to +515
let simple_connection = if is_async {
quote::quote! { diesel_async::SimpleAsyncConnection }
} else {
quote::quote! { diesel::connection::SimpleConnection }
};

let transaction_manager = if is_async {
quote::quote! { diesel_async::TransactionManager }
} else {
quote::quote! { diesel::connection::TransactionManager }
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be part of the helper instead

Comment thread diesel_derives/src/multiconnection.rs Outdated
};

let simple_impls = if is_async {
if cfg!(feature = "async_pool") {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature flag needs to be replaced by a macro call in the generated code

@jusax23
jusax23 force-pushed the dev-async_multiconnection branch 2 times, most recently from 062d7f6 to ea4e454 Compare July 7, 2026 20:36
@jusax23
jusax23 force-pushed the dev-async_multiconnection branch from ea4e454 to 919c243 Compare July 7, 2026 20:40
@jusax23

jusax23 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Hi, I've modified the commit and hopefully resolved all the issues (except one).
I've added a commit to the diesel_async pr, to add extend_poll macro.

@weiznich weiznich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, with the minor fixes applied on top

@weiznich
weiznich enabled auto-merge July 10, 2026 08:45
@weiznich
weiznich added this pull request to the merge queue Jul 10, 2026
Merged via the queue into diesel-rs:main with commit 744748d Jul 10, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants