backbone-orm 3.15.0

Backbone Framework ORM - Database layer with PostgreSQL support
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
//! Live proof of the write transaction: one transaction around everything a write does.
//!
//! Gated on `BACKBONE_ORM_RLS_DSN` (a superuser DSN) — the test creates a schema and a login
//! role, and drops both. When it is unset the test skips, so a checkout without a database
//! still passes.
//!
//! What it holds the code to:
//! - a write made inside `in_write_tx` and a later failure roll back everything, bulk writes
//!   included, on a pool connection and on a request-scope connection alike;
//! - the write rides the one connection the request holds (a one-connection pool would hang the
//!   moment anything took a second one);
//! - without `in_write_tx` the repository behaves as it always did;
//! - row-level security decides inside the transaction as it does outside it;
//! - a nested transaction is a savepoint, a swallowed statement error cannot be committed past,
//!   and a dropped future leaves no open transaction behind.

use std::time::Duration;

use backbone_orm::company_scope::{with_company_scope, with_request_scope};
use backbone_orm::generic_repository::{EntityRepoMeta, GenericCrudRepository, SoftDelete};
use backbone_orm::in_write_tx;
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPoolOptions;
use sqlx::PgPool;
use std::collections::HashMap;
use uuid::Uuid;

fn admin_dsn() -> Option<String> {
    std::env::var("BACKBONE_ORM_RLS_DSN").ok()
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, sqlx::FromRow)]
struct Item {
    id: Uuid,
    company_id: Uuid,
    name: String,
    metadata: serde_json::Value,
}

impl EntityRepoMeta for Item {
    fn column_types() -> HashMap<String, String> {
        HashMap::from([("id".to_string(), "uuid".to_string())])
    }
    fn search_fields() -> &'static [&'static str] {
        &["name"]
    }
}

fn item(company: Uuid, name: &str) -> Item {
    Item {
        id: Uuid::new_v4(),
        company_id: company,
        name: name.into(),
        metadata: serde_json::json!({}),
    }
}

struct Harness {
    admin: PgPool,
    schema: String,
    role: String,
    dsn: String,
}

impl Harness {
    async fn new(dsn: String) -> Self {
        let admin = PgPoolOptions::new()
            .max_connections(3)
            .connect(&dsn)
            .await
            .unwrap();
        let suffix = &Uuid::new_v4().simple().to_string()[..8];
        let schema = format!("wtx_{suffix}");
        let role = format!("wtx_app_{suffix}");
        sqlx::raw_sql(&format!(
            "CREATE SCHEMA {schema};
             CREATE TABLE {schema}.items (
                 id         uuid PRIMARY KEY,
                 company_id uuid NOT NULL,
                 name       text NOT NULL CONSTRAINT ck_items__name_refused CHECK (name <> 'refused'),
                 metadata   jsonb NOT NULL DEFAULT '{{}}'::jsonb
             );
             ALTER TABLE {schema}.items ENABLE ROW LEVEL SECURITY;
             ALTER TABLE {schema}.items FORCE ROW LEVEL SECURITY;
             CREATE POLICY items_company_isolation ON {schema}.items FOR ALL
                 USING      (company_id = NULLIF(current_setting('app.company_id', true), '')::uuid)
                 WITH CHECK (company_id = NULLIF(current_setting('app.company_id', true), '')::uuid);
             CREATE ROLE {role} LOGIN PASSWORD 'wtxpw';
             GRANT USAGE ON SCHEMA {schema} TO {role};
             GRANT SELECT, INSERT, UPDATE, DELETE ON {schema}.items TO {role};"
        ))
        .execute(&admin)
        .await
        .unwrap();
        Self {
            admin,
            schema,
            role,
            dsn,
        }
    }

