udb 0.4.17

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
//! Runtime schema registry for data operations (U13).
//!
//! Pre-U13 the catalog had **two** schema-aware surfaces:
//!
//! - **CDC schema registry** (`runtime::cdc::SchemaRegistryMode`): registers
//!   event payload shapes with an external HTTP service for downstream
//!   consumers. Cares about event publishing, not RPC requests.
//! - **`CatalogManager::is_compatible`**: a yes/no compatibility check used
//!   only on the catalog activation / migration path.
//!
//! Neither covers what every gRPC RPC needs to know on the hot path:
//!
//! > For this `project_id` + `client_catalog_version`, am I allowed to
//! > read/write `Customer`, and what fields does the broker expect today?
//!
//! `SchemaRegistry` is that runtime surface. It:
//!
//! - Negotiates the caller's `client_catalog_version` against the
//!   active project catalog using the existing
//!   [`CatalogManager::compatibility_error`] ladder.
//! - Surfaces a per-message [`MessageDescriptor`] — field names, SQL types,
//!   primary key, manifest checksum — so SDKs can validate payloads at
//!   bind time and cache the descriptor per `(project, version, message)`.
//! - Returns typed [`NegotiationOutcome`] verdicts so handlers map
//!   compat failures to the right gRPC `tonic::Code` instead of mixing
//!   "not found" vs "incompatible" into one error.
//!
//! The U13 acceptance gate ("Old client can continue under
//! backward-compatible catalog changes and fails clearly under breaking
//! changes") is verified by the tests in this file: they pin the
//! compatibility ladder so a future regression doesn't silently widen
//! the compat envelope.

use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::generation::manifest::ManifestColumn;
use crate::runtime::catalog::{CatalogManager, CatalogState};

/// What the broker says about a message type a client is asking about.
/// SDKs cache by `(project, version, message_type)` to avoid round-tripping
/// on every RPC.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageDescriptor {
    /// Canonical proto message name (`acme.billing.v1.Customer`).
    pub message_type: String,
    /// Project this descriptor was resolved against.
    pub project_id: String,
    /// Catalog version at lookup time. Pair with `manifest_checksum` to
    /// detect drift between the cache and the live broker.
    pub catalog_version: String,
    /// SHA-256 of the catalog manifest. Changes on every schema migration
    /// — clients use it as a cache key.
    pub manifest_checksum: String,
    /// Physical SQL location (used by some clients for direct queries; the
    /// broker still mediates all access, but it's useful for tooling).
    pub schema: String,
    pub table: String,
    pub primary_key: Vec<String>,
    pub fields: Vec<FieldDescriptor>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FieldDescriptor {
    pub name: String,
    pub column_name: String,
    pub proto_type: String,
    pub sql_type: String,
    pub not_null: bool,
    pub is_primary: bool,
    pub is_array: bool,
}

impl From<&ManifestColumn> for FieldDescriptor {
    fn from(col: &ManifestColumn) -> Self {
        Self {
            name: col.field_name.clone(),
            column_name: col.column_name.clone(),
            proto_type: col.proto_type.clone(),
            sql_type: col.sql_type.clone(),
            not_null: col.not_null,
            is_primary: col.is_primary,
            is_array: col.is_array,
        }
    }
}

/// Typed outcome of `negotiate_version`. The handler maps these to
/// `tonic::Code` directly — no string parsing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum NegotiationOutcome {
    /// Client and broker agree (exact version match, or client passed no
    /// version so the broker's active version is implied).
    Match {
        active_version: String,
        manifest_checksum: String,
    },
    /// The client's version is older but the broker's compat policy says
    /// it's still allowed (`backward` mode, same major). The client may
    /// continue; the response should carry an upgrade hint.
    BackwardCompatible {
        active_version: String,
        client_version: String,
        manifest_checksum: String,
    },
    /// **Breaking** — the client's version doesn't satisfy the active
    /// catalog's compatibility policy. The handler should map this to
    /// `tonic::Code::FailedPrecondition` with a clear remediation string.
    Incompatible {
        active_version: String,
        client_version: String,
        reason: String,
    },
}

impl NegotiationOutcome {
    /// True when the client may proceed (Match or BackwardCompatible).
    pub fn may_proceed(&self) -> bool {
        !matches!(self, Self::Incompatible { .. })
    }
}

