#[macro_export]
macro_rules! owner_reassignment {
(
$vis:vis struct $name:ident;
domain = $domain:literal;
$( $kind:ident $table:literal => $sql:literal; )+
) => {
#[derive(Debug, Clone)]
$vis struct $name {
write_pool: ::std::sync::Arc<::sqlx::PgPool>,
}
impl $name {
pub fn new(db: &$crate::DbPool) -> Self {
Self {
write_pool: db.write_pool(),
}
}
}
#[::async_trait::async_trait]
impl ::systemprompt_traits::OwnerReassignment for $name {
fn domain(&self) -> &'static str {
$domain
}
async fn reassign_owner(
&self,
from: &::systemprompt_identifiers::UserId,
to: &::systemprompt_identifiers::UserId,
) -> ::std::result::Result<
::systemprompt_traits::ReassignedRows,
::systemprompt_traits::RepositoryError,
> {
let mut tx = self.write_pool.begin().await?;
let mut tables = ::std::vec::Vec::new();
$(
let rows = $crate::owner_reassignment!(@step $kind tx from to $sql);
tables.push(($table, rows));
)+
tx.commit().await?;
Ok(::systemprompt_traits::ReassignedRows { tables })
}
}
};
(@step update $tx:ident $from:ident $to:ident $sql:literal) => {
::sqlx::query!($sql, $from.as_str(), $to.as_str())
.execute(&mut *$tx)
.await?
.rows_affected()
};
(@step delete $tx:ident $from:ident $to:ident $sql:literal) => {
::sqlx::query!($sql, $from.as_str())
.execute(&mut *$tx)
.await?
.rows_affected()
};
}