udb 0.4.27

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
//! `SqliteCanonicalStore` — new in P2P. sqlx-sqlite backed.
//!
//! ## Why SQLite belongs as a peer
//!
//! SQLite is one of the most-deployed databases on earth (every
//! browser, every Android app, every iOS app, every desktop app
//! ships it). For UDB's "any project, any DB" promise to be honest,
//! SQLite has to be a first-class canonical store, not a curiosity.
//!
//! It also doubles as the substrate for the long-pending U27
//! follow-up: an in-memory test profile. A `SqliteCanonicalStore`
//! built against `sqlite::memory:` gives every test a real canonical
//! store with no Docker, no `pg_isready`, no `TEST_POSTGRES_DSN`.
//!
//! ## Durability token strategy
//!
//! SQLite has no LSN, no GTID. The closest single-writer monotone
//! signal is `PRAGMA data_version`, which the SQLite docs describe as
//! *"a number that increases every time the database file is
//! modified by another connection or by a checkpoint."* It's stable
//! within one connection's lifetime, which matches how the runtime
//! uses the canonical store (one pool per store).
//!
//! For the `wait_for_token` semantics, SQLite is **trivially
//! up-to-date with itself** — there's no replica lag. So
//! `wait_for_token` returns `Ok(true)` immediately whenever the
//! requested data_version is `<=` the current one, and polls otherwise
//! (covers the case where a writer commits between the read of
//! `current_durability_token` on side A and `wait_for_token` on
//! side B).

use std::time::{Duration, Instant};

use async_trait::async_trait;
use sqlx::SqlitePool;

use super::{CanonicalStore, DurabilityToken};

pub struct SqliteCanonicalStore {
    pub(super) pool: SqlitePool,
    instance_name: String,
    /// SQLite has no schemas — `outbox_relation` is just the table
    /// name (no qualifier). Validated case-by-case.
    outbox_table: String,
}

impl SqliteCanonicalStore {
    pub fn new(
        pool: SqlitePool,
        instance_name: impl Into<String>,
        outbox_table: impl Into<String>,
    ) -> Self {
        Self {
            pool,
            instance_name: instance_name.into(),
            outbox_table: outbox_table.into(),
        }
    }

    fn safe_table(&self) -> Result<&str, String> {
        let t = self.outbox_table.as_str();
        if t.is_empty() || !t.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
            return Err(format!(
                "unsafe outbox_table '{t}' (SQLite identifiers must be [A-Za-z0-9_]+)"
            ));
        }
        Ok(t)
    }
}

