udb 0.4.21

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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! The tenant restore IMPORT machinery for the native `BackupService`:
//! `restore_tenant` (shared movement gate, fresh-target guard, manifest load +
//! integrity verification, decrypt-at-rest, FK-ordered reinsert, cross-tenant
//! unique/foreign-key remapping, journal + outbox) plus the restore-remap model
//! and the Postgres unique-index probe it uses. Extracted verbatim from the
//! former god file — the SQL, crypto, and remap contracts are byte-for-byte
//! identical; `svc` replaces `&self`.

use std::collections::{HashMap, HashSet};

use sqlx::{PgPool, Row};
use tonic::{Request, Response, Status};
use uuid::Uuid;

use crate::generation::{CatalogManifest, ManifestColumn, ManifestTable};
use crate::proto::udb::core::backup::services::v1 as backup_pb;
use crate::runtime::channels::OperationChannel;
use crate::runtime::core::tenant_purge::plan_tenant_purge;
use crate::runtime::executor_utils::qi_runtime;
use crate::runtime::tenant_movement::{
    TenantMovementOperation, TenantMovementRequest, tenant_movement_policy_status,
    validate_tenant_movement_scope,
};

use super::super::native_helpers::{
    admit_on as native_admit_on, native_service_context, parse_uuid, validate_request_tenant,
};
use super::BackupServiceImpl;
use super::config::{KIND_RESTORE, MANIFEST_SUFFIX, TOPIC_BACKUP_RESTORED};
use super::errors::{
    backup_internal_status, backup_not_found_status, backup_run_missing_object_prefix_status,
    ensure_target_is_fresh,
};
use super::events::emit_event;
use super::model::{qualified_relation, run_summary_from_json, sha256_hex};
use super::store::{journal_run, run_read_by_id};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct RestoreColumnKey {
    schema: String,
    table: String,
    column: String,
}

type RestoreValueRemaps = HashMap<RestoreColumnKey, HashMap<String, String>>;

fn manifest_table_by_relation<'a>(
    manifest: &'a CatalogManifest,
    schema: &str,
    table: &str,
) -> Option<&'a ManifestTable> {
    manifest
        .tables
        .iter()
        .find(|candidate| candidate.schema == schema && candidate.table == table)
}

fn manifest_column<'a>(table: &'a ManifestTable, column: &str) -> Option<&'a ManifestColumn> {
    table
        .columns
        .iter()
        .find(|candidate| candidate.column_name == column)
}

fn unique_restore_columns(table: &ManifestTable, tenant_column: &str) -> Vec<String> {
    let mut columns = Vec::new();
    let mut seen = HashSet::new();
    let fk_columns: HashSet<&str> = table
        .foreign_keys
        .iter()
        .flat_map(|fk| fk.columns.iter().map(String::as_str))
        .collect();
    let mut add_column = |column: &str| {
        if column != tenant_column
            && !fk_columns.contains(column)
            && seen.insert(column.to_string())
        {
            columns.push(column.to_string());
        }
    };

    if !table
        .primary_key
        .iter()
        .any(|column| column == tenant_column)
    {
        for column in &table.primary_key {
            add_column(column);
        }
    }
    for column in &table.columns {
        if column.unique {
            add_column(&column.column_name);
        }
    }
    for index in &table.indexes {
        if index.unique && !index.columns.iter().any(|column| column == tenant_column) {
            for column in &index.columns {
                add_column(column);
            }
        }
    }

    columns
}

fn restored_unique_value(
    column: Option<&ManifestColumn>,
    target_tenant_id: &str,
    old_value: &str,
) -> String {
    let sql_type = column
        .map(|column| column.sql_type.as_str())
        .unwrap_or_default()
        .to_ascii_lowercase();
    if sql_type.contains("uuid") && Uuid::parse_str(old_value).is_ok() {
        return Uuid::new_v4().to_string();
    }

    let target = target_tenant_id.replace('-', "");
    let nonce = Uuid::new_v4().simple().to_string();
    let mut value = format!(
        "restored-{}-{}",
        &target[..12.min(target.len())],
        &nonce[..16]
    );
    if let Some(limit) = varchar_limit(&sql_type)
        && value.len() > limit
    {
        value.truncate(limit);
    }
    value
}