    /// A pool that logs in as the non-superuser request role. One connection when `single`, so
    /// anything that takes a second connection while the first is held would hang.
    async fn app_pool(&self, single: bool) -> PgPool {
        let after_at = self.dsn.rsplit('@').next().unwrap();
        let url = format!("postgresql://{}:wtxpw@{after_at}", self.role);
        PgPoolOptions::new()
            .max_connections(if single { 1 } else { 4 })
            .acquire_timeout(Duration::from_secs(5))
            .connect(&url)
            .await
            .unwrap()
    }

    fn repo(&self, pool: &PgPool) -> GenericCrudRepository<Item, SoftDelete> {
        GenericCrudRepository::new(pool.clone(), &format!("{}.items", self.schema))
    }

    /// Rows as the owner sees them (RLS does not apply to a superuser).
    async fn count(&self) -> i64 {
        sqlx::query_scalar(&format!("SELECT COUNT(*) FROM {}.items", self.schema))
            .fetch_one(&self.admin)
            .await
            .unwrap()
    }

    async fn teardown(self) {
        sqlx::raw_sql(&format!("DROP SCHEMA IF EXISTS {} CASCADE", self.schema))
            .execute(&self.admin)
            .await
            .unwrap();
        sqlx::raw_sql(&format!("DROP ROLE IF EXISTS {}", self.role))
            .execute(&self.admin)
            .await
            .unwrap();
    }
}

macro_rules! harness {
    () => {{
        let Some(dsn) = admin_dsn() else {
            eprintln!("skipping: BACKBONE_ORM_RLS_DSN not set");
            return;
        };
        Harness::new(dsn).await
    }};
}

type Res<T> = anyhow::Result<T>;

#[tokio::test]
async fn a_later_failure_rolls_back_every_write_of_the_transaction_on_a_pool_connection() {
    let h = harness!();
    let pool = h.app_pool(false).await;
    let repo = h.repo(&pool);
    let a = Uuid::new_v4();

    let failed: Res<()> = with_company_scope(
        Some(a),
        in_write_tx(&pool, async {
            let created = repo.create(&item(a, "one")).await?;
            // Inside the transaction the row is visible to the repository, and only to it.
            assert!(repo.find_by_id(&created.id.to_string()).await?.is_some());
            assert_eq!(
                h.count().await,
                0,
                "an uncommitted row must not be visible to another connection"
            );
            repo.bulk_create(&[item(a, "two"), item(a, "three")])
                .await?;
            anyhow::bail!("a later step failed")
        }),
    )
    .await;
    assert!(failed.is_err());
    assert_eq!(
        h.count().await,
        0,
        "the failure must undo the create and the bulk create"
    );

    let committed: Res<()> = with_company_scope(
        Some(a),
        in_write_tx(&pool, async {
            repo.create(&item(a, "one")).await?;
            repo.bulk_create(&[item(a, "two"), item(a, "three")])
                .await?;
            Ok(())
        }),
    )
    .await;
    committed.unwrap();
    assert_eq!(
        h.count().await,
        3,
        "a successful transaction commits all of it"
    );
    h.teardown().await;
}

#[tokio::test]
async fn the_transaction_rides_the_one_connection_of_a_request_scope() {
    let h = harness!();
    // A single connection: the request scope holds it, so a write transaction that took another
    // one would wait for the pool until the acquire times out.
    let pool = h.app_pool(true).await;
    let repo = h.repo(&pool);
    let a = Uuid::new_v4();

    let failed: Res<()> = with_request_scope(&pool, a, async {
        in_write_tx(&pool, async {
            repo.create(&item(a, "one")).await?;
            repo.bulk_create(&[item(a, "two"), item(a, "three")])
                .await?;
            anyhow::bail!("a later step failed")
        })
        .await
    })
    .await
    .unwrap();
    assert!(failed.is_err());
    assert_eq!(h.count().await, 0);

    let done: Res<usize> = with_request_scope(&pool, a, async {
        in_write_tx(&pool, async {
            repo.create(&item(a, "one")).await?;
            let rows = repo
                .bulk_create(&[item(a, "two"), item(a, "three")])
                .await?;
            Ok(rows.len())
        })
        .await
    })
    .await
    .unwrap();
    assert_eq!(done.unwrap(), 2);
    assert_eq!(h.count().await, 3);
    h.teardown().await;
}

