feat: ADR-0035 4a.2 — per-column CHECK/DEFAULT + composite UNIQUE

Advanced-mode SQL CREATE TABLE gains the constraints that need no new
internal table (the 4a.2 slice):

- Grammar (sql_create_table.rs): column-level DEFAULT/CHECK and
  table-level UNIQUE(cols). DEFAULT is a literal or a *parenthesised*
  expression (standard SQL) — a bare sql_expr greedily eats a following
  NOT (NOT IN/LIKE/BETWEEN), breaking `DEFAULT 0 NOT NULL`; the parens
  bound it. CHECK is paren-bounded already.
- Builder (ddl.rs): captures CHECK/DEFAULT raw SQL text by byte span
  (sql_expr builds no AST) via capture_parenthesised_span /
  capture_expr_span; routes single-column table UNIQUE into the
  column's flag and composite UNIQUE into unique_constraints.
- Command/worker: ColumnSpec gains check_sql/default_sql (raw, preferred
  over the typed Expr/Value); Command::SqlCreateTable + Request +
  do_create_table gain unique_constraints; do_create_table emits raw
  CHECK/DEFAULT and composite UNIQUE clauses.
- Round-trip (part D): ReadSchema/TableSchema gain unique_constraints;
  read_schema detects composite UNIQUE via PRAGMA index_list origin 'u'
  (single-column still folds to the column flag); schema_to_ddl emits
  them; YAML RawTable/write_table round-trips (optional-on-read).
  CHECK round-trips via __rdbms_playground_columns.check_expr, DEFAULT
  via PRAGMA table_info — no new metadata table.

Table-level/multi-column CHECK remains 4a.3 (rejected "not yet
supported"); FK is 4b.

Tests: +7 builder (raw-text capture incl. the DEFAULT 0 NOT NULL
boundary the fix was found by; single/composite UNIQUE routing) and +4
Tier-3 (CHECK enforced, DEFAULT applied, composite UNIQUE enforced, and
all three survive a rebuild — the part-D round-trip). 1752 pass / 0 fail
/ 1 ignored; clippy clean. Plan + requirements.md updated.
This commit is contained in:
claude@clouddev1
2026-05-25 11:04:59 +00:00
parent 1c50133438
commit c0f5626787
10 changed files with 627 additions and 59 deletions
+25
View File
@@ -90,6 +90,21 @@ fn write_table(out: &mut String, table: &TableSchema) {
for col in &table.columns {
write_column(out, col);
}
// Composite (multi-column) UNIQUE constraints (ADR-0035 §4a.2) —
// emitted only when present so unconstrained tables stay compact.
if !table.unique_constraints.is_empty() {
let _ = writeln!(out, " unique_constraints:");
for cols in &table.unique_constraints {
write!(out, " - [").unwrap();
for (i, c) in cols.iter().enumerate() {
if i > 0 {
out.push_str(", ");
}
out.push_str(&quote_if_needed(c));
}
let _ = writeln!(out, "]");
}
}
}
/// Always render `s` as a double-quoted YAML string — used
@@ -249,6 +264,7 @@ pub(crate) fn parse_schema(body: &str) -> Result<SchemaSnapshot, YamlError> {
name: t.name,
primary_key: t.primary_key,
columns,
unique_constraints: t.unique_constraints,
});
}
let mut relationships: Vec<RelationshipSchema> = Vec::with_capacity(raw.relationships.len());
@@ -357,6 +373,10 @@ struct RawTable {
name: String,
primary_key: Vec<String>,
columns: Vec<RawColumn>,
/// Composite (multi-column) UNIQUE constraints (ADR-0035 §4a.2).
/// Optional on read — older project files omit it.
#[serde(default)]
unique_constraints: Vec<Vec<String>>,
}
#[derive(Deserialize)]
@@ -418,6 +438,7 @@ mod tests {
ColumnSchema { name: "id".to_string(), user_type: Type::Serial, unique: false, not_null: false, default: None, check: None },
ColumnSchema { name: "Name".to_string(), user_type: Type::Text, unique: false, not_null: false, default: None, check: None },
],
unique_constraints: Vec::new(),
},
TableSchema {
name: "Orders".to_string(),
@@ -426,6 +447,7 @@ mod tests {
ColumnSchema { name: "id".to_string(), user_type: Type::Serial, unique: false, not_null: false, default: None, check: None },
ColumnSchema { name: "CustId".to_string(), user_type: Type::Int, unique: false, not_null: false, default: None, check: None },
],
unique_constraints: Vec::new(),
},
],
relationships: vec![RelationshipSchema {
@@ -494,6 +516,7 @@ mod tests {
default: None,
check: None,
}],
unique_constraints: Vec::new(),
}],
relationships: vec![],
indexes: vec![],
@@ -551,6 +574,7 @@ mod tests {
check: Some("\"stock\" >= 0".to_string()),
},
],
unique_constraints: Vec::new(),
}],
relationships: vec![],
indexes: vec![],
@@ -637,6 +661,7 @@ relationships:
ColumnSchema { name: "a".to_string(), user_type: Type::Int, unique: false, not_null: false, default: None, check: None },
ColumnSchema { name: "b".to_string(), user_type: Type::Int, unique: false, not_null: false, default: None, check: None },
],
unique_constraints: Vec::new(),
}],
relationships: vec![],
indexes: vec![],