lix 0.18.0

Embeddable version control for apps and AI agents.
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
use lix::Value;
use serde_json::json;

simulation_test!(
    transaction_replacement_preserves_sql_null,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(engine.open_session().await.unwrap(), &engine);
        session
        .execute(
            "INSERT INTO lix_key_value (key, value) VALUES ('prepared-null-a', 'seed'), ('prepared-null-b', 'seed')",
            &[],
        )
        .await
        .unwrap();

        let mut transaction = session.begin_transaction().await.unwrap();
        let sql = "UPDATE lix_key_value SET value = CAST($1 AS JSONB) WHERE key = $2";
        transaction
            .execute(sql, &[Value::Null, Value::Text("prepared-null-a".into())])
            .await
            .unwrap();
        transaction
            .execute(sql, &[Value::Null, Value::Text("prepared-null-b".into())])
            .await
            .unwrap();
        transaction.commit().await.unwrap();

        let result = session
        .execute(
            "SELECT value, value IS NULL FROM lix_key_value WHERE key LIKE 'prepared-null-%' ORDER BY key",
            &[],
        )
        .await
        .unwrap();
        assert_eq!(
            result
                .rows()
                .iter()
                .map(|row| row.values().to_vec())
                .collect::<Vec<_>>(),
            vec![
                vec![Value::Null, Value::Boolean(true)],
                vec![Value::Null, Value::Boolean(true)],
            ]
        );
    }
);

simulation_test!(
    exact_key_update_distinguishes_json_null_from_sql_null,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(engine.open_session().await.unwrap(), &engine);
        session
            .execute(
                "INSERT INTO lix_key_value (key, value) VALUES ('specialized-null', 'seed')",
                &[],
            )
            .await
            .unwrap();

        session
            .execute(
                "UPDATE lix_key_value SET value = CAST($1 AS JSONB) WHERE key = $2",
                &[Value::Null, Value::Text("specialized-null".into())],
            )
            .await
            .unwrap();
        let sql_null = session
            .execute(
                "SELECT value, value IS NULL FROM lix_key_value WHERE key = 'specialized-null'",
                &[],
            )
            .await
            .unwrap();
        assert_eq!(
            sql_null.rows()[0].values(),
            &[Value::Null, Value::Boolean(true)]
        );

        session
            .execute(
                "UPDATE lix_key_value SET value = CAST($1 AS JSONB) WHERE key = $2",
                &[
                    Value::Text("null".into()),
                    Value::Text("specialized-null".into()),
                ],
            )
            .await
            .unwrap();
        let json_null = session
            .execute(
                "SELECT value, value IS NULL FROM lix_key_value WHERE key = 'specialized-null'",
                &[],
            )
            .await
            .unwrap();
        assert_eq!(
            json_null.rows()[0].values(),
            &[Value::Jsonb(json!(null).into()), Value::Boolean(false)]
        );
    }
);

simulation_test!(
    certified_path_replacement_rejects_sql_null_and_keeps_json_null,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(engine.open_session().await.unwrap(), &engine);
        let schema = json!({"$schema":"https://lix.dev/schema-v1.json","key":"certified_null_probe",
        "columns":[{"name":"path","type":"text","nullable":false},{"name":"value","type":"jsonb","nullable":false}],"primary_key":["path"]});
        session
            .execute(
                "INSERT INTO lix_registered_schema (value) VALUES ($1)",
                &[Value::Jsonb(schema.into())],
            )
            .await
            .unwrap();
        session.execute("INSERT INTO certified_null_probe (path,value) VALUES ('/a','{}'::jsonb),('/b','{}'::jsonb)",&[]).await.unwrap();
        let mut transaction = session.begin_transaction().await.unwrap();
        let sql = "UPDATE certified_null_probe SET value=CAST($1 AS JSONB) WHERE path=$2";
        crate::sql2::take_certified_single_path_value_replacements();
        for path in ["/a", "/b", "/a"] {
            transaction
                .execute(sql, &[Value::Text("null".into()), Value::Text(path.into())])
                .await
                .unwrap();
        }
        assert!(
            crate::sql2::take_certified_single_path_value_replacements() > 0,
            "exercise the certified path"
        );
        for path in ["/a", "/b"] {
            assert!(
                transaction
                    .execute(sql, &[Value::Null, Value::Text(path.into())])
                    .await
                    .is_err()
            );
        }
        assert!(
            transaction
                .execute(
                    "UPDATE certified_null_probe SET value=CAST($1 AS JSONB) WHERE path LIKE '/%'",
                    &[Value::Null]
                )
                .await
                .is_err()
        );
        transaction.commit().await.unwrap();
        let result = session
            .execute(
                "SELECT value,value IS NULL FROM certified_null_probe ORDER BY path",
                &[],
            )
            .await
            .unwrap();
        for row in result.rows() {
            assert_eq!(
                row.values(),
                &[Value::Jsonb(json!(null).into()), Value::Boolean(false)]
            );
        }
    }
);