#[async_trait]
impl CanonicalStore for SqliteCanonicalStore {
    fn backend_label(&self) -> &'static str {
        "sqlite"
    }

    fn instance_name(&self) -> &str {
        &self.instance_name
    }

    async fn current_durability_token(&self) -> Result<DurabilityToken, String> {
        let v: (i64,) = sqlx::query_as("PRAGMA data_version")
            .fetch_one(&self.pool)
            .await
            .map_err(|e| format!("PRAGMA data_version failed: {e}"))?;
        Ok(DurabilityToken::new("sqlite", v.0.to_string()))
    }

    async fn wait_for_token(
        &self,
        token: &DurabilityToken,
        timeout: Duration,
    ) -> Result<bool, String> {
        if !token.is_for("sqlite") {
            return Err(format!(
                "SqliteCanonicalStore cannot wait on a '{}' token",
                token.backend_label
            ));
        }
        let target: i64 = token
            .value
            .parse()
            .map_err(|e| format!("invalid sqlite data_version '{}': {e}", token.value))?;
        let started = Instant::now();
        let poll = crate::runtime::canonical_store::durability_poll_interval(
            timeout,
            crate::runtime::canonical_store::SQLITE_DURABILITY_POLL_MS,
        );
        loop {
            let (current,): (i64,) = sqlx::query_as("PRAGMA data_version")
                .fetch_one(&self.pool)
                .await
                .map_err(|e| format!("PRAGMA data_version poll failed: {e}"))?;
            if current >= target {
                return Ok(true);
            }
            if started.elapsed() >= timeout {
                return Ok(false);
            }
            tokio::time::sleep(poll).await;
        }
    }

    async fn enqueue_outbox_event(
        &self,
        event_id: &str,
        topic: &str,
        partition_key: &str,
        payload: &serde_json::Value,
    ) -> Result<i64, String> {
        let table = self.safe_table()?;
        // SQLite uses ROWID as the auto-increment for `INTEGER
        // PRIMARY KEY` columns. `last_insert_rowid()` returns it.
        let sql = format!(
            "INSERT INTO {table} (event_id, topic, partition_key, payload, created_at) \
             VALUES (?, ?, ?, ?, strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))"
        );
        // sqlx-sqlite serialises serde_json::Value to TEXT.
        let payload_text = serde_json::to_string(payload)
            .map_err(|e| format!("payload JSON serialise failed: {e}"))?;
        let result = sqlx::query(&sql)
            .bind(event_id)
            .bind(topic)
            .bind(partition_key)
            .bind(payload_text)
            .execute(&self.pool)
            .await
            .map_err(|e| format!("outbox insert (sqlite) failed: {e}"))?;
        Ok(result.last_insert_rowid())
    }

    async fn outbox_max_seq(&self) -> Result<i64, String> {
        let table = self.safe_table()?;
        let sql = format!("SELECT COALESCE(MAX(event_seq), 0) FROM {table}");
        let (max,): (i64,) = sqlx::query_as(&sql)
            .fetch_one(&self.pool)
            .await
            .map_err(|e| format!("outbox max seq (sqlite) failed: {e}"))?;
        Ok(max)
    }

    async fn ensure_system_tables(&self) -> Result<(), String> {
        let table = self.safe_table()?;
        // SQLite: `INTEGER PRIMARY KEY` is the auto-increment alias
        // for ROWID. `TEXT` for JSON since sqlite is typeless; the
        // app side parses with serde_json.
        //
        // B.7: outbox DDL comes from the shared `sql_schema` renderer (single
        // source of truth across SQL backends); execute/error-handling below
        // is unchanged.
        let sql = super::sql_schema::sqlite_outbox_ddl(table);
        sqlx::query(&sql)
            .execute(&self.pool)
            .await
            .map_err(|e| format!("ensure_system_tables (sqlite) failed: {e}"))?;
        Ok(())
    }

    async fn ensure_advisory_lease_table(&self) -> Result<(), String> {
        let sql = "
            CREATE TABLE IF NOT EXISTS udb_advisory_leases (
                lease_name VARCHAR(255) PRIMARY KEY,
                owner_id   VARCHAR(255) NOT NULL,
                expires_at TEXT NOT NULL
            )
        ";
        sqlx::query(sql)
            .execute(&self.pool)
            .await
            .map_err(|e| format!("ensure_advisory_lease_table (sqlite) failed: {e}"))?;
        Ok(())
    }

    async fn try_acquire_advisory_lease(
        &self,
        lease_name: &str,
        owner_id: &str,
        ttl: std::time::Duration,
    ) -> Result<bool, String> {
        // Atomic acquire in one statement:
        //   INSERT a new row, OR re-claim an expired row via ON
        //   CONFLICT DO UPDATE WHERE expires_at < NOW().
        // SQLite supports `ON CONFLICT(col) DO UPDATE SET … WHERE …`
        // since 3.24 — sqlx-sqlite ships a newer build.
        // The expires_at value is computed Rust-side so the comparison
        // uses a stable cutoff.
        let now = chrono::Utc::now();
        let now_iso = now.to_rfc3339();
        let expires = now + chrono::Duration::seconds(ttl.as_secs() as i64);
        let expires_iso = expires.to_rfc3339();
        // Confirm ownership in the SAME statement via RETURNING (sqlx-sqlite
        // supports it) — a separate follow-up SELECT was a TOCTOU window where a
        // concurrent caller could claim the lease between the upsert and the
        // confirmation, yielding a false-positive. RETURNING owner_id only emits
        // a row when our INSERT fired or the ON CONFLICT UPDATE matched (expired
        // takeover or our own refresh); a live row owned by another caller skips
        // the UPDATE and returns no row → Ok(false).
        let sql = "
            INSERT INTO udb_advisory_leases (lease_name, owner_id, expires_at)
            VALUES (?, ?, ?)
            ON CONFLICT(lease_name) DO UPDATE
              SET owner_id   = excluded.owner_id,
                  expires_at = excluded.expires_at
              WHERE udb_advisory_leases.expires_at < ?
                 OR udb_advisory_leases.owner_id = excluded.owner_id
            RETURNING owner_id
        ";
        let resulting_owner: Option<String> = sqlx::query_scalar(sql)
            .bind(lease_name)
            .bind(owner_id)
            .bind(&expires_iso)
            .bind(&now_iso)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| format!("try_acquire_advisory_lease (sqlite) failed: {e}"))?;
        Ok(matches!(resulting_owner, Some(o) if o == owner_id))
    }

    async fn release_advisory_lease(&self, lease_name: &str, owner_id: &str) -> Result<(), String> {
        let sql = "DELETE FROM udb_advisory_leases WHERE lease_name = ? AND owner_id = ?";
        sqlx::query(sql)
            .bind(lease_name)
            .bind(owner_id)
            .execute(&self.pool)
            .await
            .map_err(|e| format!("release_advisory_lease (sqlite) failed: {e}"))?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use sqlx::sqlite::SqlitePoolOptions;
    use std::path::PathBuf;
    use uuid::Uuid;

    struct TempSqliteDb {
        path: PathBuf,
    }

    impl TempSqliteDb {
        fn new(prefix: &str) -> Self {
            let path = std::env::temp_dir().join(format!("{prefix}-{}.db", Uuid::new_v4()));
            let db = Self { path };
            db.cleanup();
            db
        }

        fn dsn(&self) -> String {
            let path = self.path.to_string_lossy().replace('\\', "/");
            format!("sqlite://{path}?mode=rwc")
        }

        fn cleanup(&self) {
            let base = self.path.to_string_lossy();
            for path in [
                self.path.clone(),
                PathBuf::from(format!("{base}-wal")),
                PathBuf::from(format!("{base}-shm")),
            ] {
                let _ = std::fs::remove_file(path);
            }
        }
    }

    impl Drop for TempSqliteDb {
        fn drop(&mut self) {
            self.cleanup();
        }
    }

    async fn in_memory_pool() -> SqlitePool {
        SqlitePoolOptions::new()
            .max_connections(1) // :memory: is per-connection in SQLite
            .connect("sqlite::memory:")
            .await
            .expect("in-memory sqlite connect")
    }

    /// Pin: backend label is `"sqlite"` exactly.
    #[tokio::test]
    async fn backend_label_is_pinned() {
        let pool = in_memory_pool().await;
        let store = SqliteCanonicalStore::new(pool, "test", "udb_outbox_events");
        assert_eq!(store.backend_label(), "sqlite");
        assert_eq!(store.instance_name(), "test");
    }

    /// Pin: end-to-end smoke against in-memory SQLite. Proves the
    /// canonical store contract works without any external database
    /// — closes the U27 in-memory test profile follow-up.
    #[tokio::test]
    async fn in_memory_round_trip_proves_p2p_works_without_postgres() {
        let pool = in_memory_pool().await;
        let store = SqliteCanonicalStore::new(pool, "test", "udb_outbox_events");
        store.ensure_system_tables().await.expect("DDL");

        // Initial outbox is empty.
        assert_eq!(store.outbox_max_seq().await.expect("max seq"), 0);

        // Capture a token before any writes.
        let token_a = store.current_durability_token().await.expect("token a");
        assert_eq!(token_a.backend_label, "sqlite");

        // Enqueue one event.
        let seq = store
            .enqueue_outbox_event(
                "11111111-1111-1111-1111-111111111111",
                "test.topic",
                "pk-1",
                &serde_json::json!({"hello": "world"}),
            )
            .await
            .expect("enqueue");
        assert_eq!(seq, 1, "first auto-increment");
        assert_eq!(store.outbox_max_seq().await.expect("max seq"), 1);

        // Token after the write should be >= the earlier token.
        let token_b = store.current_durability_token().await.expect("token b");
        let a: i64 = token_a.value.parse().unwrap();
        let b: i64 = token_b.value.parse().unwrap();
        assert!(b >= a, "data_version is monotone");

        // wait_for_token at the current value clears instantly.
        let cleared = store
            .wait_for_token(&token_b, Duration::from_millis(10))
            .await
            .expect("wait");
        assert!(cleared);
    }

    /// Pin: cross-backend token rejected.
    #[tokio::test]
    async fn rejects_non_sqlite_token() {
        let pool = in_memory_pool().await;
        let store = SqliteCanonicalStore::new(pool, "test", "udb_outbox_events");
        let mysql_token = DurabilityToken::new("mysql", "gtid:xyz");
        let err = store
            .wait_for_token(&mysql_token, Duration::from_millis(1))
            .await
            .expect_err("must reject cross-backend token");
        assert!(err.contains("cannot wait on a 'mysql'"), "got: {err}");
    }

    /// Pin: NW1-3c — advisory lease acquire / release / contention.
    /// Two callers race; only one wins. Releasing lets the next win.
    /// Expired leases can be re-acquired.
    #[tokio::test]
    async fn advisory_lease_acquire_release_contention() {
        let pool = in_memory_pool().await;
        let store = SqliteCanonicalStore::new(pool, "test", "udb_outbox_events");
        store.ensure_advisory_lease_table().await.unwrap();

        let lease = "saga-worker";

        // Alice acquires.
        let alice = store
            .try_acquire_advisory_lease(lease, "alice", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(alice, "first acquire wins");

        // Bob tries — fails.
        let bob = store
            .try_acquire_advisory_lease(lease, "bob", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(
            !bob,
            "second acquirer must lose while Alice holds the lease"
        );

        // Alice re-acquires (refreshes own lease) — succeeds.
        let alice_again = store
            .try_acquire_advisory_lease(lease, "alice", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(alice_again, "owner can refresh own lease");

        // Alice releases.
        store.release_advisory_lease(lease, "alice").await.unwrap();

        // Bob now wins.
        let bob_now = store
            .try_acquire_advisory_lease(lease, "bob", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(bob_now, "after release, next acquirer wins");

        // Release by wrong owner is a no-op (idempotent).
        store.release_advisory_lease(lease, "alice").await.unwrap();
        // Bob still owns.
        let bob_still = store
            .try_acquire_advisory_lease(lease, "bob", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(
            bob_still,
            "Bob still holds after wrong-owner release attempt"
        );
    }

    /// Pin: expired leases let a new owner in. Uses ttl=0 + sleep to
    /// force expiration.
    #[tokio::test]
    async fn expired_lease_is_re_acquirable() {
        let pool = in_memory_pool().await;
        let store = SqliteCanonicalStore::new(pool, "test", "udb_outbox_events");
        store.ensure_advisory_lease_table().await.unwrap();

        let lease = "ephemeral";
        store
            .try_acquire_advisory_lease(lease, "first", Duration::from_secs(0))
            .await
            .unwrap();
        // Sleep past the 0-second TTL.
        tokio::time::sleep(Duration::from_millis(100)).await;

        // New owner acquires the now-expired lease.
        let second = store
            .try_acquire_advisory_lease(lease, "second", Duration::from_secs(60))
            .await
            .unwrap();
        assert!(second, "expired lease must be re-acquirable");
    }

    /// Phase 1 HA pin: model two broker replicas with independent pools pointed
    /// at the same canonical SQLite file. The outbox sequence allocator must
    /// produce distinct monotone event_seq values under concurrent enqueues.
    #[tokio::test]
    async fn two_store_handles_allocate_distinct_outbox_sequences() {
        let db = TempSqliteDb::new("udb-outbox-ha");
        let dsn = db.dsn();

        let pool_a = SqlitePoolOptions::new()
            .max_connections(2)
            .connect(&dsn)
            .await
            .expect("sqlite file pool a");
        let pool_b = SqlitePoolOptions::new()
            .max_connections(2)
            .connect(&dsn)
            .await
            .expect("sqlite file pool b");
        let store_a = SqliteCanonicalStore::new(pool_a, "broker-a", "udb_outbox_events");
        let store_b = SqliteCanonicalStore::new(pool_b, "broker-b", "udb_outbox_events");
        store_a.ensure_system_tables().await.expect("DDL from A");
        store_b.ensure_system_tables().await.expect("DDL from B");
        let payload_a = serde_json::json!({"broker": "a"});
        let payload_b = serde_json::json!({"broker": "b"});

        let (seq_a, seq_b) = tokio::join!(
            store_a.enqueue_outbox_event(
                "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
                "test.outbox.ha",
                "pk-a",
                &payload_a,
            ),
            store_b.enqueue_outbox_event(
                "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb",
                "test.outbox.ha",
                "pk-b",
                &payload_b,
            )
        );
        let mut seqs = vec![seq_a.expect("seq a"), seq_b.expect("seq b")];
        seqs.sort_unstable();
        assert_eq!(seqs, vec![1, 2]);
        assert_eq!(store_a.outbox_max_seq().await.expect("max seq"), 2);
        drop(store_a);
        drop(store_b);
    }

    /// Pin: unsafe table name (SQLite identifiers don't allow `.` or
    /// `"` like PG/MySQL) is rejected.
    #[tokio::test]
    async fn unsafe_table_name_is_rejected() {
        let pool = in_memory_pool().await;
        let store = SqliteCanonicalStore::new(pool, "test", "evil; DROP");
        assert!(store.safe_table().is_err());
        // Even a schema-qualified name is rejected — SQLite doesn't
        // support them.
        let store2 = SqliteCanonicalStore::new(
            SqlitePool::connect_lazy("sqlite::memory:").unwrap(),
            "test",
            "udb_system.events",
        );
        assert!(store2.safe_table().is_err());
    }
}