mongreldb-kit 0.15.0

Schema-aware, storage-backed application persistence layer for MongrelDB with migrations, relational constraints, and a query builder.
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
//! Cross-language correctness fixes mirrored from the TypeScript kit:
//!
//! 1. Nullable columns store real `NULL` (not a zero/empty sentinel); an omitted
//!    nullable column reads back as null and the insert return normalizes it.
//! 2. A plain `default: now` column is insert-only; only a `generated` now column
//!    refreshes on update.
//! 3. Single-column primary-key collisions throw a duplicate error (not upsert).
//! 4. A unique index enforces uniqueness like a `unique()` constraint.

use mongreldb_kit::{
    Column, ColumnType, Database, DefaultKind, Expr, ForeignKey, ForeignKeyAction, Index, KitError,
    Query, Schema, Select, Table, UniqueConstraint,
};
use serde_json::{json, Map, Value};
use std::path::PathBuf;

fn temp_dir() -> PathBuf {
    tempfile::tempdir().unwrap().keep()
}

fn col(id: u32, name: &str, ty: ColumnType) -> Column {
    Column::new(id, name, ty)
}

fn nullable(mut c: Column) -> Column {
    c.nullable = true;
    c
}

fn row(pairs: &[(&str, Value)]) -> Map<String, Value> {
    pairs
        .iter()
        .map(|(k, v)| (k.to_string(), v.clone()))
        .collect()
}

fn select_filter(table: &str, filter: Expr) -> Query {
    Query::Select(Select {
        table: table.into(),
        columns: vec![],
        filter: Some(filter),
        order_by: vec![],
        limit: None,
        offset: None,
    })
}

// ── 1. nullable columns store real NULL + insert-return normalization ─────────

#[test]
fn nullable_columns_store_real_null_and_round_trip() {
    let dir = temp_dir();
    let schema = Schema::new(vec![Table {
        id: 1,
        name: "t".into(),
        columns: vec![
            col(1, "id", ColumnType::Int64),
            nullable(col(2, "val", ColumnType::Int64)),
            nullable(col(3, "txt", ColumnType::Text)),
        ],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![],
        unique_constraints: vec![],
        check_constraints: vec![],
    }])
    .unwrap();
    let db = Database::create(&dir, schema).unwrap();

    let mut txn = db.begin().unwrap();
    // Row 1 omits both nullable columns entirely.
    let inserted = txn.insert("t", row(&[("id", json!(1))])).unwrap();
    // The insert return normalizes omitted columns to explicit null.
    assert_eq!(inserted.values.get("val"), Some(&Value::Null));
    assert_eq!(inserted.values.get("txt"), Some(&Value::Null));
    // Row 2 has a real zero and a real empty string, distinct from null.
    txn.insert(
        "t",
        row(&[("id", json!(2)), ("val", json!(0)), ("txt", json!(""))]),
    )
    .unwrap();
    txn.commit().unwrap();

    let txn = db.begin().unwrap();
    let r1 = txn.get_by_pk("t", &json!(1)).unwrap().unwrap();
    assert_eq!(r1.values.get("val"), Some(&Value::Null));
    assert_eq!(r1.values.get("txt"), Some(&Value::Null));
    let r2 = txn.get_by_pk("t", &json!(2)).unwrap().unwrap();
    assert_eq!(r2.values.get("val"), Some(&json!(0)));
    assert_eq!(r2.values.get("txt"), Some(&json!("")));

    // is_null matches the real null only; is_not_null matches the real zero.
    let nulls = txn
        .select(&select_filter(
            "t",
            Expr::IsNull(Box::new(Expr::Column("val".into()))),
        ))
        .unwrap();
    assert_eq!(nulls.len(), 1);
    assert_eq!(nulls[0].values.get("id"), Some(&json!(1)));

    let not_nulls = txn
        .select(&select_filter(
            "t",
            Expr::IsNotNull(Box::new(Expr::Column("val".into()))),
        ))
        .unwrap();
    assert_eq!(not_nulls.len(), 1);
    assert_eq!(not_nulls[0].values.get("id"), Some(&json!(2)));
}

// ── 2. `now` default is insert-only; `generated` now refreshes on update ──────

fn timestamps_schema() -> Schema {
    let created = Column {
        default: Some(DefaultKind::Now),
        generated: false,
        ..col(3, "created_at", ColumnType::Text)
    };
    let updated = Column {
        default: Some(DefaultKind::Now),
        generated: true,
        ..col(4, "updated_at", ColumnType::Text)
    };
    Schema::new(vec![Table {
        id: 1,
        name: "docs".into(),
        columns: vec![
            col(1, "id", ColumnType::Int64),
            col(2, "body", ColumnType::Text),
            created,
            updated,
        ],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![],
        unique_constraints: vec![],
        check_constraints: vec![],
    }])
    .unwrap()
}

