lix 0.12.2

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
use datafusion::sql::sqlparser::ast::ObjectName;

use crate::LixError;

use super::super::catalog::{PublicCatalog, PublicSurfaceContract};
use super::expr::BoundColumnRef;
use super::write::BoundWriteOp;

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct BoundTable {
    pub(crate) name: String,
    pub(crate) surface: PublicSurfaceContract,
}

pub(crate) fn bind_exact_table_name(name: &ObjectName) -> Result<String, LixError> {
    if name.0.len() != 1 {
        return Err(super::error::unsupported(
            "qualified SQL table names are not supported",
        ));
    }
    name.0
        .first()
        .and_then(|part| part.as_ident())
        .map(|ident| {
            if ident.quote_style.is_some() {
                ident.value.clone()
            } else {
                ident.value.to_ascii_lowercase()
            }
        })
        .ok_or_else(|| super::error::unsupported("unsupported SQL table name"))
}

pub(crate) fn bind_public_table(
    catalog: &PublicCatalog,
    name: &ObjectName,
) -> Result<BoundTable, LixError> {
    let table_name = bind_exact_table_name(name)?;
    let surface = catalog.require_surface(&table_name)?.clone();
    Ok(BoundTable {
        name: table_name,
        surface,
    })
}

pub(crate) fn require_public_column<'a>(
    table: &'a BoundTable,
    column_name: &str,
) -> Result<&'a super::super::catalog::PublicColumn, LixError> {
    if table.surface.public_column(column_name).is_some() {
        return Ok(table
            .surface
            .public_column(column_name)
            .expect("checked public column"));
    }
    if table.surface.column(column_name).is_some() {
        return Err(LixError::new(
            LixError::CODE_COLUMN_NOT_FOUND,
            format!(
                "column '{column_name}' is not part of public SQL surface '{}'",
                table.name
            ),
        ));
    }
    Err(LixError::new(
        LixError::CODE_COLUMN_NOT_FOUND,
        format!(
            "column '{column_name}' does not exist on SQL table '{}'",
            table.name
        ),
    ))
}

pub(crate) fn require_writable_column(
    table: &BoundTable,
    column_name: &str,
    op: BoundWriteOp,
) -> Result<BoundColumnRef, LixError> {
    let column = require_public_column(table, column_name)?;
    let allowed = match op {
        BoundWriteOp::Insert => column.is_insertable(),
        BoundWriteOp::Update => column.is_updatable(),
        BoundWriteOp::Delete => false,
    };
    if !allowed {
        if table.name == "lix_branch" && column_name == "id" && op == BoundWriteOp::Update {
            return Err(super::error::unsupported(
                "UPDATE lix_branch cannot change immutable column 'id'",
            ));
        }
        return Err(super::error::unsupported(format!(
            "column '{column_name}' is not writable on SQL table '{}'",
            table.name
        )));
    }
    Ok(BoundColumnRef {
        table: table.name.clone(),
        column_id: column.id,
        name: column.name.clone(),
    })
}

pub(crate) fn bind_public_column_ref(
    table: &BoundTable,
    column_name: &str,
) -> Result<BoundColumnRef, LixError> {
    let column = require_public_column(table, column_name)?;
    Ok(BoundColumnRef {
        table: table.name.clone(),
        column_id: column.id,
        name: column.name.clone(),
    })
}

#[cfg(test)]
mod tests {
    use datafusion::sql::sqlparser::ast::{SetExpr, Statement, TableFactor};
    use datafusion::sql::sqlparser::parser::Parser;
    use serde_json::json;

    use super::*;
    use crate::sql2::catalog::PublicSurfaceKind;

    #[test]
    fn rejects_qualified_table_name_even_when_leaf_exists() {
        let catalog = catalog();
        let error = bind_public_table(&catalog, &table_name("SELECT * FROM foo.unknown"))
            .expect_err("qualified table should be rejected");

        assert_eq!(error.code, LixError::CODE_UNSUPPORTED_SQL);
    }

    #[test]
    fn rejects_unknown_table_name() {
        let catalog = catalog();
        let error = bind_public_table(&catalog, &table_name("SELECT * FROM missing"))
            .expect_err("unknown table should be rejected");

        assert_eq!(error.code, LixError::CODE_UNSUPPORTED_SQL);
        assert!(error.message.contains("unknown SQL table 'missing'"));
    }