/// Errors `SchemaRegistry::lookup_message` returns. Distinct from
/// `NegotiationOutcome` because not-found and incompatible are different
/// failure shapes (one's `NotFound`, the other's `FailedPrecondition`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum LookupError {
    /// The active manifest doesn't declare this message type.
    MessageNotFound {
        project_id: String,
        message_type: String,
    },
    /// A convenience name matched more than one composed-catalog entity.
    MessageAmbiguous {
        project_id: String,
        message_type: String,
        candidates: Vec<String>,
    },
    /// The client's version isn't compatible with the active catalog.
    Incompatible {
        project_id: String,
        message_type: String,
        client_version: String,
        active_version: String,
        reason: String,
    },
}

/// The runtime-side wrapper around `CatalogManager` that handles
/// per-RPC schema queries.
#[derive(Debug, Clone)]
pub struct SchemaRegistry {
    catalog: Arc<CatalogManager>,
}

impl SchemaRegistry {
    pub fn new(catalog: Arc<CatalogManager>) -> Self {
        Self { catalog }
    }

    /// Negotiate the client's catalog version against the project's
    /// active catalog. Empty `client_version` is allowed (lenient mode)
    /// and resolves to `Match` against whatever the active catalog is.
    pub fn negotiate_version(&self, project_id: &str, client_version: &str) -> NegotiationOutcome {
        let active: Arc<CatalogState> = self.catalog.active_for(project_id);
        let active_version = active.metadata.version.clone();
        let manifest_checksum = active.metadata.checksum.clone();

        if client_version.trim().is_empty() {
            return NegotiationOutcome::Match {
                active_version,
                manifest_checksum,
            };
        }
        match self.catalog.compatibility_error(client_version, project_id) {
            None if active_version == client_version => NegotiationOutcome::Match {
                active_version,
                manifest_checksum,
            },
            None => NegotiationOutcome::BackwardCompatible {
                active_version,
                client_version: client_version.to_string(),
                manifest_checksum,
            },
            Some(reason) => NegotiationOutcome::Incompatible {
                active_version,
                client_version: client_version.to_string(),
                reason,
            },
        }
    }

    /// Look up a message descriptor for `(project_id, message_type)`
    /// honouring the client's compatibility envelope. SDKs cache the
    /// returned descriptor by `(project, version, message_type)`.
    pub fn lookup_message(
        &self,
        project_id: &str,
        message_type: &str,
        client_version: &str,
    ) -> Result<MessageDescriptor, LookupError> {
        // Compatibility check first — refuse to expose a descriptor a
        // client can't legally bind against.
        let outcome = self.negotiate_version(project_id, client_version);
        if let NegotiationOutcome::Incompatible {
            active_version,
            client_version,
            reason,
        } = &outcome
        {
            return Err(LookupError::Incompatible {
                project_id: project_id.to_string(),
                message_type: message_type.to_string(),
                client_version: client_version.clone(),
                active_version: active_version.clone(),
                reason: reason.clone(),
            });
        }

        let active = self.catalog.active_for(project_id);
        let table = match crate::broker::resolve_table_for_message(&active.manifest, message_type) {
            Ok(table) => table,
            Err(crate::broker::TableLookupError::Missing { .. }) => {
                return Err(LookupError::MessageNotFound {
                    project_id: project_id.to_string(),
                    message_type: message_type.to_string(),
                });
            }
            Err(crate::broker::TableLookupError::Ambiguous { candidates, .. }) => {
                return Err(LookupError::MessageAmbiguous {
                    project_id: project_id.to_string(),
                    message_type: message_type.to_string(),
                    candidates,
                });
            }
        };

        Ok(MessageDescriptor {
            message_type: table.message_name.clone(),
            project_id: project_id.to_string(),
            catalog_version: active.metadata.version.clone(),
            manifest_checksum: active.metadata.checksum.clone(),
            schema: table.schema.clone(),
            table: table.table.clone(),
            primary_key: table.primary_key.clone(),
            fields: table.columns.iter().map(FieldDescriptor::from).collect(),
        })
    }