#[test]
fn now_default_not_refreshed_on_update_but_generated_now_is() {
    let dir = temp_dir();
    let db = Database::create(&dir, timestamps_schema()).unwrap();

    // Seed with explicit, far-past timestamps so the test is robust to the
    // one-second resolution of `iso_now`.
    let past = "2000-01-01T00:00:00Z";
    let mut txn = db.begin().unwrap();
    txn.insert(
        "docs",
        row(&[
            ("id", json!(1)),
            ("body", json!("a")),
            ("created_at", json!(past)),
            ("updated_at", json!(past)),
        ]),
    )
    .unwrap();
    txn.commit().unwrap();

    let mut txn = db.begin().unwrap();
    let updated = txn
        .update("docs", &json!(1), row(&[("body", json!("b"))]))
        .unwrap();
    txn.commit().unwrap();

    // created_at (plain `now` default) must NOT change on update.
    assert_eq!(updated.values.get("created_at"), Some(&json!(past)));
    // updated_at (`generated` now) refreshes to the current time.
    assert_ne!(updated.values.get("updated_at"), Some(&json!(past)));

    let txn = db.begin().unwrap();
    let r = txn.get_by_pk("docs", &json!(1)).unwrap().unwrap();
    assert_eq!(r.values.get("created_at"), Some(&json!(past)));
    assert_ne!(r.values.get("updated_at"), Some(&json!(past)));
    assert_eq!(r.values.get("body"), Some(&json!("b")));
}

// ── 3. single-column primary-key collisions throw + re-insert after delete ────

#[test]
fn duplicate_single_pk_throws_and_reinsert_after_delete_works() {
    let dir = temp_dir();
    let schema = Schema::new(vec![Table {
        id: 1,
        name: "k".into(),
        columns: vec![
            col(1, "id", ColumnType::Int64),
            col(2, "v", ColumnType::Text),
        ],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![],
        unique_constraints: vec![],
        check_constraints: vec![],
    }])
    .unwrap();
    let db = Database::create(&dir, schema).unwrap();

    let mut txn = db.begin().unwrap();
    txn.insert("k", row(&[("id", json!(1)), ("v", json!("a"))]))
        .unwrap();
    txn.commit().unwrap();

    // A duplicate single-column PK is rejected, not upserted.
    let mut txn = db.begin().unwrap();
    let err = txn
        .insert("k", row(&[("id", json!(1)), ("v", json!("b"))]))
        .unwrap_err();
    assert!(matches!(err, KitError::Duplicate(_)), "got {err:?}");
    txn.rollback();
    // The original row is intact (no last-writer-wins upsert happened).
    let txn = db.begin().unwrap();
    assert_eq!(
        txn.get_by_pk("k", &json!(1))
            .unwrap()
            .unwrap()
            .values
            .get("v"),
        Some(&json!("a"))
    );

    // After deleting the row its PK guard is reclaimed and re-insert succeeds.
    let mut txn = db.begin().unwrap();
    txn.delete("k", &json!(1)).unwrap();
    txn.commit().unwrap();

    let mut txn = db.begin().unwrap();
    txn.insert("k", row(&[("id", json!(1)), ("v", json!("c"))]))
        .unwrap();
    txn.commit().unwrap();

    let txn = db.begin().unwrap();
    assert_eq!(
        txn.get_by_pk("k", &json!(1))
            .unwrap()
            .unwrap()
            .values
            .get("v"),
        Some(&json!("c"))
    );
}

// ── 4. a unique index enforces uniqueness like a unique() constraint ──────────

#[test]
fn unique_index_enforces_uniqueness() {
    let dir = temp_dir();
    let schema = Schema::new(vec![Table {
        id: 1,
        name: "u".into(),
        columns: vec![
            col(1, "id", ColumnType::Int64),
            col(2, "email", ColumnType::Text),
        ],
        primary_key: vec!["id".into()],
        indexes: vec![Index {
            name: "idx_email".into(),
            columns: vec!["email".into()],
            unique: true,
            kind: Default::default(),
        }],
        foreign_keys: vec![],
        // No explicit unique constraint: the unique INDEX alone must enforce it.
        unique_constraints: vec![],
        check_constraints: vec![],
    }])
    .unwrap();
    let db = Database::create(&dir, schema).unwrap();

    let mut txn = db.begin().unwrap();
    txn.insert("u", row(&[("id", json!(1)), ("email", json!("a@x.com"))]))
        .unwrap();
    // A second row with the same indexed email is rejected as a duplicate.
    let err = txn
        .insert("u", row(&[("id", json!(2)), ("email", json!("a@x.com"))]))
        .unwrap_err();
    assert!(matches!(err, KitError::Duplicate(_)), "got {err:?}");
    // A distinct email is still accepted.
    txn.insert("u", row(&[("id", json!(3)), ("email", json!("b@x.com"))]))
        .unwrap();
    txn.commit().unwrap();
}