simulation_test!(
    certified_path_replacement_accepts_typed_jsonb_values,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(engine.open_session().await.unwrap(), &engine);
        let schema = json!({"$schema":"https://lix.dev/schema-v1.json","key":"certified_typed_jsonb_probe",
        "columns":[{"name":"path","type":"text","nullable":false},{"name":"value","type":"jsonb","nullable":false}],"primary_key":["path"]});
        session
            .execute(
                "INSERT INTO lix_registered_schema (value) VALUES ($1)",
                &[Value::Jsonb(schema.into())],
            )
            .await
            .unwrap();
        session
            .execute(
                "INSERT INTO certified_typed_jsonb_probe (path,value) VALUES ('/single-null','{}'::jsonb),('/single-string','{}'::jsonb),('/single-number','{}'::jsonb),('/single-object','{}'::jsonb),('/scalar-integer','{}'::jsonb),('/scalar-boolean','{}'::jsonb),('/scalar-real','{}'::jsonb),('/scalar-blob','{}'::jsonb),('/batch-null','{}'::jsonb),('/batch-string','{}'::jsonb),('/batch-number','{}'::jsonb),('/batch-object','{}'::jsonb)",
                &[],
            )
            .await
            .unwrap();

        let sql = "UPDATE certified_typed_jsonb_probe SET value=CAST($1 AS JSONB) WHERE path=$2";
        let single_values = vec![
            ("/single-null", json!(null)),
            ("/single-string", json!("typed-string")),
            ("/single-number", json!(1.0)),
            ("/single-object", json!({"kind":"typed-object"})),
        ];
        crate::sql2::take_certified_single_path_value_replacements();
        let mut transaction = session.begin_transaction().await.unwrap();
        for (path, value) in &single_values {
            transaction
                .execute(
                    sql,
                    &[
                        Value::Jsonb(value.clone().into()),
                        Value::Text((*path).into()),
                    ],
                )
                .await
                .unwrap();
        }
        let scalar_single_values = vec![
            ("/scalar-integer", Value::Integer(7)),
            ("/scalar-boolean", Value::Boolean(true)),
            ("/scalar-real", Value::Real(1.5)),
            ("/scalar-blob", Value::Blob(b"\"binary\"".to_vec().into())),
        ];
        for (path, value) in &scalar_single_values {
            transaction
                .execute(sql, &[value.clone(), Value::Text((*path).into())])
                .await
                .unwrap();
        }
        transaction.commit().await.unwrap();
        assert!(
            crate::sql2::take_certified_single_path_value_replacements()
                >= single_values.len() + scalar_single_values.len(),
            "typed JSONB prepared values should use the certified path replacement"
        );

        let batch_values = vec![
            ("/batch-null", json!(null)),
            ("/batch-string", json!("typed-string")),
            ("/batch-number", json!(1.0)),
            ("/batch-object", json!({"kind":"typed-object"})),
        ];
        let expected_values = single_values
            .iter()
            .chain(&batch_values)
            .map(|(_, value)| value.clone())
            .collect::<Vec<_>>();
        let statements = batch_values
            .iter()
            .map(|(path, value)| lix::ExecuteBatchStatement {
                sql: sql.into(),
                params: vec![
                    Value::Jsonb(value.clone().into()),
                    Value::Text((*path).into()),
                ],
                label: None,
            })
            .collect::<Vec<_>>();
        session.execute_batch(&statements).await.unwrap();

        for ((path, _), expected) in single_values
            .into_iter()
            .chain(batch_values)
            .zip(expected_values)
        {
            let expected = session
                .execute("SELECT CAST($1 AS JSONB)", &[Value::Jsonb(expected.into())])
                .await
                .unwrap()
                .rows()[0]
                .values()[0]
                .clone();
            let result = session
                .execute(
                    "SELECT value FROM certified_typed_jsonb_probe WHERE path=$1",
                    &[Value::Text(path.into())],
                )
                .await
                .unwrap();
            assert_eq!(
                result.rows()[0].values(),
                &[expected],
                "typed JSONB value for {path} was not preserved"
            );
        }
        for (path, value) in scalar_single_values {
            let expected = session
                .execute("SELECT CAST($1 AS JSONB)", &[value])
                .await
                .unwrap()
                .rows()[0]
                .values()[0]
                .clone();
            let result = session
                .execute(
                    "SELECT value FROM certified_typed_jsonb_probe WHERE path=$1",
                    &[Value::Text(path.into())],
                )
                .await
                .unwrap();
            assert_eq!(result.rows()[0].values(), &[expected]);
        }
    }
);