#[tokio::test]
async fn without_a_write_transaction_every_write_commits_on_its_own_as_before() {
    let h = harness!();
    let pool = h.app_pool(false).await;
    let repo = h.repo(&pool);
    let a = Uuid::new_v4();

    // Outside any transaction a create is durable the moment it returns, whatever follows.
    let outcome: Res<()> = with_company_scope(Some(a), async {
        repo.create(&item(a, "one")).await?;
        anyhow::bail!("a later step failed")
    })
    .await;
    assert!(outcome.is_err());
    assert_eq!(
        h.count().await,
        1,
        "no write transaction means no rollback of an earlier write"
    );

    // A bulk create is still all-or-nothing on its own.
    let refused = with_company_scope(
        Some(a),
        repo.bulk_create(&[item(a, "two"), item(a, "refused"), item(a, "three")]),
    )
    .await;
    assert!(refused.is_err());
    assert_eq!(h.count().await, 1, "the bulk create rolled itself back");

    let rows = with_company_scope(
        Some(a),
        repo.bulk_create(&[item(a, "two"), item(a, "three")]),
    )
    .await
    .unwrap();
    assert_eq!(rows.len(), 2);
    assert_eq!(h.count().await, 3);
    h.teardown().await;
}

#[tokio::test]
async fn row_level_security_still_decides_inside_the_transaction() {
    let h = harness!();
    let pool = h.app_pool(false).await;
    let repo = h.repo(&pool);
    let (a, b) = (Uuid::new_v4(), Uuid::new_v4());
    let foreign = item(b, "b-row");
    sqlx::query(&format!(
        "INSERT INTO {}.items (id, company_id, name) VALUES ($1, $2, $3)",
        h.schema
    ))
    .bind(foreign.id)
    .bind(b)
    .bind(&foreign.name)
    .execute(&h.admin)
    .await
    .unwrap();

    // A's transaction does not see B's row, and a write forging B's company is refused.
    let seen: Res<Option<Item>> = with_company_scope(
        Some(a),
        in_write_tx(&pool, async {
            Ok(repo.find_by_id(&foreign.id.to_string()).await?)
        }),
    )
    .await;
    assert!(
        seen.unwrap().is_none(),
        "the fence must hold inside the transaction"
    );

    let forged: Res<()> = with_company_scope(
        Some(a),
        in_write_tx(&pool, async {
            repo.create(&item(a, "mine")).await?;
            repo.create(&item(b, "forged")).await?;
            Ok(())
        }),
    )
    .await;
    assert!(forged.is_err(), "WITH CHECK must refuse the forged company");
    assert_eq!(
        h.count().await,
        1,
        "the refusal undid A's own earlier write too"
    );

    // No scope at all fails closed: the write is refused, never unfenced.
    let unscoped: Res<()> = in_write_tx(&pool, async {
        repo.create(&item(a, "orphan")).await?;
        Ok(())
    })
    .await;
    assert!(
        unscoped.is_err(),
        "an unscoped write transaction must not write to a fenced table"
    );

    // The request-scope path holds the same line.
    let forged_in_request: Res<()> = with_request_scope(&pool, a, async {
        in_write_tx(&pool, async {
            repo.create(&item(b, "forged")).await?;
            Ok(())
        })
        .await
    })
    .await
    .unwrap();
    assert!(forged_in_request.is_err());
    assert_eq!(h.count().await, 1);
    h.teardown().await;
}

