Skip to content

Commit 3d8b5a5

Browse files
committed
Merge remote-tracking branch 'origin/main' into patch-3
2 parents 1d0372b + 6437e59 commit 3d8b5a5

File tree

20 files changed

+741
-154
lines changed

20 files changed

+741
-154
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on:
44
push:
55
branches:
66
- main
7+
- 0.3.x
8+
- 0.4.x
79
- 0.2.x
810

911
name: CI Tests
@@ -41,9 +43,19 @@ jobs:
4143
4244
- name: Set environment variables
4345
shell: bash
44-
if: matrix.rust == 'nightly'
46+
if: matrix.backend == 'postgres' && matrix.os == 'windows-2019'
4547
run: |
46-
echo "RUSTFLAGS=--cap-lints=warn" >> $GITHUB_ENV
48+
echo "AWS_LC_SYS_NO_ASM=1"
49+
50+
- name: Set environment variables
51+
shell: bash
52+
if: matrix.rust != 'nightly'
53+
run: |
54+
echo "RUSTFLAGS=-D warnings" >> $GITHUB_ENV
55+
echo "RUSTDOCFLAGS=-D warnings" >> $GITHUB_ENV
56+
57+
- uses: ilammy/setup-nasm@v1
58+
if: matrix.backend == 'postgres' && matrix.os == 'windows-2019'
4759

4860
- name: Install postgres (Linux)
4961
if: runner.os == 'Linux' && matrix.backend == 'postgres'
@@ -137,13 +149,13 @@ jobs:
137149
- name: Install mysql (MacOS M1)
138150
if: matrix.os == 'macos-14' && matrix.backend == 'mysql'
139151
run: |
140-
brew install mariadb@11.3
141-
ls /opt/homebrew/opt/mariadb@11.3
142-
/opt/homebrew/opt/mariadb@11.3/bin/mysql_install_db
143-
/opt/homebrew/opt/mariadb@11.3/bin/mysql.server start
152+
brew install mariadb@11.2
153+
ls /opt/homebrew/opt/mariadb@11.2
154+
/opt/homebrew/opt/mariadb@11.2/bin/mysql_install_db
155+
/opt/homebrew/opt/mariadb@11.2/bin/mysql.server start
144156
sleep 3
145-
/opt/homebrew/opt/mariadb@11.3/bin/mysqladmin -u runner password diesel
146-
/opt/homebrew/opt/mariadb@11.3/bin/mysql -e "create database diesel_test; create database diesel_unit_test; grant all on \`diesel_%\`.* to 'runner'@'localhost';" -urunner
157+
/opt/homebrew/opt/mariadb@11.2/bin/mysqladmin -u runner password diesel
158+
/opt/homebrew/opt/mariadb@11.2/bin/mysql -e "create database diesel_test; create database diesel_unit_test; grant all on \`diesel_%\`.* to 'runner'@'localhost';" -urunner
147159
echo "DATABASE_URL=mysql://runner:diesel@localhost/diesel_test" >> $GITHUB_ENV
148160
149161
- name: Install postgres (Windows)
@@ -223,7 +235,7 @@ jobs:
223235
find ~/.cargo/registry -iname "*clippy.toml" -delete
224236
225237
- name: Run clippy
226-
run: cargo +stable clippy --all
238+
run: cargo +stable clippy --all --all-features
227239

228240
- name: Check formating
229241
run: cargo +stable fmt --all -- --check

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
77
## [Unreleased]
88

99
* Added type `diesel_async::pooled_connection::mobc::PooledConnection`
10-
* MySQL/MariaDB now use `CLIENT_FOUND_ROWS` capability to allow consistent behavior with PostgreSQL regarding return value of UPDATe commands.
10+
* MySQL/MariaDB now use `CLIENT_FOUND_ROWS` capability to allow consistent behaviour with PostgreSQL regarding return value of UPDATe commands.
1111
* The minimal supported rust version is now 1.78.0
12+
* Add a `SyncConnectionWrapper` type that turns a sync connection into an async one. This enables SQLite support for diesel-async
13+
* Add support for `diesel::connection::Instrumentation` to support logging and other instrumentation for any of the provided connection impls.
14+
* Bump minimal supported mysql_async version to 0.34
1215

1316
## [0.4.1] - 2023-09-01
1417

