nodedb 0.2.0

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
// SPDX-License-Identifier: BUSL-1.1

//! Integration tests for SQL transaction behavior.

mod common;

use common::pgwire_harness::TestServer;

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn commit_persists_buffered_writes() {
    let server = TestServer::start().await;

    server
        .exec("CREATE COLLECTION txn_test (id TEXT PRIMARY KEY, val INT) WITH (engine='document_strict')")
        .await
        .unwrap();

    server.exec("BEGIN").await.unwrap();
    server
        .exec("INSERT INTO txn_test (id, val) VALUES ('t1', 10)")
        .await
        .unwrap();
    server
        .exec("INSERT INTO txn_test (id, val) VALUES ('t2', 20)")
        .await
        .unwrap();
    server.exec("COMMIT").await.unwrap();

    let rows = server
        .query_text("SELECT id FROM txn_test WHERE id = 't1'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn rollback_discards_buffered_write_and_missing_row_is_empty() {
    let server = TestServer::start().await;

    server
        .exec("CREATE COLLECTION txn_test (id TEXT PRIMARY KEY, val INT) WITH (engine='document_strict')")
        .await
        .unwrap();

    server.exec("BEGIN").await.unwrap();
    server
        .exec("INSERT INTO txn_test (id, val) VALUES ('t3', 30)")
        .await
        .unwrap();
    server.exec("ROLLBACK").await.unwrap();

    let rows = server
        .query_text("SELECT id FROM txn_test WHERE id = 't3'")
        .await
        .unwrap();
    assert!(rows.is_empty(), "rolled-back row should not be visible");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn alter_table_add_column_refreshes_strict_schema() {
    let server = TestServer::start().await;

    server
        .exec("CREATE COLLECTION alter_test (id TEXT PRIMARY KEY, name TEXT) WITH (engine='document_strict')")
        .await
        .unwrap();
    server
        .exec("INSERT INTO alter_test (id, name) VALUES ('a1', 'Alice')")
        .await
        .unwrap();

    server
        .exec("ALTER TABLE alter_test ADD COLUMN score INT DEFAULT 0")
        .await
        .unwrap();
    server
        .exec("INSERT INTO alter_test (id, name, score) VALUES ('a3', 'New', 100)")
        .await
        .unwrap();

    let rows = server
        .query_text("SELECT id FROM alter_test WHERE id = 'a3'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(
        rows[0].contains("a3"),
        "expected row to include inserted id"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn alter_collection_add_column_refreshes_strict_schema() {
    let server = TestServer::start().await;

    server
        .exec(
            "CREATE COLLECTION memories (\
                id TEXT PRIMARY KEY, \
                name TEXT NOT NULL) WITH (engine='document_strict')",
        )
        .await
        .unwrap();
    server
        .exec("INSERT INTO memories (id, name) VALUES ('m1', 'first')")
        .await
        .unwrap();

    // `ALTER COLLECTION ... ADD COLUMN` must reach the catalog-generic
    // add-column handler — the same path exercised by `ALTER TABLE` above.
    server
        .exec("ALTER COLLECTION memories ADD COLUMN is_latest BOOL DEFAULT true")
        .await
        .unwrap();

    server
        .exec("INSERT INTO memories (id, name, is_latest) VALUES ('m2', 'second', false)")
        .await
        .unwrap();

    let rows = server
        .query_text("SELECT id FROM memories WHERE id = 'm2'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert!(rows[0].contains("m2"));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn alter_collection_drop_column() {
    let server = TestServer::start().await;

    server
        .exec(
            "CREATE COLLECTION memories (\
                id TEXT PRIMARY KEY, \
                name TEXT NOT NULL, \
                scratch TEXT) WITH (engine='document_strict')",
        )
        .await
        .unwrap();
    server
        .exec("INSERT INTO memories (id, name, scratch) VALUES ('m1', 'first', 'temp')")
        .await
        .unwrap();

    server
        .exec("ALTER COLLECTION memories DROP COLUMN scratch")
        .await
        .unwrap();

    // New inserts without the dropped column still succeed, and old data reads.
    server
        .exec("INSERT INTO memories (id, name) VALUES ('m2', 'second')")
        .await
        .unwrap();
    let rows = server
        .query_text("SELECT id FROM memories WHERE id = 'm2'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn alter_collection_rename_column() {
    let server = TestServer::start().await;

    server
        .exec(
            "CREATE COLLECTION memories (\
                id TEXT PRIMARY KEY, \
                name TEXT NOT NULL) WITH (engine='document_strict')",
        )
        .await
        .unwrap();
    server
        .exec("INSERT INTO memories (id, name) VALUES ('m1', 'first')")
        .await
        .unwrap();

    server
        .exec("ALTER COLLECTION memories RENAME COLUMN name TO title")
        .await
        .unwrap();

    let rows = server
        .query_text("SELECT title FROM memories WHERE id = 'm1'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(
        rows[0], "first",
        "expected renamed column 'title' = 'first', got {:?}",
        rows[0]
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn alter_collection_alter_column_type() {
    let server = TestServer::start().await;

    server
        .exec(
            "CREATE COLLECTION measurements (\
                id TEXT PRIMARY KEY, \
                value INT NOT NULL) WITH (engine='document_strict')",
        )
        .await
        .unwrap();
    server
        .exec("INSERT INTO measurements (id, value) VALUES ('m1', 42)")
        .await
        .unwrap();

    server
        .exec("ALTER COLLECTION measurements ALTER COLUMN value TYPE BIGINT")
        .await
        .unwrap();

    // Re-insert using the widened type.
    server
        .exec("INSERT INTO measurements (id, value) VALUES ('m2', 9999999999)")
        .await
        .unwrap();
    let rows = server
        .query_text("SELECT id FROM measurements WHERE id = 'm2'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
}

// ── Pre-ALTER row survival tests ──────────────────────────────────────
//
// Every test below verifies that rows written BEFORE a schema-altering DDL
// remain readable with correct values AFTER the DDL. The bug class is:
// catalog schema mutated without row migration or read-time compat shim.

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn add_column_preserves_pre_alter_row_existing_columns() {
    let server = TestServer::start().await;

    server
        .exec("CREATE COLLECTION ac_preserve (id TEXT PRIMARY KEY, name TEXT) WITH (engine='document_strict')")
        .await
        .unwrap();
    server
        .exec("INSERT INTO ac_preserve (id, name) VALUES ('a', 'alice')")
        .await
        .unwrap();

    server
        .exec("ALTER TABLE ac_preserve ADD COLUMN note TEXT DEFAULT 'n/a'")
        .await
        .unwrap();

    // Pre-ALTER row must still return correct values for original columns.
    let rows = server
        .query_rows("SELECT id, name FROM ac_preserve WHERE id = 'a'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1, "pre-ALTER row must be visible");
    // row[0]=id, row[1]=name
    assert_eq!(
        rows[0][1], "alice",
        "original column 'name' must retain its value, got {:?}",
        rows[0]
    );
    // Regression guard: must NOT return null.
    assert_ne!(
        rows[0][1], "null",
        "pre-ALTER row must not have null-everywhere corruption, got {:?}",
        rows[0]
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn add_column_returns_default_for_pre_alter_row() {
    let server = TestServer::start().await;

    server
        .exec("CREATE COLLECTION ac_default (id TEXT PRIMARY KEY, name TEXT) WITH (engine='document_strict')")
        .await
        .unwrap();
    server
        .exec("INSERT INTO ac_default (id, name) VALUES ('a', 'alice')")
        .await
        .unwrap();

    server
        .exec("ALTER TABLE ac_default ADD COLUMN note TEXT DEFAULT 'n/a'")
        .await
        .unwrap();

    // The new column should virtual-fill with its DEFAULT for pre-ALTER rows.
    let rows = server
        .query_rows("SELECT id, name, note FROM ac_default WHERE id = 'a'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1, "pre-ALTER row must be visible");
    // row[0]=id, row[1]=name, row[2]=note
    assert_eq!(
        rows[0][2], "n/a",
        "new column must return DEFAULT value for pre-ALTER rows, got {:?}",
        rows[0]
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn add_column_then_update_pre_alter_row() {
    let server = TestServer::start().await;

    server
        .exec("CREATE COLLECTION ac_update (id TEXT PRIMARY KEY, name TEXT) WITH (engine='document_strict')")
        .await
        .unwrap();
    server
        .exec("INSERT INTO ac_update (id, name) VALUES ('a', 'alice')")
        .await
        .unwrap();

    server
        .exec("ALTER TABLE ac_update ADD COLUMN note TEXT DEFAULT 'n/a'")
        .await
        .unwrap();

    // Updating a pre-ALTER row must succeed and preserve all columns.
    server
        .exec("UPDATE ac_update SET note = 'updated' WHERE id = 'a'")
        .await
        .unwrap();

    let rows = server
        .query_rows("SELECT id, name, note FROM ac_update WHERE id = 'a'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // row[0]=id, row[1]=name, row[2]=note
    assert_eq!(
        rows[0][1], "alice",
        "original column must survive update, got {:?}",
        rows[0]
    );
    assert_eq!(
        rows[0][2], "updated",
        "updated column must reflect new value, got {:?}",
        rows[0]
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn multiple_add_columns_preserves_pre_alter_row() {
    let server = TestServer::start().await;

    server
        .exec("CREATE COLLECTION ac_multi (id TEXT PRIMARY KEY, name TEXT) WITH (engine='document_strict')")
        .await
        .unwrap();
    server
        .exec("INSERT INTO ac_multi (id, name) VALUES ('a', 'alice')")
        .await
        .unwrap();

    server
        .exec("ALTER TABLE ac_multi ADD COLUMN col1 INT DEFAULT 0")
        .await
        .unwrap();
    server
        .exec("ALTER TABLE ac_multi ADD COLUMN col2 TEXT DEFAULT 'x'")
        .await
        .unwrap();

    // Two sequential ADD COLUMNs compound the schema drift — pre-ALTER row
    // must still be readable with correct values and defaults.
    let rows = server
        .query_rows("SELECT id, name, col1, col2 FROM ac_multi WHERE id = 'a'")
        .await
        .unwrap();
    assert_eq!(
        rows.len(),
        1,
        "pre-ALTER row must be visible after two ADD COLUMNs"
    );
    // row[0]=id, row[1]=name, row[2]=col1, row[3]=col2
    assert_eq!(
        rows[0][1], "alice",
        "original column must retain value, got {:?}",
        rows[0]
    );
    // Regression guard: null-everywhere means total schema-data offset corruption.
    assert_ne!(
        rows[0][1], "null",
        "must not exhibit null-everywhere corruption, got {:?}",
        rows[0]
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn drop_column_preserves_pre_alter_row_remaining_columns() {
    let server = TestServer::start().await;

    server
        .exec(
            "CREATE COLLECTION dc_preserve (\
                id TEXT PRIMARY KEY, \
                name TEXT NOT NULL, \
                scratch TEXT) WITH (engine='document_strict')",
        )
        .await
        .unwrap();
    server
        .exec("INSERT INTO dc_preserve (id, name, scratch) VALUES ('a', 'alice', 'temp')")
        .await
        .unwrap();

    server
        .exec("ALTER COLLECTION dc_preserve DROP COLUMN scratch")
        .await
        .unwrap();

    // Remaining columns of the pre-ALTER row must read correctly.
    let rows = server
        .query_rows("SELECT id, name FROM dc_preserve WHERE id = 'a'")
        .await
        .unwrap();
    assert_eq!(
        rows.len(),
        1,
        "pre-ALTER row must be visible after DROP COLUMN"
    );
    // row[0]=id, row[1]=name
    assert_eq!(
        rows[0][1], "alice",
        "remaining column 'name' must retain its value after DROP COLUMN, got {:?}",
        rows[0]
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn rename_column_preserves_pre_alter_row_value() {
    let server = TestServer::start().await;

    server
        .exec(
            "CREATE COLLECTION rc_preserve (\
                id TEXT PRIMARY KEY, \
                name TEXT NOT NULL, \
                score INT DEFAULT 0) WITH (engine='document_strict')",
        )
        .await
        .unwrap();
    server
        .exec("INSERT INTO rc_preserve (id, name, score) VALUES ('a', 'alice', 42)")
        .await
        .unwrap();

    server
        .exec("ALTER COLLECTION rc_preserve RENAME COLUMN score TO points")
        .await
        .unwrap();

    // Pre-ALTER row must be readable under the new column name with correct value.
    let rows = server
        .query_rows("SELECT id, name, points FROM rc_preserve WHERE id = 'a'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // row[0]=id, row[1]=name, row[2]=points
    assert_eq!(
        rows[0][2], "42",
        "renamed column must retain pre-ALTER value, got {:?}",
        rows[0]
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn alter_column_type_preserves_pre_alter_row_value() {
    let server = TestServer::start().await;

    server
        .exec(
            "CREATE COLLECTION at_preserve (\
                id TEXT PRIMARY KEY, \
                value INT NOT NULL) WITH (engine='document_strict')",
        )
        .await
        .unwrap();
    server
        .exec("INSERT INTO at_preserve (id, value) VALUES ('a', 42)")
        .await
        .unwrap();

    server
        .exec("ALTER COLLECTION at_preserve ALTER COLUMN value TYPE BIGINT")
        .await
        .unwrap();

    // Pre-ALTER row must still read correctly after type widening.
    let rows = server
        .query_rows("SELECT id, value FROM at_preserve WHERE id = 'a'")
        .await
        .unwrap();
    assert_eq!(rows.len(), 1);
    // row[0]=id, row[1]=value
    assert_eq!(
        rows[0][1], "42",
        "value must survive ALTER COLUMN TYPE, got {:?}",
        rows[0]
    );
}