    #[test]
    fn base_row_table_does_not_expose_branch_column() {
        let catalog = catalog();
        let table = bind_public_table(&catalog, &table_name("SELECT * FROM test_state_schema"))
            .expect("base row table should bind");

        assert!(matches!(
            table.surface.kind,
            PublicSurfaceKind::SchemaBase { .. }
        ));
        assert!(require_public_column(&table, "name").is_ok());
        let error = require_public_column(&table, "lixcol_branch_id")
            .expect_err("base schema surface should not expose branch column");
        assert!(error.message.contains("does not exist"));
    }

    #[test]
    fn by_branch_row_exposes_lixcol_branch_id_without_branch_id_alias() {
        let catalog = catalog();
        let table = bind_public_table(
            &catalog,
            &table_name("SELECT * FROM test_state_schema_by_branch"),
        )
        .expect("by-branch row table should bind");

        assert!(matches!(
            table.surface.kind,
            PublicSurfaceKind::SchemaByBranch { .. }
        ));
        assert!(require_public_column(&table, "lixcol_branch_id").is_ok());
        let error = require_public_column(&table, "branch_id")
            .expect_err("by-branch schema surface should not alias branch_id");
        assert!(error.message.contains("does not exist"));
    }

    #[test]
    fn quoted_table_names_are_case_sensitive() {
        let catalog = catalog();

        bind_public_table(&catalog, &table_name("SELECT * FROM \"lix_file\""))
            .expect("quoted exact case should bind");
        let error = bind_public_table(&catalog, &table_name("SELECT * FROM \"LIX_FILE\""))
            .expect_err("quoted mixed case should not be folded");

        assert!(error.message.contains("unknown SQL table 'LIX_FILE'"));
    }

    #[test]
    fn hidden_columns_cannot_bind_as_public_columns() {
        let catalog = catalog();
        let table = bind_public_table(&catalog, &table_name("SELECT * FROM lix_file"))
            .expect("lix_file should bind");

        let error = require_public_column(&table, "lixcol_schema_key")
            .expect_err("hidden column should not bind");
        assert!(error.message.contains("not part of public SQL surface"));
    }