Cargo.toml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ futures-util = { version = "0.3.17", default-features = false, features = [
2727
] }
2828
tokio-postgres = { version = "0.7.10", optional = true }
2929
tokio = { version = "1.26", optional = true }
30-
mysql_async = { version = ">=0.30.0,<0.34", optional = true, default-features = false, features = [
31-
"minimal",
32-
"derive",
33-
] }
34-
mysql_common = { version = ">=0.29.0,<0.32.0", optional = true, default-features = false, features = [
35-
"frunk",
36-
"derive",
30+
mysql_async = { version = "0.34", optional = true, default-features = false, features = [
31+
"minimal-rust",
3732
] }
33+
mysql_common = { version = "0.32", optional = true, default-features = false }
3834

3935
bb8 = { version = "0.8", optional = true }
40-
deadpool = { version = "0.11", optional = true, default-features = false, features = [
36+
deadpool = { version = "0.12", optional = true, default-features = false, features = [
4137
"managed",
4238
] }
4339
mobc = { version = ">=0.7,<0.10", optional = true }
@@ -49,9 +45,10 @@ cfg-if = "1"
4945
chrono = "0.4"
5046
diesel = { version = "2.2.0", default-features = false, features = ["chrono"] }
5147
diesel_migrations = "2.2.0"
48+
assert_matches = "1.0.1"
5249

5350
[features]
54-
default = []
51+
default = ["sync-connection-wrapper"]
5552
mysql = [
5653
"diesel/mysql_backend",
5754
"mysql_async",
@@ -83,8 +80,8 @@ features = [
8380
"r2d2",
8481
]
8582
no-default-features = true
86-
rustc-args = ["--cfg", "doc_cfg"]
87-
rustdoc-args = ["--cfg", "doc_cfg"]
83+
rustc-args = ["--cfg", "docsrs"]
84+
rustdoc-args = ["--cfg", "docsrs"]
8885

8986
[workspace]
9087
members = [
@@ -93,4 +90,3 @@ members = [
9390
"examples/postgres/run-pending-migrations-with-rustls",
9491
"examples/sync-wrapper",
9592
]
96-

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ let mut conn = pool.get().await?;
168168
let res = users::table.select(User::as_select()).load::(&mut conn).await?;
169169
```
170170

171+
## Diesel-Async with Secure Database
172+
173+
In the event of using this crate with a `sslmode=require` flag, it will be necessary to build a TLS cert.
174+
There is an example provided for doing this using the `rustls` crate in the `postgres` examples folder.
175+
171176
## Crate Feature Flags
172177

173178
Diesel-async offers several configurable features:

examples/postgres/pooled-with-rustls/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
diesel = { version = "2.1.0", default-features = false, features = ["postgres"] }
9+
diesel = { version = "2.2.0", default-features = false, features = ["postgres"] }
1010
diesel-async = { version = "0.4.0", path = "../../../", features = ["bb8", "postgres"] }
1111
futures-util = "0.3.21"
12-
rustls = "0.20.8"
13-
rustls-native-certs = "0.6.2"
12+
rustls = "0.23.8"
13+
rustls-native-certs = "0.7.1"
1414
tokio = { version = "1.2.0", default-features = false, features = ["macros", "rt-multi-thread"] }
1515
tokio-postgres = "0.7.7"
16-
tokio-postgres-rustls = "0.9.0"
16+
tokio-postgres-rustls = "0.12.0"

examples/postgres/pooled-with-rustls/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConne
4343
let fut = async {
4444
// We first set up the way we want rustls to work.
4545
let rustls_config = rustls::ClientConfig::builder()
46-
.with_safe_defaults()
4746
.with_root_certificates(root_certs())
4847
.with_no_client_auth();
4948
let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config);
@@ -63,7 +62,6 @@ fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConne
6362
fn root_certs() -> rustls::RootCertStore {
6463
let mut roots = rustls::RootCertStore::empty();
6564
let certs = rustls_native_certs::load_native_certs().expect("Certs not loadable!");
66-
let certs: Vec<_> = certs.into_iter().map(|cert| cert.0).collect();
67-
roots.add_parsable_certificates(&certs);
65+
roots.add_parsable_certificates(certs);
6866
roots
6967
}

examples/postgres/run-pending-migrations-with-rustls/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
diesel = { version = "2.1.0", default-features = false, features = ["postgres"] }
9+
diesel = { version = "2.2.0", default-features = false, features = ["postgres"] }
1010
diesel-async = { version = "0.4.0", path = "../../../", features = ["bb8", "postgres", "async-connection-wrapper"] }
11-
diesel_migrations = "2.1.0"
11+
diesel_migrations = "2.2.0"
1212
futures-util = "0.3.21"
13-
rustls = "0.20.8"
14-
rustls-native-certs = "0.6.2"
13+
rustls = "0.23.10"
14+
rustls-native-certs = "0.7.1"
1515
tokio = { version = "1.2.0", default-features = false, features = ["macros", "rt-multi-thread"] }
1616
tokio-postgres = "0.7.7"
17-
tokio-postgres-rustls = "0.9.0"
17+
tokio-postgres-rustls = "0.12.0"

examples/postgres/run-pending-migrations-with-rustls/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConne
2929
let fut = async {
3030
// We first set up the way we want rustls to work.
3131
let rustls_config = rustls::ClientConfig::builder()
32-
.with_safe_defaults()
3332
.with_root_certificates(root_certs())
3433
.with_no_client_auth();
3534
let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config);
@@ -49,7 +48,6 @@ fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConne
4948
fn root_certs() -> rustls::RootCertStore {
5049
let mut roots = rustls::RootCertStore::empty();
5150
let certs = rustls_native_certs::load_native_certs().expect("Certs not loadable!");
52-
let certs: Vec<_> = certs.into_iter().map(|cert| cert.0).collect();
53-
roots.add_parsable_certificates(&certs);
51+
roots.add_parsable_certificates(certs);
5452
roots
5553
}

examples/sync-wrapper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
diesel = { version = "2.1.0", default-features = false }
9+
diesel = { version = "2.1.0", default-features = false, features = ["returning_clauses_for_sqlite_3_35"] }
1010
diesel-async = { version = "0.4.0", path = "../../", features = ["sync-connection-wrapper", "async-connection-wrapper"] }
1111
diesel_migrations = "2.1.0"
1212
futures-util = "0.3.21"

examples/sync-wrapper/src/main.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use diesel::prelude::*;
22
use diesel::sqlite::{Sqlite, SqliteConnection};
33
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
44
use diesel_async::sync_connection_wrapper::SyncConnectionWrapper;
5-
use diesel_async::{AsyncConnection, RunQueryDsl, SimpleAsyncConnection};
5+
use diesel_async::{AsyncConnection, RunQueryDsl};
66
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
77

88
// ordinary diesel model setup
@@ -15,7 +15,7 @@ table! {
1515
}
1616

1717
#[allow(dead_code)]
18-
#[derive(Debug, Queryable, Selectable)]
18+
#[derive(Debug, Queryable, QueryableByName, Selectable)]
1919
#[diesel(table_name = users)]
2020
struct User {
2121
id: i32,
@@ -47,6 +47,27 @@ where
4747
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
4848
}
4949

50+
async fn transaction(
51+
async_conn: &mut SyncConnectionWrapper<InnerConnection>,
52+
old_name: &str,
53+
new_name: &str,
54+
) -> Result<Vec<User>, diesel::result::Error> {
55+
async_conn
56+
.transaction::<Vec<User>, diesel::result::Error, _>(|c| {
57+
Box::pin(async {
58+
if old_name.is_empty() {
59+
Ok(Vec::new())
60+
} else {
61+
diesel::update(users::table.filter(users::name.eq(old_name)))
62+
.set(users::name.eq(new_name))
63+
.load(c)
64+
.await
65+
}
66+
})
67+
})
68+
.await
69+
}
70+
5071
#[tokio::main]
5172
async fn main() -> Result<(), Box<dyn std::error::Error>> {
5273
let db_url = std::env::var("DATABASE_URL").expect("Env var `DATABASE_URL` not set");
@@ -57,10 +78,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
5778

5879
let mut sync_wrapper: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
5980

60-
sync_wrapper.batch_execute("DELETE FROM users").await?;
81+
diesel::delete(users::table)
82+
.execute(&mut sync_wrapper)
83+
.await?;
6184

62-
sync_wrapper
63-
.batch_execute("INSERT INTO users(id, name) VALUES (3, 'toto')")
85+
diesel::insert_into(users::table)
86+
.values((users::id.eq(3), users::name.eq("toto")))
87+
.execute(&mut sync_wrapper)
6488
.await?;
6589

6690
let data: Vec<User> = users::table
@@ -86,5 +110,28 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
86110
.await?;
87111
println!("{data:?}");
88112

113+
// a quick test to check if we correctly handle transactions
114+
let mut conn_a: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
115+
let mut conn_b: SyncConnectionWrapper<InnerConnection> = establish(&db_url).await?;
116+
117+
let handle_1 = tokio::spawn(async move {
118+
loop {
119+
let changed = transaction(&mut conn_a, "iLuke", "JustLuke").await;
120+
println!("Changed {changed:?}");
121+
std::thread::sleep(std::time::Duration::from_secs(1));
122+
}
123+
});
124+
125+
let handle_2 = tokio::spawn(async move {
126+
loop {
127+
let changed = transaction(&mut conn_b, "JustLuke", "iLuke").await;
128+
println!("Changed {changed:?}");
129+
std::thread::sleep(std::time::Duration::from_secs(1));
130+
}
131+
});
132+
133+
let _ = handle_2.await;
134+
let _ = handle_1.await;
135+
89136
Ok(())
90137
}

src/async_connection_wrapper.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ mod implementation {
107107
pub struct AsyncConnectionWrapper<C, B> {
108108
inner: C,
109109
runtime: B,
110-
instrumentation: Option<Box<dyn Instrumentation>>,
111110
}
112111

113112
impl<C, B> From<C> for AsyncConnectionWrapper<C, B>
@@ -119,7 +118,6 @@ mod implementation {
119118
Self {
120119
inner,
121120
runtime: B::get_runtime(),
122-
instrumentation: None,
123121
}
124122
}
125123
}
@@ -150,11 +148,7 @@ mod implementation {
150148
let runtime = B::get_runtime();
151149
let f = C::establish(database_url);
152150
let inner = runtime.block_on(f)?;
153-
Ok(Self {
154-
inner,
155-
runtime,
156-
instrumentation: None,
157-
})
151+
Ok(Self { inner, runtime })
158152
}
159153

160154
fn execute_returning_count<T>(&mut self, source: &T) -> diesel::QueryResult<usize>
@@ -165,18 +159,18 @@ mod implementation {
165159
self.runtime.block_on(f)
166160
}
167161

168-
fn transaction_state(
169-
&mut self,
162+
fn transaction_state(
163+
&mut self,
170164
) -> &mut <Self::TransactionManager as diesel::connection::TransactionManager<Self>>::TransactionStateData{
171165
self.inner.transaction_state()
172166
}
173167

174168
fn instrumentation(&mut self) -> &mut dyn Instrumentation {
175-
&mut self.instrumentation
169+
self.inner.instrumentation()
176170
}
177171

178172
fn set_instrumentation(&mut self, instrumentation: impl Instrumentation) {
179-
self.instrumentation = Some(Box::new(instrumentation));
173+
self.inner.set_instrumentation(instrumentation);
180174
}
181175
}
182176

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg_attr(doc_cfg, feature(doc_cfg, doc_auto_cfg))]
1+
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
22
//! Diesel-async provides async variants of diesel related query functionality
33
//!
44
//! diesel-async is an extension to diesel itself. It is designed to be used together
@@ -69,6 +69,7 @@
6969
#![warn(missing_docs)]
7070

7171
use diesel::backend::Backend;
72+
use diesel::connection::Instrumentation;
7273
use diesel::query_builder::{AsQuery, QueryFragment, QueryId};
7374
use diesel::result::Error;
7475
use diesel::row::Row;
@@ -347,4 +348,10 @@ pub trait AsyncConnection: SimpleAsyncConnection + Sized + Send {
347348
fn _silence_lint_on_execute_future(_: Self::ExecuteFuture<'_, '_>) {}
348349
#[doc(hidden)]
349350
fn _silence_lint_on_load_future(_: Self::LoadFuture<'_, '_>) {}
351+
352+
#[doc(hidden)]
353+
fn instrumentation(&mut self) -> &mut dyn Instrumentation;
354+
355+
/// Set a specific [`Instrumentation`] implementation for this connection
356+
fn set_instrumentation(&mut self, instrumentation: impl Instrumentation);
350357
}

0 commit comments

Comments
 (0)