udb 0.2.0

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! MySQL implementation of [`ProjectionTaskStore`].
//!
//! ## Dialect choices
//!
//! - **No RETURNING**. MySQL doesn't support RETURNING on UPDATE
//!   (MariaDB has it; vanilla MySQL doesn't). We work around by
//!   selecting the claim candidate IDs first inside a transaction,
//!   then UPDATE-ing them by ID, then SELECT-ing the full rows.
//!   The transaction provides atomicity.
//! - **`FOR UPDATE SKIP LOCKED`** — requires MySQL 8.0.1+ (2017-09).
//!   Older MySQL falls back to plain `FOR UPDATE` which serialises
//!   claims (correctness preserved, throughput reduced).
//! - **Auto-increment for `task_id`**: MySQL has no native UUID type
//!   short of CHAR(36). We generate the UUID Rust-side and pass it
//!   in; the column is `CHAR(36) NOT NULL` with the PRIMARY KEY.
//! - **JSON columns**: native `JSON` column type (MySQL 5.7+).
//! - **Timestamps**: `TIMESTAMP(6)` for microsecond precision with
//!   `DEFAULT CURRENT_TIMESTAMP(6)`.
//! - **`ON DUPLICATE KEY UPDATE id=id`** idempotency idiom for
//!   enqueue — same effect as PG's `ON CONFLICT DO NOTHING`.
//! - **CHECK constraints**: supported since MySQL 8.0.16; enforced
//!   for `status` and `operation`.

use std::time::Duration;

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use sqlx::Row;
use uuid::Uuid;

use super::dialect::apply_projection_summary_bucket;
use super::mysql::MysqlCanonicalStore;
use super::system_store::{
    DeadLetterGroup, PendingTaskMetric, ProjectionClaimFilter, ProjectionOperation,
    ProjectionTaskInsert, ProjectionTaskRow, ProjectionTaskStatus, ProjectionTaskStore,
    ProjectionTaskSummary, SystemStoreError, SystemStoreResult,
};

const TABLE: &str = "udb_projection_tasks";

fn projection_retry_delay_secs(retry_count: i32) -> i64 {
    let attempt = retry_count.max(1).min(12) as u32;
    (1_i64 << (attempt - 1)).min(3600)
}

impl MysqlCanonicalStore {
    /// Pool getter for the projection impl.
    pub(crate) fn mysql_pool(&self) -> &sqlx::MySqlPool {
        &self.pool
    }
}

fn row_to_projection_task(row: sqlx::mysql::MySqlRow) -> SystemStoreResult<ProjectionTaskRow> {
    let task_id_str: String = row
        .try_get("task_id")
        .map_err(|e| SystemStoreError::query("mysql", "SELECT task_id", e))?;
    let task_id = Uuid::parse_str(&task_id_str).map_err(|e| {
        SystemStoreError::InvalidInput(format!("task_id '{task_id_str}' is not a valid UUID: {e}"))
    })?;
    let operation_str: String = row
        .try_get("operation")
        .map_err(|e| SystemStoreError::query("mysql", "SELECT operation", e))?;
    let operation = ProjectionOperation::parse(&operation_str).ok_or_else(|| {
        SystemStoreError::InvalidInput(format!(
            "unknown projection operation '{operation_str}' in MySQL row"
        ))
    })?;
    let status_str: String = row
        .try_get("status")
        .map_err(|e| SystemStoreError::query("mysql", "SELECT status", e))?;
    let status = ProjectionTaskStatus::parse(&status_str).ok_or_else(|| {
        SystemStoreError::InvalidInput(format!(
            "unknown projection status '{status_str}' in MySQL row"
        ))
    })?;

    let source_row_key: serde_json::Value = row
        .try_get("source_row_key")
        .unwrap_or(serde_json::Value::Null);
    let target_options: serde_json::Value = row
        .try_get("target_options")
        .unwrap_or(serde_json::Value::Null);
    let source_payload: serde_json::Value = row
        .try_get("source_payload")
        .unwrap_or(serde_json::Value::Null);

    Ok(ProjectionTaskRow {
        task_id,
        idempotency_key: row.try_get("idempotency_key").unwrap_or_default(),
        project_id: row.try_get("project_id").unwrap_or_default(),
        target_backend: row.try_get("target_backend").unwrap_or_default(),
        target_instance: row.try_get("target_instance").unwrap_or_default(),
        projection_kind: row.try_get("projection_kind").unwrap_or_default(),
        resource_name: row.try_get("resource_name").unwrap_or_default(),
        operation,
        source_row_key,
        target_options,
        source_payload,
        source_checksum: row.try_get("source_checksum").unwrap_or_default(),
        status,
        retry_count: row.try_get("retry_count").unwrap_or(0),
        last_error: row.try_get("last_error").unwrap_or_default(),
        created_at: row
            .try_get::<DateTime<Utc>, _>("created_at")
            .unwrap_or_else(|_| Utc::now()),
        updated_at: row
            .try_get::<DateTime<Utc>, _>("updated_at")
            .unwrap_or_else(|_| Utc::now()),
        next_retry_at: row
            .try_get::<Option<DateTime<Utc>>, _>("next_retry_at")
            .ok()
            .flatten(),
        completed_at: row
            .try_get::<Option<DateTime<Utc>>, _>("completed_at")
            .ok()
            .flatten(),
    })
}