    #[test]
    fn catalog_rejects_runtime_schema_in_reserved_namespace_before_surface_collision() {
        let error = PublicCatalog::from_visible_schemas(&[json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "lix_file",
            "columns": [
                { "name": "id", "type": "text", "nullable": false },
            ],
            "primary_key": ["id"],
        })])
        .expect_err("the complete lix_* runtime namespace should be rejected");

        assert_eq!(error.code, LixError::CODE_RESERVED_SCHEMA_NAMESPACE);
        assert!(error.message.contains("lix_file"));
    }

    #[test]
    fn catalog_uses_validated_schema_surface_derivation() {
        let error = PublicCatalog::from_visible_schemas(&[json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "bad_row",
            "columns": [
                { "name": "value", "type": "jsonb", "nullable": false },
            ],
            "primary_key": ["value"],
        })])
        .expect_err("Schema v1 rejects unsupported primary-key types");
        assert_eq!(error.code, LixError::CODE_SCHEMA_DEFINITION);
    }

    #[test]
    fn fixed_catalog_exposes_only_the_deliberate_lix_sql_contract() {
        let actual = PublicCatalog::fixed_system()
            .surfaces()
            .map(|surface| surface.name.as_str())
            .collect::<Vec<_>>();
        let expected = vec![
            "lix_account",
            "lix_account_by_branch",
            "lix_account_history",
            "lix_apply",
            "lix_branch",
            "lix_branch_descriptor",
            "lix_branch_descriptor_by_branch",
            "lix_branch_descriptor_history",
            "lix_branch_ref",
            "lix_branch_ref_by_branch",
            "lix_branch_ref_history",
            "lix_change",
            "lix_checkpoint",
            "lix_checkpoint_history",
            "lix_commit",
            "lix_commit_by_branch",
            "lix_commit_edge",
            "lix_commit_edge_by_branch",
            "lix_create_checkpoint",
            "lix_directory",
            "lix_directory_by_branch",
            "lix_directory_history",
            "lix_directory_working_diff",
            "lix_directory_working_diff_by_branch",
            "lix_file",
            "lix_file_by_branch",
            "lix_file_history",
            "lix_file_working_diff",
            "lix_file_working_diff_by_branch",
            "lix_key_value",
            "lix_key_value_by_branch",
            "lix_key_value_history",
            "lix_registered_schema",
            "lix_registered_schema_by_branch",
            "lix_registered_schema_history",
            "lix_revert",
            "lix_working_diff",
            "lix_working_diff_by_branch",
        ];

        assert_eq!(actual, expected);
    }

    #[test]
    fn fixed_catalog_keeps_registry_surfaces_and_hides_storage_adapters() {
        let catalog = PublicCatalog::fixed_system();
        for surface_name in [
            "lix_key_value",
            "lix_key_value_by_branch",
            "lix_key_value_history",
            "lix_registered_schema",
            "lix_registered_schema_by_branch",
            "lix_registered_schema_history",
            "lix_checkpoint",
            "lix_checkpoint_history",
            "lix_working_diff",
            "lix_working_diff_by_branch",
        ] {
            assert!(
                catalog.surface(surface_name).is_some(),
                "{surface_name} should remain public"
            );
        }
        for surface_name in [
            "lix_state",
            "lix_state_by_branch",
            "lix_state_history",
            "lix_label",
            "lix_label_by_branch",
            "lix_label_history",
            "lix_label_assignment",
            "lix_label_assignment_by_branch",
            "lix_label_assignment_history",
            "lix_checkpoint_by_branch",
            "lix_undo_redo_marker",
            "lix_collection_generation",
            "lix_binary_blob_ref",
            "lix_binary_blob_ref_by_branch",
            "lix_binary_blob_ref_history",
            "lix_directory_descriptor",
            "lix_directory_descriptor_by_branch",
            "lix_directory_descriptor_history",
            "lix_file_descriptor",
            "lix_file_descriptor_by_branch",
            "lix_file_descriptor_history",
        ] {
            assert!(
                catalog.surface(surface_name).is_none(),
                "{surface_name} should not be public"
            );
        }
    }

    #[test]
    fn runtime_schema_namespace_check_matches_unquoted_sql_normalization() {
        for schema_key in [
            "lix",
            "LIX",
            "lix_plugin_note",
            "LIX_PLUGIN_NOTE",
            "LiX_PlUgIn_NoTe",
        ] {
            assert!(
                PublicCatalog::runtime_schema_key_uses_reserved_namespace(schema_key),
                "{schema_key} should normalize into the reserved namespace"
            );
        }
        assert!(!PublicCatalog::runtime_schema_key_uses_reserved_namespace(
            "acme_lix_note"
        ));
    }

    #[test]
    fn dynamic_row_history_surface_uses_provider_history_column_names() {
        let catalog = catalog();
        let table = bind_public_table(
            &catalog,
            &table_name("SELECT * FROM test_state_schema_history()"),
        )
        .expect("schema history surface should bind");

        assert!(matches!(
            table.surface.kind,
            PublicSurfaceKind::SchemaHistory { .. }
        ));
        assert!(require_public_column(&table, "lixcol_row_pk").is_ok());
        assert!(require_public_column(&table, "lixcol_snapshot_content").is_err());
    }

    #[test]
    fn dynamic_row_file_id_is_public_and_insert_only() {
        let catalog = catalog();
        let table = bind_public_table(&catalog, &table_name("SELECT * FROM test_state_schema"))
            .expect("schema surface should bind");

        assert!(require_public_column(&table, "lixcol_file_id").is_ok());
        assert!(require_writable_column(&table, "lixcol_file_id", BoundWriteOp::Insert).is_ok());
        let error = require_writable_column(&table, "lixcol_file_id", BoundWriteOp::Update)
            .expect_err("row file id should remain immutable after insert");
        assert!(error.message.contains("is not writable"));
    }

    fn catalog() -> PublicCatalog {
        PublicCatalog::from_visible_schemas(&[json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "test_state_schema",
            "columns": [
                { "name": "id", "type": "text", "nullable": false },
                { "name": "name", "type": "text", "nullable": true },
                { "name": "lixcol_internal", "type": "text", "nullable": true },
            ],
            "primary_key": ["id"],
        })])
        .expect("test catalog")
    }

    fn table_name(sql: &str) -> ObjectName {
        let mut statements =
            Parser::parse_sql(&crate::sql2::dialect::lix_sql_dialect(), sql).expect("parse SQL");
        let Some(Statement::Query(query)) = statements.pop() else {
            panic!("expected query");
        };
        let SetExpr::Select(select) = query.body.as_ref() else {
            panic!("expected select");
        };
        let TableFactor::Table { name, .. } = &select.from[0].relation else {
            panic!("expected table factor");
        };
        name.clone()
    }
}