    /// Enumerate every message type the active catalog declares for
    /// `project_id` — used by the `ListMessageSchemas` admin RPC and the
    /// portal's schema browser.
    pub fn list_messages(&self, project_id: &str) -> Vec<String> {
        let active = self.catalog.active_for(project_id);
        active
            .manifest
            .tables
            .iter()
            .map(|t| t.message_name.clone())
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generation::manifest::{CatalogManifest, ManifestColumn, ManifestTable};

    fn manifest_with_customer(checksum: &str) -> CatalogManifest {
        CatalogManifest {
            checksum_sha256: checksum.to_string(),
            tables: vec![ManifestTable {
                checksum_sha256: checksum.to_string(),
                message_name: "acme.billing.v1.Customer".to_string(),
                schema: "public".to_string(),
                table: "customers".to_string(),
                primary_key: vec!["id".to_string()],
                columns: vec![
                    ManifestColumn {
                        field_name: "id".into(),
                        column_name: "id".into(),
                        proto_type: "string".into(),
                        sql_type: "uuid".into(),
                        is_primary: true,
                        not_null: true,
                        ..Default::default()
                    },
                    ManifestColumn {
                        field_name: "email".into(),
                        column_name: "email".into(),
                        proto_type: "string".into(),
                        sql_type: "text".into(),
                        ..Default::default()
                    },
                ],
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    async fn registry_with_project(version: &str, compat: &str) -> SchemaRegistry {
        let catalog = Arc::new(CatalogManager::new(manifest_with_customer("default")));
        catalog
            .stage_catalog(
                manifest_with_customer("billing-v1"),
                "billing".into(),
                version.into(),
                compat.into(),
            )
            .await
            .unwrap();
        catalog
            .activate_catalog_for("billing", version)
            .await
            .unwrap();
        SchemaRegistry::new(catalog)
    }

    #[tokio::test]
    async fn empty_client_version_matches_active() {
        let registry = registry_with_project("2.0.0", "backward").await;
        let outcome = registry.negotiate_version("billing", "");
        match outcome {
            NegotiationOutcome::Match { active_version, .. } => {
                assert_eq!(active_version, "2.0.0");
            }
            other => panic!("expected Match, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn exact_version_match() {
        let registry = registry_with_project("2.0.0", "backward").await;
        let outcome = registry.negotiate_version("billing", "2.0.0");
        assert!(matches!(outcome, NegotiationOutcome::Match { .. }));
    }

    /// **Acceptance gate**: "Old client can continue under backward-
    /// compatible catalog changes." A 1.x client with a backward-compat
    /// 2.x active catalog gets `BackwardCompatible`, not `Incompatible`.
    #[tokio::test]
    async fn older_client_passes_under_backward_compat() {
        // Active: 2.5.0 in "backward" mode. Client: 2.1.0 (same major,
        // older minor). Must be BackwardCompatible.
        let registry = registry_with_project("2.5.0", "backward").await;
        let outcome = registry.negotiate_version("billing", "2.1.0");
        assert!(
            matches!(outcome, NegotiationOutcome::BackwardCompatible { .. }),
            "got {outcome:?}"
        );
        assert!(outcome.may_proceed());
    }

    /// **Acceptance gate**: "Fails clearly under breaking changes." A
    /// cross-major-version client gets `Incompatible` with a typed
    /// reason the handler maps to `FailedPrecondition`.
    #[tokio::test]
    async fn breaking_change_returns_incompatible_with_reason() {
        // Active: 2.5.0, client: 3.0.0 (new major — broker can't serve
        // unreleased client expectations).
        let registry = registry_with_project("2.5.0", "backward").await;
        let outcome = registry.negotiate_version("billing", "3.0.0");
        match outcome {
            NegotiationOutcome::Incompatible {
                client_version,
                reason,
                ..
            } => {
                assert_eq!(client_version, "3.0.0");
                assert!(reason.contains("backward-compatible") || reason.contains("compat"));
            }
            other => panic!("expected Incompatible, got {other:?}"),
        }
    }

    /// `exact` compat mode is stricter than `backward` — even minor-
    /// version drift fails. SDKs that need byte-exact behaviour can
    /// request this mode at catalog stage time.
    #[tokio::test]
    async fn exact_mode_rejects_any_drift() {
        let registry = registry_with_project("2.5.0", "exact").await;
        let outcome = registry.negotiate_version("billing", "2.5.1");
        assert!(matches!(outcome, NegotiationOutcome::Incompatible { .. }));
    }

    #[tokio::test]
    async fn lookup_returns_descriptor_with_fields() {
        let registry = registry_with_project("1.0.0", "backward").await;
        let descriptor = registry
            .lookup_message("billing", "acme.billing.v1.Customer", "1.0.0")
            .expect("descriptor");
        assert_eq!(descriptor.message_type, "acme.billing.v1.Customer");
        assert_eq!(descriptor.project_id, "billing");
        assert_eq!(descriptor.schema, "public");
        assert_eq!(descriptor.table, "customers");
        assert_eq!(descriptor.primary_key, vec!["id"]);
        assert_eq!(descriptor.fields.len(), 2);
        let pk = descriptor
            .fields
            .iter()
            .find(|f| f.is_primary)
            .expect("pk field");
        assert_eq!(pk.name, "id");
    }

    #[tokio::test]
    async fn lookup_unknown_message_returns_message_not_found() {
        let registry = registry_with_project("1.0.0", "backward").await;
        let err = registry
            .lookup_message("billing", "acme.billing.v1.UnknownThing", "1.0.0")
            .unwrap_err();
        assert!(matches!(err, LookupError::MessageNotFound { .. }));
    }

    #[tokio::test]
    async fn lookup_ambiguous_short_name_returns_candidates_and_exact_fqns_route() {
        let mut manifest = manifest_with_customer("ambiguous-default");
        let customer_columns = manifest.tables[0].columns.clone();
        manifest.tables.push(ManifestTable {
            message_name: "udb.core.billing.v1.Customer".to_string(),
            schema: "udb_billing".to_string(),
            table: "customers".to_string(),
            primary_key: vec!["id".to_string()],
            columns: customer_columns,
            ..Default::default()
        });
        let registry = SchemaRegistry::new(Arc::new(CatalogManager::new(manifest)));

        let error = registry
            .lookup_message("default", "Customer", "")
            .expect_err("colliding short names must not route first-wins");
        let LookupError::MessageAmbiguous { candidates, .. } = error else {
            panic!("short-name collision must remain typed as ambiguous");
        };
        assert_eq!(candidates.len(), 2);
        assert!(
            candidates
                .iter()
                .any(|candidate| candidate.contains("acme.billing.v1.Customer"))
        );
        assert!(
            candidates
                .iter()
                .any(|candidate| candidate.contains("udb.core.billing.v1.Customer"))
        );

        let consumer = registry
            .lookup_message("default", "acme.billing.v1.Customer", "")
            .expect("consumer FQN routes exactly");
        assert_eq!(consumer.schema, "public");
        let embedded = registry
            .lookup_message("default", "udb.core.billing.v1.Customer", "")
            .expect("embedded FQN routes exactly");
        assert_eq!(embedded.schema, "udb_billing");
    }

    #[tokio::test]
    async fn lookup_with_incompatible_client_returns_typed_error() {
        let registry = registry_with_project("2.0.0", "exact").await;
        let err = registry
            .lookup_message("billing", "acme.billing.v1.Customer", "2.5.0")
            .unwrap_err();
        assert!(matches!(err, LookupError::Incompatible { .. }));
    }

    #[tokio::test]
    async fn list_messages_enumerates_active_catalog() {
        let registry = registry_with_project("1.0.0", "backward").await;
        let messages = registry.list_messages("billing");
        assert_eq!(messages, vec!["acme.billing.v1.Customer".to_string()]);
    }

    /// Unknown project_id falls back to the default catalog (U4's
    /// back-compat shim). Schema registry inherits the same behaviour
    /// for free — a missing `x-udb-project-id` header doesn't poison
    /// lookups for callers running against the default project.
    #[tokio::test]
    async fn unknown_project_falls_back_to_default() {
        let registry = registry_with_project("1.0.0", "backward").await;
        // Lookup against unconfigured project — sees default's catalog,
        // which has the same Customer table (we seeded both projects
        // with the same fixture).
        let descriptor = registry
            .lookup_message("nonexistent-project", "acme.billing.v1.Customer", "")
            .expect("falls back to default");
        assert_eq!(descriptor.project_id, "nonexistent-project");
    }
}