#[async_trait]
impl ProjectionTaskStore for MysqlCanonicalStore {
    fn backend_label(&self) -> &'static str {
        "mysql"
    }

    async fn ensure_projection_tables(&self) -> SystemStoreResult<()> {
        // DDL applied in three separate statements so each is
        // independently idempotent. `CREATE TABLE IF NOT EXISTS` +
        // `CREATE INDEX` (no IF NOT EXISTS in older MySQL — wrap in
        // a session var trick below).
        let create_table = format!(
            r#"
            CREATE TABLE IF NOT EXISTS {TABLE} (
                task_id            CHAR(36) NOT NULL PRIMARY KEY,
                idempotency_key    VARCHAR(255) NOT NULL UNIQUE,
                project_id         VARCHAR(255) NOT NULL DEFAULT '',
                manifest_checksum  VARCHAR(255) NOT NULL DEFAULT '',
                message_type       VARCHAR(255) NOT NULL DEFAULT '',
                source_schema      VARCHAR(255) NOT NULL DEFAULT '',
                source_table       VARCHAR(255) NOT NULL DEFAULT '',
                source_row_key     JSON NOT NULL,
                operation          VARCHAR(16) NOT NULL DEFAULT 'upsert',
                target_backend     VARCHAR(64) NOT NULL DEFAULT '',
                target_instance    VARCHAR(255) NOT NULL DEFAULT '',
                projection_kind    VARCHAR(64) NOT NULL DEFAULT '',
                resource_name      VARCHAR(255) NOT NULL DEFAULT '',
                target_options     JSON NOT NULL,
                source_payload     JSON NOT NULL,
                source_checksum    VARCHAR(255) NOT NULL DEFAULT '',
                status             VARCHAR(16) NOT NULL DEFAULT 'PENDING',
                retry_count        INT NOT NULL DEFAULT 0,
                last_error         TEXT NOT NULL,
                next_retry_at      TIMESTAMP(6) NULL,
                created_at         TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
                updated_at         TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
                completed_at       TIMESTAMP(6) NULL,
                CONSTRAINT chk_{TABLE}_op CHECK (operation IN ('upsert','delete')),
                CONSTRAINT chk_{TABLE}_status CHECK (status IN ('PENDING','IN_PROGRESS','COMPLETED','FAILED','DEAD_LETTER'))
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
            "#
        );
        // CREATE INDEX IF NOT EXISTS was added in MySQL 8.0.29.
        // For broader compatibility we attempt the create and
        // tolerate the "already exists" error. sqlx surfaces that as
        // a unique-key error; we match on substring.
        let create_idx_status = format!(
            "CREATE INDEX idx_{TABLE}_status_created_at \
             ON {TABLE} (status, created_at)"
        );
        let create_idx_project_status = format!(
            "CREATE INDEX idx_{TABLE}_project_status_created_at \
             ON {TABLE} (project_id, status, created_at)"
        );
        let create_idx_backend = format!(
            "CREATE INDEX idx_{TABLE}_backend_status \
             ON {TABLE} (target_backend, target_instance, status)"
        );
        let add_next_retry =
            format!("ALTER TABLE {TABLE} ADD COLUMN next_retry_at TIMESTAMP(6) NULL");
        let create_idx_retry = format!(
            "CREATE INDEX idx_{TABLE}_next_retry \
             ON {TABLE} (status, next_retry_at)"
        );
        sqlx::query(&create_table)
            .execute(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", create_table.clone(), e))?;
        if let Err(e) = sqlx::query(&add_next_retry)
            .execute(self.mysql_pool())
            .await
        {
            let msg = e.to_string();
            if !msg.contains("Duplicate column name") && !msg.contains("already exists") {
                return Err(SystemStoreError::query("mysql", add_next_retry, e));
            }
        }
        // Index creation is best-effort: on a fresh DB it succeeds;
        // on a re-run it errors as "Duplicate key name" which we
        // treat as success.
        for sql in [
            &create_idx_status,
            &create_idx_project_status,
            &create_idx_backend,
            &create_idx_retry,
        ] {
            if let Err(e) = sqlx::query(sql).execute(self.mysql_pool()).await {
                let msg = e.to_string();
                if !msg.contains("Duplicate key name") && !msg.contains("already exists") {
                    return Err(SystemStoreError::query("mysql", sql.clone(), e));
                }
            }
        }
        Ok(())
    }

    async fn enqueue_projection_task(
        &self,
        task: &ProjectionTaskInsert,
    ) -> SystemStoreResult<Uuid> {
        // We generate the UUID Rust-side. If the idempotency_key is
        // already present, ON DUPLICATE KEY UPDATE prevents an INSERT
        // collision — the existing row is unchanged and the SELECT
        // afterwards returns its task_id. If a fresh row is inserted,
        // the same SELECT returns the new task_id we just generated.
        let task_id = Uuid::new_v4();
        let task_id_str = task_id.to_string();
        let source_row_key = serde_json::to_string(&task.source_row_key)
            .map_err(|e| SystemStoreError::InvalidInput(format!("source_row_key: {e}")))?;
        let target_options = serde_json::to_string(&task.target_options)
            .map_err(|e| SystemStoreError::InvalidInput(format!("target_options: {e}")))?;
        let source_payload = serde_json::to_string(&task.source_payload)
            .map_err(|e| SystemStoreError::InvalidInput(format!("source_payload: {e}")))?;

        // `ON DUPLICATE KEY UPDATE idempotency_key = idempotency_key`
        // is the canonical MySQL no-op upsert idiom — it consumes the
        // duplicate-key error without changing the row.
        let insert_sql = format!(
            "INSERT INTO {TABLE} (
                task_id, idempotency_key, project_id, manifest_checksum, message_type,
                source_schema, source_table, source_row_key, operation,
                target_backend, target_instance, projection_kind, resource_name,
                target_options, source_payload, source_checksum, last_error
            ) VALUES (
                ?, ?, ?, ?, ?, ?, ?, CAST(? AS JSON), ?, ?, ?, ?, ?, CAST(? AS JSON), CAST(? AS JSON), ?, ''
            )
            ON DUPLICATE KEY UPDATE idempotency_key = idempotency_key"
        );
        sqlx::query(&insert_sql)
            .bind(&task_id_str)
            .bind(&task.idempotency_key)
            .bind(&task.project_id)
            .bind(&task.manifest_checksum)
            .bind(&task.message_type)
            .bind(&task.source_schema)
            .bind(&task.source_table)
            .bind(&source_row_key)
            .bind(task.operation.as_str())
            .bind(&task.target_backend)
            .bind(&task.target_instance)
            .bind(&task.projection_kind)
            .bind(&task.resource_name)
            .bind(&target_options)
            .bind(&source_payload)
            .bind(&task.source_checksum)
            .execute(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", insert_sql.clone(), e))?;

        // SELECT to get the task_id (ours if INSERT happened,
        // existing if ON DUPLICATE KEY fired).
        let lookup_sql = format!("SELECT task_id FROM {TABLE} WHERE idempotency_key = ?");
        let stored: (String,) = sqlx::query_as(&lookup_sql)
            .bind(&task.idempotency_key)
            .fetch_one(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", lookup_sql.clone(), e))?;
        Uuid::parse_str(&stored.0).map_err(|e| {
            SystemStoreError::InvalidInput(format!(
                "stored task_id '{}' is not a valid UUID: {e}",
                stored.0
            ))
        })
    }

    async fn claim_projection_tasks(
        &self,
        filter: &ProjectionClaimFilter,
    ) -> SystemStoreResult<Vec<ProjectionTaskRow>> {
        if filter.batch_size <= 0 {
            return Ok(Vec::new());
        }
        // MySQL workflow without RETURNING:
        // 1. BEGIN.
        // 2. SELECT candidate task_ids FOR UPDATE SKIP LOCKED.
        // 3. UPDATE the selected rows to IN_PROGRESS.
        // 4. SELECT the full rows by those IDs.
        // 5. COMMIT.
        let mut tx = self
            .mysql_pool()
            .begin()
            .await
            .map_err(|e| SystemStoreError::io("mysql", e))?;

        // Step 2: pick candidate IDs with SKIP LOCKED. Bind the
        // backend/instance filter parameters in a fixed shape.
        let (target_clause, n_extra_binds): (&str, usize) =
            match (&filter.target_backend, &filter.target_instance) {
                (Some(_), Some(_)) => ("AND target_backend = ? AND target_instance = ?", 2),
                (Some(_), None) => ("AND target_backend = ?", 1),
                (None, Some(_)) => ("AND target_instance = ?", 1),
                (None, None) => ("", 0),
            };
        let select_sql = format!(
            "SELECT task_id FROM {TABLE}
             WHERE status IN ('PENDING', 'FAILED')
               AND retry_count < ?
               AND (? = '' OR project_id = ?)
               AND (next_retry_at IS NULL OR next_retry_at <= NOW(6))
               {target_clause}
             ORDER BY created_at
             LIMIT ?
             FOR UPDATE SKIP LOCKED"
        );
        let project_id = filter.project_id.as_deref().unwrap_or("");
        let mut q = sqlx::query_scalar::<_, String>(&select_sql)
            .bind(filter.max_retries)
            .bind(project_id)
            .bind(project_id);
        match (&filter.target_backend, &filter.target_instance) {
            (Some(b), Some(i)) => {
                q = q.bind(b.clone()).bind(i.clone());
            }
            (Some(b), None) => {
                q = q.bind(b.clone());
            }
            (None, Some(i)) => {
                q = q.bind(i.clone());
            }
            (None, None) => {}
        }
        let _ = n_extra_binds; // documentation only
        let candidate_ids: Vec<String> = q
            .bind(filter.batch_size)
            .fetch_all(&mut *tx)
            .await
            .map_err(|e| SystemStoreError::query("mysql", select_sql.clone(), e))?;

        if candidate_ids.is_empty() {
            tx.commit()
                .await
                .map_err(|e| SystemStoreError::io("mysql", e))?;
            return Ok(Vec::new());
        }

        // Step 3: UPDATE those IDs to IN_PROGRESS. We build the
        // `IN (...)` placeholder list dynamically based on count.
        let placeholders: Vec<&str> = candidate_ids.iter().map(|_| "?").collect();
        let update_sql = format!(
            "UPDATE {TABLE}
             SET status = 'IN_PROGRESS', updated_at = NOW(6)
             WHERE task_id IN ({})",
            placeholders.join(",")
        );
        let mut upd = sqlx::query(&update_sql);
        for id in &candidate_ids {
            upd = upd.bind(id);
        }
        upd.execute(&mut *tx)
            .await
            .map_err(|e| SystemStoreError::query("mysql", update_sql.clone(), e))?;

        // Step 4: SELECT the updated rows.
        let select_full_sql = format!(
            "SELECT task_id, idempotency_key, project_id,
                    target_backend, target_instance, projection_kind, resource_name,
                    operation, source_row_key, target_options, source_payload,
                    source_checksum, status, retry_count, last_error,
                    created_at, updated_at, next_retry_at, completed_at
             FROM {TABLE}
             WHERE task_id IN ({})
             ORDER BY created_at",
            placeholders.join(",")
        );
        let mut sel = sqlx::query(&select_full_sql);
        for id in &candidate_ids {
            sel = sel.bind(id);
        }
        let rows = sel
            .fetch_all(&mut *tx)
            .await
            .map_err(|e| SystemStoreError::query("mysql", select_full_sql.clone(), e))?;

        tx.commit()
            .await
            .map_err(|e| SystemStoreError::io("mysql", e))?;

        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            out.push(row_to_projection_task(row)?);
        }
        Ok(out)
    }

    async fn mark_projection_task_completed(&self, task_id: Uuid) -> SystemStoreResult<()> {
        let sql = format!(
            "UPDATE {TABLE}
             SET status = 'COMPLETED', completed_at = NOW(6), next_retry_at = NULL, updated_at = NOW(6)
             WHERE task_id = ?"
        );
        sqlx::query(&sql)
            .bind(task_id.to_string())
            .execute(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        Ok(())
    }

    async fn mark_projection_task_failed(
        &self,
        task_id: Uuid,
        new_retry_count: i32,
        new_status: ProjectionTaskStatus,
        error: &str,
    ) -> SystemStoreResult<()> {
        if !matches!(
            new_status,
            ProjectionTaskStatus::Failed | ProjectionTaskStatus::DeadLetter
        ) {
            return Err(SystemStoreError::InvalidInput(format!(
                "mark_projection_task_failed only accepts FAILED or DEAD_LETTER, got {}",
                new_status.as_str()
            )));
        }
        let sql = format!(
            "UPDATE {TABLE}
             SET status = ?, retry_count = ?, last_error = ?,
                 next_retry_at = NULL,
                 updated_at = NOW(6)
             WHERE task_id = ?"
        );
        sqlx::query(&sql)
            .bind(new_status.as_str())
            .bind(new_retry_count)
            .bind(error)
            .bind(task_id.to_string())
            .execute(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        Ok(())
    }

    async fn requeue_dead_letter_tasks(
        &self,
        target_backend: Option<&str>,
    ) -> SystemStoreResult<i64> {
        let (where_clause, bind_backend) = match target_backend {
            Some(b) => ("AND target_backend = ?", Some(b.to_string())),
            None => ("", None),
        };
        let sql = format!(
            "UPDATE {TABLE}
             SET status = 'PENDING', retry_count = 0, last_error = '', next_retry_at = NULL, updated_at = NOW(6)
             WHERE status = 'DEAD_LETTER' {where_clause}"
        );
        let mut q = sqlx::query(&sql);
        if let Some(b) = bind_backend {
            q = q.bind(b);
        }
        let result = q
            .execute(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        Ok(result.rows_affected() as i64)
    }

    async fn reset_stale_in_progress_tasks(&self, stale_after: Duration) -> SystemStoreResult<i64> {
        // MySQL's TIMESTAMPDIFF is the canonical way to do this.
        // We bind the seconds parameter and let the server compare.
        let sql = format!(
            "UPDATE {TABLE}
             SET status = 'PENDING',
                 last_error = 'stale in-progress reconciliation',
                 updated_at = NOW(6)
             WHERE status = 'IN_PROGRESS'
               AND TIMESTAMPDIFF(SECOND, updated_at, NOW(6)) >= ?"
        );
        let result = sqlx::query(&sql)
            .bind(stale_after.as_secs() as i64)
            .execute(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        Ok(result.rows_affected() as i64)
    }

    async fn pending_task_metrics(&self, limit: i64) -> SystemStoreResult<Vec<PendingTaskMetric>> {
        // MySQL: TIMESTAMPDIFF(MICROSECOND, oldest, NOW(6)) gives
        // microseconds; divide by 1_000_000 for seconds-as-double.
        let sql = format!(
            "SELECT project_id, target_backend, target_instance, projection_kind,
                    COUNT(*) AS pending,
                    TIMESTAMPDIFF(MICROSECOND, MIN(created_at), NOW(6)) / 1000000.0
                      AS oldest_age_seconds
             FROM {TABLE}
             WHERE status IN ('PENDING', 'FAILED')
             GROUP BY project_id, target_backend, target_instance, projection_kind
             LIMIT ?"
        );
        let rows = sqlx::query(&sql)
            .bind(limit.max(1))
            .fetch_all(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            out.push(PendingTaskMetric {
                project_id: row.try_get("project_id").unwrap_or_default(),
                target_backend: row.try_get("target_backend").unwrap_or_default(),
                target_instance: row.try_get("target_instance").unwrap_or_default(),
                projection_kind: row.try_get("projection_kind").unwrap_or_default(),
                pending: row.try_get("pending").unwrap_or(0),
                oldest_age_seconds: row.try_get("oldest_age_seconds").unwrap_or(0.0),
            });
        }
        Ok(out)
    }

    async fn dead_letter_groups(&self, limit: i64) -> SystemStoreResult<Vec<DeadLetterGroup>> {
        let sql = format!(
            "SELECT source_table, target_backend, target_instance,
                    COUNT(*) AS dead_count
             FROM {TABLE}
             WHERE status = 'DEAD_LETTER'
             GROUP BY source_table, target_backend, target_instance
             LIMIT ?"
        );
        let rows = sqlx::query(&sql)
            .bind(limit.max(1))
            .fetch_all(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            out.push(DeadLetterGroup {
                source_table: row.try_get("source_table").unwrap_or_default(),
                target_backend: row.try_get("target_backend").unwrap_or_default(),
                target_instance: row.try_get("target_instance").unwrap_or_default(),
                dead_count: row.try_get("dead_count").unwrap_or(0),
            });
        }
        Ok(out)
    }

    async fn requeue_dead_letter_by_source(
        &self,
        source_table: &str,
        target_backend: &str,
        target_instance: &str,
    ) -> SystemStoreResult<i64> {
        let sql = format!(
            "UPDATE {TABLE}
             SET status = 'PENDING', retry_count = 0,
                 last_error = 'reconciliation repair', updated_at = NOW(6)
             WHERE status = 'DEAD_LETTER'
               AND source_table = ?
               AND target_backend = ?
               AND target_instance = ?"
        );
        let result = sqlx::query(&sql)
            .bind(source_table)
            .bind(target_backend)
            .bind(target_instance)
            .execute(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        Ok(result.rows_affected() as i64)
    }

    async fn pending_projection_task_count(
        &self,
        idempotency_keys: &[String],
    ) -> SystemStoreResult<i64> {
        if idempotency_keys.is_empty() {
            return Ok(0);
        }
        // MySQL: build the IN list with one ? per key.
        let placeholders: Vec<&str> = idempotency_keys.iter().map(|_| "?").collect();
        let sql = format!(
            "SELECT COUNT(*) FROM {TABLE}
             WHERE idempotency_key IN ({})
               AND status NOT IN ('COMPLETED','DEAD_LETTER','FAILED')",
            placeholders.join(",")
        );
        let mut q = sqlx::query_scalar::<_, i64>(&sql);
        for k in idempotency_keys {
            q = q.bind(k.clone());
        }
        let n: i64 = q
            .fetch_one(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        Ok(n)
    }

    async fn projection_task_summary(&self) -> SystemStoreResult<ProjectionTaskSummary> {
        let sql = format!("SELECT status, COUNT(*) AS n FROM {TABLE} GROUP BY status");
        let rows: Vec<(String, i64)> = sqlx::query_as(&sql)
            .fetch_all(self.mysql_pool())
            .await
            .map_err(|e| SystemStoreError::query("mysql", sql.clone(), e))?;
        let mut s = ProjectionTaskSummary::default();
        for (status, n) in rows {
            apply_projection_summary_bucket(&mut s, "mysql", &status, n)?;
        }
        Ok(s)
    }
}