simulation_test!(
    certified_path_execute_batch_rolls_back_sql_null_for_required_value,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(engine.open_session().await.unwrap(), &engine);
        let schema = json!({"$schema":"https://lix.dev/schema-v1.json","key":"certified_required_batch_probe",
        "columns":[{"name":"path","type":"text","nullable":false},{"name":"value","type":"jsonb","nullable":false}],"primary_key":["path"]});
        session
            .execute(
                "INSERT INTO lix_registered_schema (value) VALUES ($1)",
                &[Value::Jsonb(schema.into())],
            )
            .await
            .unwrap();
        session
            .execute(
                "INSERT INTO certified_required_batch_probe (path,value) VALUES ('/first','\"seed\"'::jsonb),('/second','\"seed\"'::jsonb)",
                &[],
            )
            .await
            .unwrap();

        let sql = "UPDATE certified_required_batch_probe SET value=CAST($1 AS JSONB) WHERE path=$2";
        assert!(
            session
                .execute_batch(&[
                    lix::ExecuteBatchStatement {
                        sql: sql.into(),
                        params: vec![
                            Value::Jsonb(json!({"written":true}).into()),
                            Value::Text("/first".into()),
                        ],
                        label: Some("valid-first".into()),
                    },
                    lix::ExecuteBatchStatement {
                        sql: sql.into(),
                        params: vec![Value::Null, Value::Text("/second".into())],
                        label: Some("invalid-null".into()),
                    },
                ])
                .await
                .is_err(),
            "SQL NULL must violate the required certified value column"
        );

        let result = session
            .execute(
                "SELECT path,value FROM certified_required_batch_probe ORDER BY path",
                &[],
            )
            .await
            .unwrap();
        assert_eq!(
            result
                .rows()
                .iter()
                .map(|row| row.values().to_vec())
                .collect::<Vec<_>>(),
            vec![
                vec![
                    Value::Text("/first".into()),
                    Value::Jsonb(json!("seed").into()),
                ],
                vec![
                    Value::Text("/second".into()),
                    Value::Jsonb(json!("seed").into()),
                ],
            ]
        );
    }
);

simulation_test!(
    generic_nullable_path_execute_batch_preserves_mixed_null_provenance,
    |sim| async move {
        let engine = sim.boot_engine().await;
        let session = sim.wrap_session(engine.open_session().await.unwrap(), &engine);
        let schema = json!({"$schema":"https://lix.dev/schema-v1.json","key":"generic_nullable_batch_probe",
        "columns":[{"name":"path","type":"text","nullable":false},{"name":"value","type":"jsonb","nullable":true}],"primary_key":["path"]});
        session
            .execute(
                "INSERT INTO lix_registered_schema (value) VALUES ($1)",
                &[Value::Jsonb(schema.into())],
            )
            .await
            .unwrap();
        session
            .execute(
                "INSERT INTO generic_nullable_batch_probe (path,value) VALUES ('/sql-null','{}'::jsonb),('/json-null','{}'::jsonb)",
                &[],
            )
            .await
            .unwrap();

        let sql = "UPDATE generic_nullable_batch_probe SET value=CAST($1 AS JSONB) WHERE path=$2";
        session
            .execute_batch(&[
                lix::ExecuteBatchStatement {
                    sql: sql.into(),
                    params: vec![Value::Null, Value::Text("/sql-null".into())],
                    label: None,
                },
                lix::ExecuteBatchStatement {
                    sql: sql.into(),
                    params: vec![
                        Value::Jsonb(json!(null).into()),
                        Value::Text("/json-null".into()),
                    ],
                    label: None,
                },
            ])
            .await
            .unwrap();

        let result = session
            .execute(
                "SELECT path,value,value IS NULL FROM generic_nullable_batch_probe ORDER BY path",
                &[],
            )
            .await
            .unwrap();
        assert_eq!(
            result
                .rows()
                .iter()
                .map(|row| row.values().to_vec())
                .collect::<Vec<_>>(),
            vec![
                vec![
                    Value::Text("/json-null".into()),
                    Value::Jsonb(json!(null).into()),
                    Value::Boolean(false),
                ],
                vec![
                    Value::Text("/sql-null".into()),
                    Value::Null,
                    Value::Boolean(true),
                ],
            ]
        );
    }
);