fn can_remap_unique_value(column: Option<&ManifestColumn>) -> bool {
    let sql_type = column
        .map(|column| column.sql_type.as_str())
        .unwrap_or_default()
        .to_ascii_lowercase();
    sql_type.contains("uuid")
        || sql_type.contains("char")
        || sql_type.contains("text")
        || sql_type.is_empty()
}

fn varchar_limit(sql_type: &str) -> Option<usize> {
    let open = sql_type.find('(')?;
    let close = sql_type[open + 1..].find(')')? + open + 1;
    sql_type[open + 1..close].trim().parse().ok()
}

fn apply_parent_restore_remaps(
    row: &mut serde_json::Map<String, serde_json::Value>,
    table: &ManifestTable,
    remaps: &RestoreValueRemaps,
) {
    for fk in &table.foreign_keys {
        for (column, ref_column) in fk.columns.iter().zip(fk.ref_columns.iter()) {
            let Some(serde_json::Value::String(old_value)) = row.get(column) else {
                continue;
            };
            let key = RestoreColumnKey {
                schema: fk.ref_schema.clone(),
                table: fk.ref_table.clone(),
                column: ref_column.clone(),
            };
            let Some(new_value) = remaps.get(&key).and_then(|values| values.get(old_value)) else {
                continue;
            };
            row.insert(column.clone(), serde_json::Value::String(new_value.clone()));
        }
    }
}

fn apply_cross_tenant_restore_remaps(
    row: &mut serde_json::Map<String, serde_json::Value>,
    table: &ManifestTable,
    tenant_column: &str,
    target_tenant_id: &str,
    remaps: &mut RestoreValueRemaps,
    extra_unique_columns: &[String],
) {
    let mut columns = unique_restore_columns(table, tenant_column);
    let mut seen: HashSet<String> = columns.iter().cloned().collect();
    for column in extra_unique_columns {
        if seen.insert(column.clone()) {
            columns.push(column.clone());
        }
    }
    for column in columns {
        let Some(serde_json::Value::String(old_value)) = row.get(&column) else {
            continue;
        };
        if old_value.is_empty() {
            continue;
        }
        let column_meta = manifest_column(table, &column);
        if !can_remap_unique_value(column_meta) {
            continue;
        }
        let key = RestoreColumnKey {
            schema: table.schema.clone(),
            table: table.table.clone(),
            column: column.clone(),
        };
        let values = remaps.entry(key).or_default();
        let new_value = values
            .entry(old_value.clone())
            .or_insert_with(|| restored_unique_value(column_meta, target_tenant_id, old_value))
            .clone();
        row.insert(column, serde_json::Value::String(new_value));
    }
}

async fn postgres_unique_restore_columns(
    pool: &PgPool,
    schema: &str,
    table: &str,
    tenant_column: &str,
    table_meta: &ManifestTable,
) -> Result<Vec<String>, Status> {
    let fk_columns: HashSet<&str> = table_meta
        .foreign_keys
        .iter()
        .flat_map(|fk| fk.columns.iter().map(String::as_str))
        .collect();
    let sql = r#"
        SELECT cols
        FROM (
          SELECT i.indexrelid, array_agg(a.attname::text ORDER BY k.ordinality) AS cols
          FROM pg_catalog.pg_index i
          JOIN pg_catalog.pg_class c ON c.oid = i.indrelid
          JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
          JOIN unnest(i.indkey) WITH ORDINALITY AS k(attnum, ordinality) ON true
          JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid AND a.attnum = k.attnum
          WHERE i.indisunique
            AND n.nspname = $1
            AND c.relname = $2
            AND k.attnum > 0
          GROUP BY i.indexrelid
        ) unique_indexes
        WHERE NOT ($3 = ANY(cols))
    "#;
    let rows = sqlx::query(sql)
        .bind(schema)
        .bind(table)
        .bind(tenant_column)
        .fetch_all(pool)
        .await
        .map_err(|err| {
            backup_internal_status(
                "restore_unique_index_probe",
                format!("restore unique-index probe failed for {schema}.{table}: {err}"),
            )
        })?;
    let mut columns = Vec::new();
    let mut seen = HashSet::new();
    for row in rows {
        let cols: Vec<String> = row.try_get("cols").map_err(|err| {
            backup_internal_status(
                "restore_unique_index_probe",
                format!("restore unique-index row decode failed for {schema}.{table}: {err}"),
            )
        })?;
        for column in cols {
            if column != tenant_column
                && !fk_columns.contains(column.as_str())
                && seen.insert(column.clone())
            {
                columns.push(column);
            }
        }
    }
    Ok(columns)
}

