Skip to main content

systemprompt_files/repository/
ownership.rs

1//! [`OwnerReassignment`] for the files domain: moves every file row from one
2//! user to another.
3//!
4//! Copyright (c) systemprompt.io — Business Source License 1.1.
5//! See <https://systemprompt.io> for licensing details.
6
7use async_trait::async_trait;
8use sqlx::PgPool;
9use std::sync::Arc;
10use systemprompt_database::DbPool;
11use systemprompt_identifiers::UserId;
12use systemprompt_traits::{OwnerReassignment, ReassignedRows, RepositoryError};
13
14#[derive(Debug, Clone)]
15pub struct FilesOwnerReassignment {
16    write_pool: Arc<PgPool>,
17}
18
19impl FilesOwnerReassignment {
20    pub fn new(db: &DbPool) -> Self {
21        let write_pool = db.write_pool();
22        Self { write_pool }
23    }
24}
25
26#[async_trait]
27impl OwnerReassignment for FilesOwnerReassignment {
28    fn domain(&self) -> &'static str {
29        "files"
30    }
31
32    async fn reassign_owner(
33        &self,
34        from: &UserId,
35        to: &UserId,
36    ) -> Result<ReassignedRows, RepositoryError> {
37        let files = sqlx::query!(
38            "UPDATE files SET user_id = $2 WHERE user_id = $1",
39            from.as_str(),
40            to.as_str()
41        )
42        .execute(self.write_pool.as_ref())
43        .await
44        .map_err(RepositoryError::database)?
45        .rows_affected();
46
47        Ok(ReassignedRows {
48            tables: vec![("files", files)],
49        })
50    }
51}