a2a_protocol_server/push/tenant_sqlite_config_store/
caller_keys.rs1use a2a_protocol_types::error::A2aResult;
10
11use super::{Sealing, TenantAwareSqlitePushConfigStore, to_a2a_error};
12use crate::store::caller_keys::{
13 CallerErasure, CallerKeyMigration, is_callers_partition, moved_partition,
14};
15
16impl TenantAwareSqlitePushConfigStore {
17 pub async fn migrate_caller_keys(
30 &self,
31 map: impl Fn(&str) -> Option<String>,
32 ) -> A2aResult<CallerKeyMigration> {
33 let mut tx = self.pool.begin().await.map_err(|e| to_a2a_error(&e))?;
34 let partitions: Vec<(String,)> =
35 sqlx::query_as("SELECT DISTINCT tenant_id FROM tenant_push_configs")
36 .fetch_all(&mut *tx)
37 .await
38 .map_err(|e| to_a2a_error(&e))?;
39 let mut done = CallerKeyMigration::default();
40 for (old,) in partitions {
41 let Some(new) = moved_partition(&old, &map) else {
42 continue;
43 };
44 let rows: Vec<(String, String, String)> = sqlx::query_as(
45 "SELECT task_id, id, data FROM tenant_push_configs WHERE tenant_id = ?1",
46 )
47 .bind(&old)
48 .fetch_all(&mut *tx)
49 .await
50 .map_err(|e| to_a2a_error(&e))?;
51 for (task_id, id, data) in rows {
52 let config = self
53 .sealing
54 .decode_text(&data, &Sealing::context(&old, &task_id, &id))
55 .await?;
56 let data = self
57 .sealing
58 .encode_text(&config, &Sealing::context(&new, &task_id, &id))
59 .await?;
60 sqlx::query(
61 "UPDATE tenant_push_configs SET tenant_id = ?1, data = ?2
62 WHERE tenant_id = ?3 AND task_id = ?4 AND id = ?5",
63 )
64 .bind(&new)
65 .bind(&data)
66 .bind(&old)
67 .bind(&task_id)
68 .bind(&id)
69 .execute(&mut *tx)
70 .await
71 .map_err(|e| to_a2a_error(&e))?;
72 done.rows += 1;
73 }
74 done.partitions += 1;
75 }
76 tx.commit().await.map_err(|e| to_a2a_error(&e))?;
77 Ok(done)
78 }
79}
80
81impl TenantAwareSqlitePushConfigStore {
82 pub async fn erase_caller(&self, caller: &str) -> A2aResult<CallerErasure> {
90 let mut tx = self.pool.begin().await.map_err(|e| to_a2a_error(&e))?;
91 let partitions: Vec<(String,)> =
92 sqlx::query_as("SELECT DISTINCT tenant_id FROM tenant_push_configs")
93 .fetch_all(&mut *tx)
94 .await
95 .map_err(|e| to_a2a_error(&e))?;
96 let mut done = CallerErasure::default();
97 for (partition,) in partitions {
98 if !is_callers_partition(&partition, caller) {
99 continue;
100 }
101 done.rows += sqlx::query("DELETE FROM tenant_push_configs WHERE tenant_id = ?1")
102 .bind(&partition)
103 .execute(&mut *tx)
104 .await
105 .map_err(|e| to_a2a_error(&e))?
106 .rows_affected();
107 done.partitions += 1;
108 }
109 tx.commit().await.map_err(|e| to_a2a_error(&e))?;
110 Ok(done)
111 }
112}