a2a_protocol_server/store/tenant_sqlite_store/
caller_keys.rs1use super::TenantAwareSqliteTaskStore;
10use crate::store::caller_keys::{
11 CallerErasure, CallerKeyMigration, is_callers_partition, moved_partition,
12};
13
14impl TenantAwareSqliteTaskStore {
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 = 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",
46 )
47 .bind(&new)
48 .bind(&old)
49 .execute(&mut *tx)
50 .await?
51 .rows_affected();
52 let events =
53 sqlx::query("UPDATE tenant_task_events SET tenant_id = ?1 WHERE tenant_id = ?2")
54 .bind(&new)
55 .bind(&old)
56 .execute(&mut *tx)
57 .await?
58 .rows_affected();
59 sqlx::query("DELETE FROM tenant_tasks WHERE tenant_id = ?1")
60 .bind(&old)
61 .execute(&mut *tx)
62 .await?;
63 let keys = sqlx::query(
64 "UPDATE tenant_idempotency_keys SET tenant_id = ?1 WHERE tenant_id = ?2",
65 )
66 .bind(&new)
67 .bind(&old)
68 .execute(&mut *tx)
69 .await?
70 .rows_affected();
71 done.partitions += 1;
72 done.rows += tasks + events + keys;
73 }
74 tx.commit().await?;
75 Ok(done)
76 }
77}
78
79impl TenantAwareSqliteTaskStore {
80 pub async fn erase_caller(&self, caller: &str) -> Result<CallerErasure, sqlx::Error> {
88 let mut tx = self.pool.begin().await?;
89 let partitions: Vec<(String,)> = sqlx::query_as(
90 "SELECT tenant_id FROM tenant_tasks UNION SELECT tenant_id FROM tenant_idempotency_keys
91 UNION SELECT tenant_id FROM tenant_task_events",
92 )
93 .fetch_all(&mut *tx)
94 .await?;
95 let mut done = CallerErasure::default();
96 for (partition,) in partitions {
97 if !is_callers_partition(&partition, caller) {
98 continue;
99 }
100 for table in [
102 "tenant_task_events",
103 "tenant_tasks",
104 "tenant_idempotency_keys",
105 ] {
106 done.rows += sqlx::query(sqlx::AssertSqlSafe(format!(
108 "DELETE FROM {table} WHERE tenant_id = ?1"
109 )))
110 .bind(&partition)
111 .execute(&mut *tx)
112 .await?
113 .rows_affected();
114 }
115 done.partitions += 1;
116 }
117 tx.commit().await?;
118 Ok(done)
119 }
120}