a2a_protocol_server/store/tenant_postgres_store/
caller_keys.rs1use super::TenantAwarePostgresTaskStore;
10use crate::store::caller_keys::{
11 CallerErasure, CallerKeyMigration, is_callers_partition, moved_partition,
12};
13
14impl TenantAwarePostgresTaskStore {
15 pub async fn migrate_caller_keys(
25 &self,
26 map: impl Fn(&str) -> Option<String>,
27 ) -> Result<CallerKeyMigration, sqlx::Error> {
28 let mut tx = crate::store::pg_retry::retry(&*self.metrics, || self.pool.begin()).await?;
29 let partitions: Vec<(String,)> = sqlx::query_as(
30 "SELECT tenant_id FROM tenant_tasks UNION SELECT tenant_id FROM tenant_idempotency_keys",
31 )
32 .fetch_all(&mut *tx)
33 .await?;
34 let mut done = CallerKeyMigration::default();
35 for (old,) in partitions {
36 let Some(new) = moved_partition(&old, &map) else {
37 continue;
38 };
39 let tasks = sqlx::query(
43 "INSERT INTO tenant_tasks (tenant_id, id, context_id, state, data, updated_at, created_at, applied_seq)
44 SELECT $1, id, context_id, state, data, updated_at, created_at, applied_seq
45 FROM tenant_tasks WHERE tenant_id = $2 AND set_config('a2a.claims', 'on', true) = 'on'",
46 )
47 .bind(&new)
48 .bind(&old)
49 .execute(&mut *tx)
50 .await?
51 .rows_affected();
52 let events = sqlx::query(
53 "UPDATE tenant_task_events SET tenant_id = $1 \
54 WHERE tenant_id = $2 AND set_config('a2a.claims', 'on', true) = 'on'",
55 )
56 .bind(&new)
57 .bind(&old)
58 .execute(&mut *tx)
59 .await?
60 .rows_affected();
61 sqlx::query("DELETE FROM tenant_tasks WHERE tenant_id = $1")
62 .bind(&old)
63 .execute(&mut *tx)
64 .await?;
65 let keys = sqlx::query(
66 "UPDATE tenant_idempotency_keys SET tenant_id = $1 WHERE tenant_id = $2",
67 )
68 .bind(&new)
69 .bind(&old)
70 .execute(&mut *tx)
71 .await?
72 .rows_affected();
73 done.partitions += 1;
74 done.rows += tasks + events + keys;
75 }
76 tx.commit().await?;
77 Ok(done)
78 }
79}
80
81impl TenantAwarePostgresTaskStore {
82 pub async fn erase_caller(&self, caller: &str) -> Result<CallerErasure, sqlx::Error> {
90 let mut tx = crate::store::pg_retry::retry(&*self.metrics, || self.pool.begin()).await?;
91 let partitions: Vec<(String,)> = sqlx::query_as(
92 "SELECT tenant_id FROM tenant_tasks UNION SELECT tenant_id FROM tenant_idempotency_keys
93 UNION SELECT tenant_id FROM tenant_task_events",
94 )
95 .fetch_all(&mut *tx)
96 .await?;
97 let mut done = CallerErasure::default();
98 for (partition,) in partitions {
99 if !is_callers_partition(&partition, caller) {
100 continue;
101 }
102 for table in [
104 "tenant_task_events",
105 "tenant_tasks",
106 "tenant_idempotency_keys",
107 ] {
108 done.rows += sqlx::query(sqlx::AssertSqlSafe(format!(
110 "DELETE FROM {table} WHERE tenant_id = $1"
111 )))
112 .bind(&partition)
113 .execute(&mut *tx)
114 .await?
115 .rows_affected();
116 }
117 done.partitions += 1;
118 }
119 tx.commit().await?;
120 Ok(done)
121 }
122}