#[test]
fn non_unique_index_does_not_enforce_uniqueness() {
    let dir = temp_dir();
    let schema = Schema::new(vec![Table {
        id: 1,
        name: "u".into(),
        columns: vec![
            col(1, "id", ColumnType::Int64),
            col(2, "email", ColumnType::Text),
        ],
        primary_key: vec!["id".into()],
        indexes: vec![Index {
            name: "idx_email".into(),
            columns: vec!["email".into()],
            unique: false,
            kind: Default::default(),
        }],
        foreign_keys: vec![],
        unique_constraints: vec![],
        check_constraints: vec![],
    }])
    .unwrap();
    let db = Database::create(&dir, schema).unwrap();

    let mut txn = db.begin().unwrap();
    txn.insert("u", row(&[("id", json!(1)), ("email", json!("a@x.com"))]))
        .unwrap();
    // A non-unique index does not constrain duplicates.
    txn.insert("u", row(&[("id", json!(2)), ("email", json!("a@x.com"))]))
        .unwrap();
    txn.commit().unwrap();
}

// ── auto-assigned primary keys never collide and skip the duplicate check ──────

#[test]
fn auto_increment_pks_do_not_collide() {
    let dir = temp_dir();
    let id = Column {
        default: Some(DefaultKind::Sequence("k_seq".into())),
        ..col(1, "id", ColumnType::Int64)
    };
    let schema = Schema::new(vec![Table {
        id: 1,
        name: "k".into(),
        columns: vec![id, col(2, "v", ColumnType::Text)],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![],
        unique_constraints: vec![],
        check_constraints: vec![],
    }])
    .unwrap();
    let db = Database::create(&dir, schema).unwrap();

    // Inserts that omit the PK get distinct, auto-assigned ids with no
    // duplicate-PK error — an auto PK is guaranteed unique, so it is never
    // checked or guarded.
    let mut txn = db.begin().unwrap();
    let a = txn.insert("k", row(&[("v", json!("a"))])).unwrap();
    let b = txn.insert("k", row(&[("v", json!("b"))])).unwrap();
    let c = txn.insert("k", row(&[("v", json!("c"))])).unwrap();
    txn.commit().unwrap();

    assert_eq!(a.values.get("id"), Some(&json!(1)));
    assert_eq!(b.values.get("id"), Some(&json!(2)));
    assert_eq!(c.values.get("id"), Some(&json!(3)));

    let txn = db.begin().unwrap();
    let rows = txn
        .select(&select_filter(
            "k",
            Expr::IsNotNull(Box::new(Expr::Column("id".into()))),
        ))
        .unwrap();
    assert_eq!(rows.len(), 3);
}

// ── batch insert (insert_many) inserts a batch in one transaction ─────────────

