From 3abcce2eec55208993dd9c7c3e51cff83412d15a Mon Sep 17 00:00:00 2001 From: dull b Date: Tue, 25 Jul 2023 17:52:49 +0000 Subject: [PATCH] Fix Crud::delete --- crates/db_schema/src/traits.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index b7702acba..736bceb95 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -21,7 +21,8 @@ use diesel_async::{ AsyncPgConnection, RunQueryDsl, }; -use std::hash::Hash; +use futures_util::{Future, TryFutureExt}; +use std::{hash::Hash, pin::Pin}; /// Returned by `diesel::delete` pub type Delete = DeleteStatement<::Table, ::WhereClause>; @@ -33,12 +34,13 @@ pub trait Crud<'a> where Self: HasTable + Sized, for<'b> Self::Table: FindDsl<>::IdType> + 'static, - for<'b> Find<'b, Self>: LimitDsl + Send + IntoUpdateTarget + 'b, - for<'b, 'query> dsl::Limit>: - Send + LoadQuery<'query, AsyncPgConnection, Self> + 'query, + for<'b> Find<'b, Self>: LimitDsl + Send + IntoUpdateTarget + 'static, + for<'b> dsl::Limit>: Send + LoadQuery<'static, AsyncPgConnection, Self> + 'static, ::PrimaryKey: ExpressionMethods + Send, <::PrimaryKey as Expression>::SqlType: SqlType + TypedExpressionType, - for<'b> Delete>: ExecuteDsl + Send + 'b, + for<'b> Delete>: ExecuteDsl + Send + 'static, + for<'b> as IntoUpdateTarget>::WhereClause: 'static + Send, + for<'b> as HasTable>::Table: 'static + Send, { /*for<'a> &'a Self::InsertForm: Insertable, for<'a> InsertStatement>::Values>: @@ -46,7 +48,8 @@ where for<'a> <&'a Self::InsertForm as Insertable>::Values: 'a,*/ type InsertForm; type UpdateForm; - type IdType: Hash + type IdType: 'static + + Hash + Eq + Sized + Send @@ -82,13 +85,18 @@ where .get_result::(conn) .await }*/ - async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result + fn delete<'life0, 'life1, 'async_trait>( + pool: &'life0 mut DbPool<'life1>, + id: Self::IdType, + ) -> Pin> + Send + 'async_trait>> where 'a: 'async_trait, + 'life0: 'async_trait, + 'life1: 'async_trait, + Self: Send + 'async_trait, { - let query = diesel::delete(Self::table().find(id)); - let conn = &mut *get_conn(pool).await?; - query.execute(conn).await + let query: Delete> = diesel::delete(Self::table().find(id)); + Box::pin(get_conn(pool).and_then(move |mut conn| query.execute(&mut *conn))) } }