Skip to content

Commit 7aa1016

Browse files
committed
editoast: db connection v3
1 parent c1359a6 commit 7aa1016

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1015
-1157
lines changed

editoast/editoast_derive/src/modelv2/codegen/count_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl ToTokens for CountImpl {
2828
use diesel::QueryDsl;
2929
use diesel_async::RunQueryDsl;
3030
use futures_util::stream::TryStreamExt;
31+
use std::ops::DerefMut;
3132

3233
let mut query = #table_mod::table.select(diesel::dsl::count_star()).into_boxed();
3334

@@ -48,7 +49,7 @@ impl ToTokens for CountImpl {
4849
}
4950
}
5051

51-
Ok(query.get_result::<i64>(conn).await? as u64)
52+
Ok(query.get_result::<i64>(conn.write().await.deref_mut()).await? as u64)
5253
}
5354
}
5455

editoast/editoast_derive/src/modelv2/codegen/create_batch_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl ToTokens for CreateBatchImpl {
3838
) -> crate::error::Result<C> {
3939
use crate::modelsv2::Model;
4040
use #table_mod::dsl;
41+
use std::ops::DerefMut;
4142
use diesel::prelude::*;
4243
use diesel_async::RunQueryDsl;
4344
use futures_util::stream::TryStreamExt;
@@ -50,7 +51,7 @@ impl ToTokens for CreateBatchImpl {
5051
chunk => {
5152
diesel::insert_into(dsl::#table_name)
5253
.values(chunk)
53-
.load_stream::<#row>(conn)
54+
.load_stream::<#row>(conn.write().await.deref_mut())
5455
.await
5556
.map(|s| s.map_ok(<#model as Model>::from_row).try_collect::<Vec<_>>())?
5657
.await?

editoast/editoast_derive/src/modelv2/codegen/create_batch_with_key_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl ToTokens for CreateBatchWithKeyImpl {
4343
) -> crate::error::Result<C> {
4444
use crate::models::Identifiable;
4545
use crate::modelsv2::Model;
46+
use std::ops::DerefMut;
4647
use #table_mod::dsl;
4748
use diesel::prelude::*;
4849
use diesel_async::RunQueryDsl;
@@ -56,7 +57,7 @@ impl ToTokens for CreateBatchWithKeyImpl {
5657
chunk => {
5758
diesel::insert_into(dsl::#table_name)
5859
.values(chunk)
59-
.load_stream::<#row>(conn)
60+
.load_stream::<#row>(conn.write().await.deref_mut())
6061
.await
6162
.map(|s| {
6263
s.map_ok(|row| {

editoast/editoast_derive/src/modelv2/codegen/create_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ impl ToTokens for CreateImpl {
2828
conn: &mut editoast_models::DbConnection,
2929
) -> crate::error::Result<#model> {
3030
use diesel_async::RunQueryDsl;
31+
use std::ops::DerefMut;
3132
diesel::insert_into(#table_mod::table)
3233
.values(&self)
33-
.get_result::<#row>(conn)
34+
.get_result::<#row>(conn.write().await.deref_mut())
3435
.await
3536
.map(Into::into)
3637
.map_err(Into::into)

editoast/editoast_derive/src/modelv2/codegen/delete_batch_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ impl ToTokens for DeleteBatchImpl {
3838
use #table_mod::dsl;
3939
use diesel::prelude::*;
4040
use diesel_async::RunQueryDsl;
41+
use std::ops::DerefMut;
4142
let ids = ids.into_iter().collect::<Vec<_>>();
4243
tracing::Span::current().record("query_ids", tracing::field::debug(&ids));
4344
let counts = crate::chunked_for_libpq! {
@@ -49,7 +50,7 @@ impl ToTokens for DeleteBatchImpl {
4950
for #id_ident in chunk.into_iter() {
5051
query = query.or_filter(#filters);
5152
}
52-
query.execute(conn).await?
53+
query.execute(conn.write().await.deref_mut()).await?
5354
}
5455
};
5556
Ok(counts.into_iter().sum())

editoast/editoast_derive/src/modelv2/codegen/delete_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ impl ToTokens for DeleteImpl {
2828
use diesel::prelude::*;
2929
use diesel_async::RunQueryDsl;
3030
use #table_mod::dsl;
31+
use std::ops::DerefMut;
3132
let id = self.#primary_key;
3233
diesel::delete(#table_mod::table.find(id))
33-
.execute(conn)
34+
.execute(conn.write().await.deref_mut())
3435
.await
3536
.map(|n| n == 1)
3637
.map_err(Into::into)

editoast/editoast_derive/src/modelv2/codegen/delete_static_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ impl ToTokens for DeleteStaticImpl {
3535
) -> crate::error::Result<bool> {
3636
use diesel::prelude::*;
3737
use diesel_async::RunQueryDsl;
38+
use std::ops::DerefMut;
3839
use #table_mod::dsl;
3940
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
4041
diesel::delete(dsl::#table_name.#(filter(#eqs)).*)
41-
.execute(conn)
42+
.execute(conn.write().await.deref_mut())
4243
.await
4344
.map(|n| n == 1)
4445
.map_err(Into::into)

editoast/editoast_derive/src/modelv2/codegen/exists_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ impl ToTokens for ExistsImpl {
3535
) -> crate::error::Result<bool> {
3636
use diesel::prelude::*;
3737
use diesel_async::RunQueryDsl;
38+
use std::ops::DerefMut;
3839
use #table_mod::dsl;
3940
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
4041
diesel::select(diesel::dsl::exists(dsl::#table_name.#(filter(#eqs)).*))
41-
.get_result(conn)
42+
.get_result(conn.write().await.deref_mut())
4243
.await
4344
.map_err(Into::into)
4445
}

editoast/editoast_derive/src/modelv2/codegen/list_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl ToTokens for ListImpl {
3434
use diesel::QueryDsl;
3535
use diesel_async::RunQueryDsl;
3636
use futures_util::stream::TryStreamExt;
37+
use std::ops::DerefMut;
3738

3839
let mut query = #table_mod::table.into_boxed();
3940

@@ -58,7 +59,7 @@ impl ToTokens for ListImpl {
5859
}
5960

6061
let results: Vec<#model> = query
61-
.load_stream::<#row>(conn)
62+
.load_stream::<#row>(conn.write().await.deref_mut())
6263
.await?
6364
.map_ok(<#model as crate::modelsv2::prelude::Model>::from_row)
6465
.try_collect()

editoast/editoast_derive/src/modelv2/codegen/retrieve_batch_impl.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl ToTokens for RetrieveBatchImpl {
4646
use diesel::prelude::*;
4747
use diesel_async::RunQueryDsl;
4848
use futures_util::stream::TryStreamExt;
49+
use std::ops::DerefMut;
4950
let ids = ids.into_iter().collect::<Vec<_>>();
5051
tracing::Span::current().record("query_ids", tracing::field::debug(&ids));
5152
Ok(crate::chunked_for_libpq! {
@@ -62,7 +63,7 @@ impl ToTokens for RetrieveBatchImpl {
6263
query = query.or_filter(#filters);
6364
}
6465
query
65-
.load_stream::<#row>(conn)
66+
.load_stream::<#row>(conn.write().await.deref_mut())
6667
.await
6768
.map(|s| s.map_ok(<#model as Model>::from_row).try_collect::<Vec<_>>())?
6869
.await?
@@ -84,6 +85,7 @@ impl ToTokens for RetrieveBatchImpl {
8485
use diesel::prelude::*;
8586
use diesel_async::RunQueryDsl;
8687
use futures_util::stream::TryStreamExt;
88+
use std::ops::DerefMut;
8789
let ids = ids.into_iter().collect::<Vec<_>>();
8890
tracing::Span::current().record("query_ids", tracing::field::debug(&ids));
8991
Ok(crate::chunked_for_libpq! {
@@ -97,7 +99,7 @@ impl ToTokens for RetrieveBatchImpl {
9799
query = query.or_filter(#filters);
98100
}
99101
query
100-
.load_stream::<#row>(conn)
102+
.load_stream::<#row>(conn.write().await.deref_mut())
101103
.await
102104
.map(|s| {
103105
s.map_ok(|row| {

editoast/editoast_derive/src/modelv2/codegen/retrieve_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ impl ToTokens for RetrieveImpl {
3838
use diesel::prelude::*;
3939
use diesel_async::RunQueryDsl;
4040
use #table_mod::dsl;
41+
use std::ops::DerefMut;
4142
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
4243
dsl::#table_name
4344
.#(filter(#eqs)).*
44-
.first::<#row>(conn)
45+
.first::<#row>(conn.write().await.deref_mut())
4546
.await
4647
.map(Into::into)
4748
.optional()

editoast/editoast_derive/src/modelv2/codegen/update_batch_impl.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ impl ToTokens for UpdateBatchImpl {
5151
use diesel::prelude::*;
5252
use diesel_async::RunQueryDsl;
5353
use futures_util::stream::TryStreamExt;
54+
use std::ops::DerefMut;
5455
let ids = ids.into_iter().collect::<Vec<_>>();
5556
tracing::Span::current().record("query_ids", tracing::field::debug(&ids));
5657
Ok(crate::chunked_for_libpq! {
@@ -69,7 +70,7 @@ impl ToTokens for UpdateBatchImpl {
6970
diesel::update(dsl::#table_name)
7071
.filter(dsl::#primary_key_column.eq_any(query))
7172
.set(&self)
72-
.load_stream::<#row>(conn)
73+
.load_stream::<#row>(conn.write().await.deref_mut())
7374
.await
7475
.map(|s| s.map_ok(<#model as Model>::from_row).try_collect::<Vec<_>>())?
7576
.await?
@@ -89,6 +90,7 @@ impl ToTokens for UpdateBatchImpl {
8990
use crate::models::Identifiable;
9091
use crate::modelsv2::Model;
9192
use #table_mod::dsl;
93+
use std::ops::DerefMut;
9294
use diesel::prelude::*;
9395
use diesel_async::RunQueryDsl;
9496
use futures_util::stream::TryStreamExt;
@@ -109,7 +111,7 @@ impl ToTokens for UpdateBatchImpl {
109111
diesel::update(dsl::#table_name)
110112
.filter(dsl::#primary_key_column.eq_any(query))
111113
.set(&self)
112-
.load_stream::<#row>(conn)
114+
.load_stream::<#row>(conn.write().await.deref_mut())
113115
.await
114116
.map(|s| {
115117
s.map_ok(|row| {

editoast/editoast_derive/src/modelv2/codegen/update_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ impl ToTokens for UpdateImpl {
4040
) -> crate::error::Result<Option<#model>> {
4141
use diesel::prelude::*;
4242
use diesel_async::RunQueryDsl;
43+
use std::ops::DerefMut;
4344
use #table_mod::dsl;
4445
tracing::Span::current().record("query_id", tracing::field::debug(#id_ref_ident));
4546
diesel::update(dsl::#table_name.#(filter(#eqs)).*)
4647
.set(&self)
47-
.get_result::<#row>(conn)
48+
.get_result::<#row>(conn.write().await.deref_mut())
4849
.await
4950
.map(Into::into)
5051
.optional()

editoast/editoast_models/src/db_connection_pool.rs

+19-27
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,18 @@ impl DbConnectionV3 {
6868

6969
{
7070
let mut handle = self.write().await;
71-
TxManager::begin_transaction(
72-
handle.deref_mut(),
73-
)
74-
.await?;
71+
TxManager::begin_transaction(handle.deref_mut()).await?;
7572
}
7673

7774
match callback(self.clone()).await {
7875
Ok(result) => {
7976
let mut handle = self.write().await;
80-
TxManager::commit_transaction(
81-
handle.deref_mut(),
82-
)
83-
.await?;
77+
TxManager::commit_transaction(handle.deref_mut()).await?;
8478
Ok(result)
8579
}
8680
Err(callback_error) => {
8781
let mut handle = self.write().await;
88-
match TxManager::rollback_transaction(
89-
handle.deref_mut(),
90-
)
91-
.await
92-
{
82+
match TxManager::rollback_transaction(handle.deref_mut()).await {
9383
Ok(()) | Err(diesel::result::Error::BrokenTransactionManager) => {
9484
Err(callback_error)
9585
}
@@ -174,7 +164,7 @@ impl DbConnectionPoolV2 {
174164
}
175165

176166
#[cfg(feature = "testing")]
177-
async fn get_connection(&self) -> Result<DbConnectionV2, DatabasePoolError> {
167+
async fn get_connection_v1(&self) -> Result<DbConnectionV2, DatabasePoolError> {
178168
let Some(test_connection) = &self.test_connection else {
179169
panic!(
180170
"Test connection not initialized in test DatabasePool -- was `for_tests` called?"
@@ -185,7 +175,7 @@ impl DbConnectionPoolV2 {
185175
}
186176

187177
#[cfg(feature = "testing")]
188-
async fn get_connection_v3(&self) -> Result<DbConnectionV3, DatabasePoolError> {
178+
async fn get_connection(&self) -> Result<DbConnectionV3, DatabasePoolError> {
189179
Ok(self
190180
.test_connection_v3
191181
.as_ref()
@@ -194,13 +184,13 @@ impl DbConnectionPoolV2 {
194184
}
195185

196186
#[cfg(not(feature = "testing"))]
197-
async fn get_connection(&self) -> Result<DbConnectionV2, DatabasePoolError> {
187+
async fn get_connection_v1(&self) -> Result<DbConnectionV2, DatabasePoolError> {
198188
let connection = self.pool.get().await?;
199189
Ok(connection)
200190
}
201191

202192
#[cfg(not(feature = "testing"))]
203-
async fn get_connection_v3(&self) -> Result<DbConnectionV3, DatabasePoolError> {
193+
async fn get_connection(&self) -> Result<DbConnectionV3, DatabasePoolError> {
204194
let connection = self.pool.get().await?;
205195
Ok(DbConnectionV3::new(Arc::new(RwLock::new(connection))))
206196
}
@@ -334,17 +324,17 @@ impl DbConnectionPoolV2 {
334324
/// # Ok(())
335325
/// # }
336326
/// ```
337-
pub async fn get(&self) -> Result<DbConnectionV2, DatabasePoolError> {
338-
self.get_connection().await
327+
pub async fn get_v1(&self) -> Result<DbConnectionV2, DatabasePoolError> {
328+
self.get_connection_v1().await
339329
}
340330

341-
pub async fn get_v3(&self) -> Result<DbConnectionV3, DatabasePoolError> {
342-
self.get_connection_v3().await
331+
pub async fn get(&self) -> Result<DbConnectionV3, DatabasePoolError> {
332+
self.get_connection().await
343333
}
344334

345335
#[cfg(feature = "testing")]
346-
pub fn get_ok_v3(&self) -> DbConnectionV3 {
347-
futures::executor::block_on(self.get_v3()).expect("Failed to get test connection")
336+
pub fn get_ok(&self) -> DbConnectionV3 {
337+
futures::executor::block_on(self.get()).expect("Failed to get test connection")
348338
}
349339

350340
/// Gets a test connection from the pool synchronously, failing if the connection is not available
@@ -354,8 +344,8 @@ impl DbConnectionPoolV2 {
354344
/// See [DbConnectionPoolV2::get] for more information on how connections should be used
355345
/// in tests.
356346
#[cfg(feature = "testing")]
357-
pub fn get_ok(&self) -> DbConnectionV2 {
358-
futures::executor::block_on(self.get()).expect("Failed to get test connection")
347+
pub fn get_ok_v1(&self) -> DbConnectionV2 {
348+
futures::executor::block_on(self.get_v1()).expect("Failed to get test connection")
359349
}
360350

361351
/// Returns an infinite iterator of futures resolving to connections acquired from the pool
@@ -390,7 +380,7 @@ impl DbConnectionPoolV2 {
390380
/// ```
391381
pub fn iter_conn(
392382
&self,
393-
) -> impl Iterator<Item = impl Future<Output = Result<DbConnectionV2, DatabasePoolError>> + '_>
383+
) -> impl Iterator<Item = impl Future<Output = Result<DbConnection, DatabasePoolError>> + '_>
394384
{
395385
std::iter::repeat_with(|| self.get())
396386
}
@@ -472,7 +462,9 @@ impl DbConnectionPoolV2 {
472462
pub struct PingError(#[from] diesel::result::Error);
473463

474464
pub async fn ping_database(conn: &mut DbConnection) -> Result<(), PingError> {
475-
sql_query("SELECT 1").execute(conn).await?;
465+
sql_query("SELECT 1")
466+
.execute(conn.write().await.deref_mut())
467+
.await?;
476468
Ok(())
477469
}
478470

editoast/editoast_models/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod tables;
88
pub use db_connection_pool::DbConnectionPoolV2;
99
pub use db_connection_pool::DbConnectionV3;
1010

11-
pub type DbConnection = AsyncPgConnection;
11+
pub type DbConnection = DbConnectionV3;
1212
pub type DieselConnection = AsyncPgConnection;
1313
pub type DbConnectionPool = Pool<DieselConnection>;
1414

0 commit comments

Comments
 (0)