#[tokio::test]
async fn a_nested_transaction_is_a_savepoint_and_a_bulk_failure_leaves_the_outer_one_usable() {
    let h = harness!();
    let pool = h.app_pool(false).await;
    let repo = h.repo(&pool);
    let a = Uuid::new_v4();

    let outcome: Res<()> = with_company_scope(
        Some(a),
        in_write_tx(&pool, async {
            repo.create(&item(a, "outer")).await?;
            // The inner transaction fails: only its own work is undone.
            let inner: Res<()> = in_write_tx(&pool, async {
                repo.create(&item(a, "inner")).await?;
                anyhow::bail!("inner step failed")
            })
            .await;
            assert!(inner.is_err());
            // A bulk create refused partway rolls back its own rows and nothing else.
            let bulk = repo.bulk_create(&[item(a, "x"), item(a, "refused")]).await;
            assert!(bulk.is_err());
            repo.create(&item(a, "after")).await?;
            Ok(())
        }),
    )
    .await;
    outcome.unwrap();
    let names: Vec<String> = sqlx::query_scalar(&format!(
        "SELECT name FROM {}.items ORDER BY name",
        h.schema
    ))
    .fetch_all(&h.admin)
    .await
    .unwrap();
    assert_eq!(names, vec!["after".to_string(), "outer".to_string()]);

    // Joined, the inner work waits for the outer commit: the outer failing undoes it too.
    let outer_fails: Res<()> = with_company_scope(
        Some(a),
        in_write_tx(&pool, async {
            let inner: Res<()> = in_write_tx(&pool, async {
                repo.create(&item(a, "inner-ok")).await?;
                Ok(())
            })
            .await;
            inner?;
            anyhow::bail!("outer step failed")
        }),
    )
    .await;
    assert!(outer_fails.is_err());
    assert_eq!(h.count().await, 2);
    h.teardown().await;
}

#[tokio::test]
async fn a_swallowed_statement_error_cannot_be_committed_past() {
    let h = harness!();
    let pool = h.app_pool(false).await;
    let repo = h.repo(&pool);
    let a = Uuid::new_v4();

    // A hook that catches the database's refusal and carries on leaves the transaction aborted.
    // Postgres would answer COMMIT with a quiet ROLLBACK; the write transaction must not call
    // that a success.
    let outcome: Res<()> = with_company_scope(
        Some(a),
        in_write_tx(&pool, async {
            repo.create(&item(a, "kept?")).await?;
            let _ignored = repo.create(&item(a, "refused")).await;
            Ok(())
        }),
    )
    .await;
    assert!(
        outcome.is_err(),
        "an aborted transaction must surface as an error, not as success"
    );
    assert_eq!(h.count().await, 0);
    h.teardown().await;
}

#[tokio::test]
async fn a_dropped_future_leaves_no_transaction_open_and_no_row_behind() {
    let h = harness!();
    // One connection: the connection the dropped transaction used is the only one there is.
    let pool = h.app_pool(true).await;
    let repo = h.repo(&pool);
    let a = Uuid::new_v4();

    let abandoned = tokio::time::timeout(
        Duration::from_millis(400),
        with_company_scope(
            Some(a),
            in_write_tx(&pool, async {
                repo.create(&item(a, "doomed")).await?;
                // Never finishes: the timeout drops this future mid-transaction.
                std::future::pending::<()>().await;
                Ok::<(), anyhow::Error>(())
            }),
        ),
    )
    .await;
    assert!(abandoned.is_err(), "the write must have been cut off");

    // The rollback runs from a spawned task; the pool hands the connection back only after it.
    let open: i64 = sqlx::query_scalar(
        "SELECT COUNT(*) FROM pg_stat_activity \
         WHERE usename = current_user AND state LIKE 'idle in transaction%'",
    )
    .fetch_one(&pool)
    .await
    .unwrap();
    assert_eq!(open, 0, "a dropped write transaction must not stay open");
    assert_eq!(h.count().await, 0, "and its rows must be gone");
    h.teardown().await;
}