#[test]
fn insert_many_inserts_a_batch_in_one_transaction() {
    let dir = temp_dir();
    let schema = Schema::new(vec![Table {
        id: 1,
        name: "k".into(),
        columns: vec![
            col(1, "id", ColumnType::Int64),
            col(2, "code", ColumnType::Text),
        ],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![],
        unique_constraints: vec![UniqueConstraint {
            name: "uq_code".into(),
            columns: vec!["code".into()],
        }],
        check_constraints: vec![],
    }])
    .unwrap();
    let db = Database::create(&dir, schema).unwrap();

    // A batch of explicit-PK rows is staged and committed in a single transaction.
    let mut txn = db.begin().unwrap();
    let inserted = txn
        .insert_many(
            "k",
            vec![
                row(&[("id", json!(1)), ("code", json!("a"))]),
                row(&[("id", json!(2)), ("code", json!("b"))]),
                row(&[("id", json!(3)), ("code", json!("c"))]),
            ],
        )
        .unwrap();
    assert_eq!(inserted.len(), 3);
    assert_eq!(inserted[2].values.get("code"), Some(&json!("c")));
    txn.commit().unwrap();

    let count = |db: &Database| {
        let txn = db.begin().unwrap();
        txn.select(&select_filter(
            "k",
            Expr::IsNotNull(Box::new(Expr::Column("id".into()))),
        ))
        .unwrap()
        .len()
    };
    assert_eq!(count(&db), 3);

    // A duplicate PK colliding with a committed row rolls the whole batch back.
    let mut txn = db.begin().unwrap();
    let err = txn
        .insert_many(
            "k",
            vec![
                row(&[("id", json!(4)), ("code", json!("d"))]),
                row(&[("id", json!(1)), ("code", json!("e"))]),
            ],
        )
        .unwrap_err();
    assert!(matches!(err, KitError::Duplicate(_)), "got {err:?}");
    txn.rollback();
    assert_eq!(count(&db), 3);

    // A duplicate PK appearing only within the batch is rejected via the
    // in-memory pk-seen set.
    let mut txn = db.begin().unwrap();
    let err = txn
        .insert_many(
            "k",
            vec![
                row(&[("id", json!(5)), ("code", json!("f"))]),
                row(&[("id", json!(5)), ("code", json!("g"))]),
            ],
        )
        .unwrap_err();
    assert!(matches!(err, KitError::Duplicate(_)), "got {err:?}");
    txn.rollback();
    assert_eq!(count(&db), 3);

    // A duplicate unique value inside the batch is rejected too.
    let mut txn = db.begin().unwrap();
    let err = txn
        .insert_many(
            "k",
            vec![
                row(&[("id", json!(6)), ("code", json!("h"))]),
                row(&[("id", json!(7)), ("code", json!("a"))]),
            ],
        )
        .unwrap_err();
    assert!(matches!(err, KitError::Duplicate(_)), "got {err:?}");
    txn.rollback();
    assert_eq!(count(&db), 3);
}

#[test]
fn insert_many_assigns_sequence_pks() {
    let dir = temp_dir();
    let id = Column {
        default: Some(DefaultKind::Sequence("k_seq".into())),
        ..col(1, "id", ColumnType::Int64)
    };
    let schema = Schema::new(vec![Table {
        id: 1,
        name: "k".into(),
        columns: vec![id, col(2, "v", ColumnType::Text)],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![],
        unique_constraints: vec![],
        check_constraints: vec![],
    }])
    .unwrap();
    let db = Database::create(&dir, schema).unwrap();

    let mut txn = db.begin().unwrap();
    let inserted = txn
        .insert_many(
            "k",
            vec![
                row(&[("v", json!("a"))]),
                row(&[("v", json!("b"))]),
                row(&[("v", json!("c"))]),
            ],
        )
        .unwrap();
    txn.commit().unwrap();
    let ids: Vec<_> = inserted
        .iter()
        .map(|r| r.values.get("id").cloned())
        .collect();
    assert_eq!(ids, vec![Some(json!(1)), Some(json!(2)), Some(json!(3))]);
}

// ── cascade delete reuses rows scanned during planning (no per-row re-read) ────

#[test]
fn cascade_delete_removes_all_children() {
    let dir = temp_dir();
    let parent = Table {
        id: 1,
        name: "parent".into(),
        columns: vec![col(1, "id", ColumnType::Int64)],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![],
        unique_constraints: vec![],
        check_constraints: vec![],
    };
    let child = Table {
        id: 2,
        name: "child".into(),
        columns: vec![
            col(1, "id", ColumnType::Int64),
            col(2, "parent_id", ColumnType::Int64),
        ],
        primary_key: vec!["id".into()],
        indexes: vec![],
        foreign_keys: vec![ForeignKey {
            name: "fk_child_parent".into(),
            columns: vec!["parent_id".into()],
            references_table: "parent".into(),
            references_columns: vec!["id".into()],
            on_delete: ForeignKeyAction::Cascade,
        }],
        unique_constraints: vec![],
        check_constraints: vec![],
    };
    let db = Database::create(&dir, Schema::new(vec![parent, child]).unwrap()).unwrap();

    let mut txn = db.begin().unwrap();
    txn.insert("parent", row(&[("id", json!(1))])).unwrap();
    for i in 1..=5 {
        txn.insert("child", row(&[("id", json!(i)), ("parent_id", json!(1))]))
            .unwrap();
    }
    txn.commit().unwrap();

    // Deleting the parent cascades to every child, reusing the child rows scanned
    // while planning rather than re-reading each child by PK.
    let mut txn = db.begin().unwrap();
    txn.delete("parent", &json!(1)).unwrap();
    txn.commit().unwrap();

    let txn = db.begin().unwrap();
    let children = txn
        .select(&select_filter(
            "child",
            Expr::IsNotNull(Box::new(Expr::Column("id".into()))),
        ))
        .unwrap();
    assert!(
        children.is_empty(),
        "expected all children cascaded, got {children:?}"
    );
    assert!(txn.get_by_pk("parent", &json!(1)).unwrap().is_none());
}