pub(crate) async fn restore_tenant(
    svc: &BackupServiceImpl,
    request: Request<backup_pb::RestoreTenantRequest>,
) -> Result<Response<backup_pb::RestoreTenantResponse>, Status> {
    let metadata = request.metadata().clone();
    let req = request.into_inner();
    let source_tenant_id = req.source_tenant_id.trim().to_string();
    let target_tenant_id = req.target_tenant_id.trim().to_string();
    let backup_id = req.backup_id.trim().to_string();
    if source_tenant_id.is_empty() || target_tenant_id.is_empty() || backup_id.is_empty() {
        return Err(crate::runtime::executor_utils::invalid_argument_fields(
            "source_tenant_id, target_tenant_id and backup_id are required",
            [
                ("source_tenant_id", "must be a non-empty source tenant id"),
                ("target_tenant_id", "must be a non-empty target tenant id"),
                ("backup_id", "must be a non-empty backup id"),
            ],
        ));
    }
    // Same-tenant restore acts on the target tenant. Cross-tenant restore
    // into a fresh target has no target principal yet, so authorize the
    // source tenant and require the explicit movement gate below.
    let auth_tenant_id = if req.allow_cross_tenant {
        &source_tenant_id
    } else {
        &target_tenant_id
    };
    validate_request_tenant(&metadata, auth_tenant_id)?;
    // DESTRUCTIVE: a missing confirmation token fails CLOSED.
    if req.confirmation_token.trim().is_empty() {
        return Err(crate::runtime::executor_utils::invalid_argument_fields(
            "RestoreTenant overwrites a tenant's data; confirmation_token is required",
            [(
                "confirmation_token",
                "must be present to restore over tenant data",
            )],
        ));
    }

    // SHARED fail-closed movement validator — RestoreImport. A target that
    // differs from the source is a cross-tenant move and requires explicit
    // privileged approval; otherwise this returns an error and we fail closed.
    let movement = TenantMovementRequest {
        operation: TenantMovementOperation::RestoreImport,
        tenant_id: &source_tenant_id,
        target_tenant_id: Some(&target_tenant_id),
        tenant_filter_present: true,
        privileged_cross_tenant: req.allow_cross_tenant,
    };
    validate_tenant_movement_scope(&movement)
        .map_err(|err| tenant_movement_policy_status(movement.operation, err))?;

    let runtime = svc.require_runtime()?;
    let pool = svc.require_pool()?;
    let manifest = svc.require_manifest()?;
    let _ = parse_uuid("source_tenant_id", &source_tenant_id)?;
    let _ = parse_uuid("target_tenant_id", &target_tenant_id)?;
    let context = native_service_context(&metadata, &target_tenant_id, "");

    // Resolve the source run's object prefix from the durable journal.
    let source_ctx = native_service_context(&metadata, &source_tenant_id, "");
    let run = runtime
        .native_entity_read_for_service(
            "backup",
            &source_ctx,
            run_read_by_id(&source_tenant_id, &backup_id),
        )
        .await?
        .first()
        .map(run_summary_from_json)
        .ok_or_else(|| {
            backup_not_found_status(
                "restore_tenant",
                "backup_run_not_found",
                "backup run not found for source tenant",
            )
        })?;
    let object_prefix = run.object_prefix.trim().to_string();
    if object_prefix.is_empty() {
        return Err(backup_run_missing_object_prefix_status());
    }

    // Enumerate the tenant-owned tables via the SHARED planner.
    let plan = plan_tenant_purge(manifest);

    // FRESH-target guard: refuse to write over a live tenant. Probe each
    // tenant-owned table for ANY existing row under the target tenant.
    let mut existing_rows: u64 = 0;
    for target in &plan.targets {
        let rel = qualified_relation(&target.schema, &target.table);
        let probe_sql = format!(
            "SELECT 1 FROM {rel} WHERE {col}::text = $1 LIMIT 1",
            col = qi_runtime(&target.tenant_column),
        );
        let present: Option<i32> = sqlx::query_scalar(&probe_sql)
            .bind(&target_tenant_id)
            .fetch_optional(pool)
            .await
            .map_err(|err| {
                backup_internal_status(
                    "restore_freshness_probe",
                    format!(
                        "restore freshness probe failed on {}.{}: {err}",
                        target.schema, target.table
                    ),
                )
            })?;
        if present.is_some() {
            existing_rows += 1;
        }
    }
    ensure_target_is_fresh(existing_rows)?;

    let _admit = native_admit_on(
        svc.channels.as_ref(),
        &svc.metrics,
        "backup",
        OperationChannel::Admin,
        &target_tenant_id,
        None,
    )
    .await?;

    // Load + verify the run manifest (object backend/bucket are recorded in
    // it). The manifest itself lives under the service default object target
    // (where the backup wrote it); the per-table artifacts then use whatever
    // backend/bucket the manifest records.
    let manifest_key = format!("{object_prefix}{MANIFEST_SUFFIX}");
    let run_backend = svc.object_backend.clone();
    let run_bucket = svc.object_bucket.clone();
    let manifest_get = crate::runtime::core::setup_data::object_request_json(
        "get",
        &run_bucket,
        &manifest_key,
        "",
    );
    let manifest_bytes = runtime
        .get_object_backend_target_for_project(
            &run_backend,
            None,
            &context.project_id,
            &manifest_get,
        )
        .await?;
    let manifest_value: serde_json::Value =
        serde_json::from_slice(&manifest_bytes).map_err(|err| {
            backup_internal_status(
                "restore_manifest_parse",
                format!("restore manifest parse failed: {err}"),
            )
        })?;
    let object_backend = manifest_value
        .get("object_backend")
        .and_then(|v| v.as_str())
        .filter(|v| !v.trim().is_empty())
        .map(str::to_string)
        .unwrap_or(run_backend);
    let object_bucket = manifest_value
        .get("object_bucket")
        .and_then(|v| v.as_str())
        .filter(|v| !v.trim().is_empty())
        .map(str::to_string)
        .unwrap_or(run_bucket);
    let manifest_tables = manifest_value
        .get("tables")
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default();

    // Restore in REVERSE planner order: the planner emits children→parents
    // (safe for delete); inserting parents→children satisfies FK constraints.
    let mut ordered_tables = manifest_tables;
    ordered_tables.reverse();

    let mut restored_rows: i64 = 0;
    let mut restored_table_count: i32 = 0;
    let cross_tenant_restore = source_tenant_id != target_tenant_id;
    let mut restore_remaps: RestoreValueRemaps = HashMap::new();
    let mut tx = pool.begin().await.map_err(|err| {
        backup_internal_status(
            "restore_transaction_begin",
            format!("failed to begin restore transaction: {err}"),
        )
    })?;
    for entry in &ordered_tables {
        let schema = entry.get("schema").and_then(|v| v.as_str()).unwrap_or("");
        let table = entry.get("table").and_then(|v| v.as_str()).unwrap_or("");
        let tenant_column = entry
            .get("tenant_column")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let object_key = entry
            .get("object_key")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let expected_checksum = entry
            .get("checksum_sha256")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        if schema.is_empty() || table.is_empty() || object_key.is_empty() {
            continue;
        }
        let manifest_table = manifest_table_by_relation(manifest, schema, table);
        let db_unique_restore_columns = if cross_tenant_restore {
            match manifest_table {
                Some(table_meta) => {
                    postgres_unique_restore_columns(pool, schema, table, tenant_column, table_meta)
                        .await?
                }
                None => Vec::new(),
            }
        } else {
            Vec::new()
        };
        let get_req = crate::runtime::core::setup_data::object_request_json(
            "get",
            &object_bucket,
            object_key,
            "",
        );
        let bytes = runtime
            .get_object_backend_target_for_project(
                &object_backend,
                None,
                &context.project_id,
                &get_req,
            )
            .await?;
        // Integrity: the encrypted artifact must match the manifest checksum.
        if !expected_checksum.is_empty() && sha256_hex(&bytes) != expected_checksum {
            return Err(Status::data_loss(format!(
                "restore integrity check failed for {schema}.{table} (checksum mismatch)"
            )));
        }
        let ciphertext = String::from_utf8(bytes).map_err(|err| {
            backup_internal_status(
                "restore_artifact_utf8",
                format!("restore artifact is not valid UTF-8: {err}"),
            )
        })?;
        let jsonl = runtime.decrypt_secret_at_rest(&ciphertext).map_err(|err| {
            backup_internal_status(
                "restore_decrypt_artifact",
                format!("restore decryption failed: {err}"),
            )
        })?;
        let rel = qualified_relation(schema, table);
        // `jsonb_populate_record` casts the row JSON into the table's row type,
        // so every column type is handled by Postgres without hand-mapping.
        let insert_sql =
            format!("INSERT INTO {rel} SELECT (jsonb_populate_record(NULL::{rel}, $1::jsonb)).*");
        for line in jsonl.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            let mut value: serde_json::Value = serde_json::from_str(line).map_err(|err| {
                backup_internal_status(
                    "restore_row_parse",
                    format!("restore row parse failed for {schema}.{table}: {err}"),
                )
            })?;
            // Rewrite the tenant column to the target on insert.
            if let Some(obj) = value.as_object_mut()
                && !tenant_column.is_empty()
            {
                if cross_tenant_restore && let Some(table_meta) = manifest_table {
                    apply_parent_restore_remaps(obj, table_meta, &restore_remaps);
                }
                obj.insert(
                    tenant_column.to_string(),
                    serde_json::Value::String(target_tenant_id.clone()),
                );
                if cross_tenant_restore && let Some(table_meta) = manifest_table {
                    apply_cross_tenant_restore_remaps(
                        obj,
                        table_meta,
                        tenant_column,
                        &target_tenant_id,
                        &mut restore_remaps,
                        &db_unique_restore_columns,
                    );
                }
            }
            // Bind the row as text and cast to jsonb in SQL ($1::jsonb), so the
            // bind never depends on the sqlx json feature being enabled.
            let row_json = serde_json::to_string(&value).map_err(|err| {
                backup_internal_status(
                    "restore_row_reserialize",
                    format!("restore row reserialize failed: {err}"),
                )
            })?;
            sqlx::query(&insert_sql)
                .bind(row_json)
                .execute(&mut *tx)
                .await
                .map_err(|err| {
                    backup_internal_status(
                        "restore_insert_row",
                        format!("restore insert failed for {schema}.{table}: {err}"),
                    )
                })?;
            restored_rows += 1;
        }
        restored_table_count += 1;
    }
    tx.commit().await.map_err(|err| {
        backup_internal_status(
            "restore_transaction_commit",
            format!("failed to commit restore transaction: {err}"),
        )
    })?;

    let restore_id = Uuid::new_v4().to_string();
    journal_run(
        runtime,
        &context,
        &restore_id,
        &target_tenant_id,
        KIND_RESTORE,
        &object_prefix,
        &run.manifest_checksum,
        restored_table_count as i64,
        restored_rows,
        0,
        &source_tenant_id,
        &target_tenant_id,
    )
    .await?;

    emit_event(
        svc,
        TOPIC_BACKUP_RESTORED,
        &target_tenant_id,
        &target_tenant_id,
        &context.project_id,
        &restore_id,
        serde_json::json!({
            "backup_id": restore_id,
            "source_backup_id": backup_id,
            "source_tenant_id": source_tenant_id,
            "target_tenant_id": target_tenant_id,
            "object_prefix": object_prefix,
            "restored_table_count": restored_table_count,
            "restored_rows": restored_rows,
        }),
    )
    .await;

    Ok(Response::new(backup_pb::RestoreTenantResponse {
        backup_id: restore_id,
        source_object_prefix: object_prefix,
        restored_table_count,
        restored_rows,
        message: "tenant restored".to_string(),
        error: None,
    }))
}