diff --git a/sqlx-core/src/transaction.rs b/sqlx-core/src/transaction.rs index 8ad91e6b..0dfd50a4 100644 --- a/sqlx-core/src/transaction.rs +++ b/sqlx-core/src/transaction.rs @@ -197,6 +197,16 @@ where } } +// Implement `AsMut` so `Transaction` can be given to a +// `PgAdvisoryLockGuard`. +// +// See: https://github.com/launchbadge/sqlx/issues/2520 +impl<'c, DB: Database> AsMut for Transaction<'c, DB> { + fn as_mut(&mut self) -> &mut DB::Connection { + &mut self.connection + } +} + impl<'c, 't, DB: Database> crate::acquire::Acquire<'t> for &'t mut Transaction<'c, DB> { type Database = DB; diff --git a/sqlx-postgres/src/connection/mod.rs b/sqlx-postgres/src/connection/mod.rs index 2bbc38fa..6259033b 100644 --- a/sqlx-postgres/src/connection/mod.rs +++ b/sqlx-postgres/src/connection/mod.rs @@ -213,3 +213,13 @@ impl Connection for PgConnection { !self.stream.write_buffer().is_empty() } } + +// Implement `AsMut` so that `PgConnection` can be wrapped in +// a `PgAdvisoryLockGuard`. +// +// See: https://github.com/launchbadge/sqlx/issues/2520 +impl AsMut for PgConnection { + fn as_mut(&mut self) -> &mut PgConnection { + self + } +}