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:
@@ -51,6 +51,7 @@ fn created_table_appears_with_playground_types() {
|
||||
ColumnSpec::new("name", Type::Text),
|
||||
],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table Widget (id int primary key, name text)".to_string()),
|
||||
))
|
||||
@@ -87,6 +88,7 @@ fn integer_primary_key_is_plain_int() {
|
||||
"T".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (id integer primary key)".to_string()),
|
||||
))
|
||||
@@ -111,6 +113,7 @@ fn serial_pk_autoincrements_in_multi_column_table() {
|
||||
ColumnSpec::new("name", Type::Text),
|
||||
],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (id serial primary key, name text)".to_string()),
|
||||
))
|
||||
@@ -153,6 +156,7 @@ fn if_not_exists_is_a_noop_when_table_exists() {
|
||||
"T".to_string(),
|
||||
specs(),
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -163,6 +167,7 @@ fn if_not_exists_is_a_noop_when_table_exists() {
|
||||
"T".to_string(),
|
||||
specs(),
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
true, // IF NOT EXISTS
|
||||
Some("create table if not exists T (id int)".to_string()),
|
||||
))
|
||||
@@ -188,6 +193,7 @@ fn table_without_primary_key_is_allowed() {
|
||||
"Notes".to_string(),
|
||||
vec![ColumnSpec::new("body", Type::Text)],
|
||||
vec![], // no primary key
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table Notes (body text)".to_string()),
|
||||
))
|
||||
@@ -207,6 +213,162 @@ fn table_without_primary_key_is_allowed() {
|
||||
assert_eq!(data.rows.len(), 1);
|
||||
}
|
||||
|
||||
/// A column carrying a raw-SQL `CHECK` (ADR-0035 §4a.2).
|
||||
fn col_check(name: &str, ty: Type, check_sql: &str) -> ColumnSpec {
|
||||
let mut c = ColumnSpec::new(name, ty);
|
||||
c.check_sql = Some(check_sql.to_string());
|
||||
c
|
||||
}
|
||||
|
||||
/// A column carrying a raw-SQL `DEFAULT` (ADR-0035 §4a.2).
|
||||
fn col_default(name: &str, ty: Type, default_sql: &str) -> ColumnSpec {
|
||||
let mut c = ColumnSpec::new(name, ty);
|
||||
c.default_sql = Some(default_sql.to_string());
|
||||
c
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_constraint_is_enforced() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"T".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), col_check("price", Type::Real, "price >= 0")],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
false,
|
||||
Some("create table T (id serial primary key, price real check (price >= 0))".to_string()),
|
||||
))
|
||||
.expect("create");
|
||||
// A satisfying row inserts; a violating one is rejected by the CHECK.
|
||||
r.block_on(db.insert(
|
||||
"T".to_string(),
|
||||
Some(vec!["price".to_string()]),
|
||||
vec![Value::Number("10".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.expect("price 10 satisfies the check");
|
||||
let bad = r.block_on(db.insert(
|
||||
"T".to_string(),
|
||||
Some(vec!["price".to_string()]),
|
||||
vec![Value::Number("-5".to_string())],
|
||||
Some("insert".to_string()),
|
||||
));
|
||||
assert!(bad.is_err(), "CHECK (price >= 0) rejects -5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_is_applied_when_column_omitted() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"T".to_string(),
|
||||
vec![
|
||||
ColumnSpec::new("id", Type::Serial),
|
||||
ColumnSpec::new("label", Type::Text),
|
||||
col_default("n", Type::Int, "7"),
|
||||
],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
false,
|
||||
Some("create table T (id serial primary key, label text, n int default 7)".to_string()),
|
||||
))
|
||||
.expect("create");
|
||||
// Insert only `label`; `id` auto-fills and `n` takes its default.
|
||||
r.block_on(db.insert(
|
||||
"T".to_string(),
|
||||
Some(vec!["label".to_string()]),
|
||||
vec![Value::Text("x".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.expect("insert");
|
||||
let data = r
|
||||
.block_on(db.query_data("T".to_string(), None, None, None))
|
||||
.expect("query");
|
||||
let n_idx = data.columns.iter().position(|c| c == "n").expect("n column");
|
||||
assert_eq!(data.rows[0][n_idx].as_deref(), Some("7"), "DEFAULT 7 applied");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composite_unique_is_enforced() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"T".to_string(),
|
||||
vec![ColumnSpec::new("a", Type::Int), ColumnSpec::new("b", Type::Int)],
|
||||
vec![],
|
||||
vec![vec!["a".to_string(), "b".to_string()]],
|
||||
false,
|
||||
Some("create table T (a int, b int, unique (a, b))".to_string()),
|
||||
))
|
||||
.expect("create");
|
||||
let ins = |a: &str, b: &str| {
|
||||
db.insert(
|
||||
"T".to_string(),
|
||||
None,
|
||||
vec![Value::Number(a.to_string()), Value::Number(b.to_string())],
|
||||
Some("insert".to_string()),
|
||||
)
|
||||
};
|
||||
r.block_on(ins("1", "2")).expect("first (1,2)");
|
||||
assert!(r.block_on(ins("1", "2")).is_err(), "UNIQUE(a,b) rejects duplicate (1,2)");
|
||||
r.block_on(ins("1", "3")).expect("distinct (1,3) is allowed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_default_and_composite_unique_survive_rebuild() {
|
||||
// The part-D round-trip: CHECK (metadata), DEFAULT (PRAGMA), and
|
||||
// composite UNIQUE (TableSchema + PRAGMA index_list origin 'u')
|
||||
// must all be reconstructed from project.yaml on rebuild.
|
||||
let (p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"T".to_string(),
|
||||
vec![
|
||||
ColumnSpec::new("a", Type::Int),
|
||||
ColumnSpec::new("b", Type::Int),
|
||||
col_check("price", Type::Real, "price >= 0"),
|
||||
col_default("n", Type::Int, "7"),
|
||||
],
|
||||
vec![],
|
||||
vec![vec!["a".to_string(), "b".to_string()]],
|
||||
false,
|
||||
Some(
|
||||
"create table T (a int, b int, price real check (price >= 0), \
|
||||
n int default 7, unique (a, b))"
|
||||
.to_string(),
|
||||
),
|
||||
))
|
||||
.expect("create");
|
||||
|
||||
r.block_on(db.rebuild_from_text(p.path().to_path_buf(), None))
|
||||
.expect("rebuild");
|
||||
|
||||
let ins = |a: &str, b: &str, price: &str| {
|
||||
db.insert(
|
||||
"T".to_string(),
|
||||
Some(vec!["a".to_string(), "b".to_string(), "price".to_string()]),
|
||||
vec![
|
||||
Value::Number(a.to_string()),
|
||||
Value::Number(b.to_string()),
|
||||
Value::Number(price.to_string()),
|
||||
],
|
||||
Some("insert".to_string()),
|
||||
)
|
||||
};
|
||||
// CHECK survived: a negative price is rejected.
|
||||
assert!(r.block_on(ins("1", "1", "-1")).is_err(), "CHECK survived rebuild");
|
||||
// A valid row inserts; DEFAULT n=7 survived.
|
||||
r.block_on(ins("1", "1", "5")).expect("valid row");
|
||||
let data = r
|
||||
.block_on(db.query_data("T".to_string(), None, None, None))
|
||||
.expect("query");
|
||||
let n_idx = data.columns.iter().position(|c| c == "n").expect("n column");
|
||||
assert_eq!(data.rows[0][n_idx].as_deref(), Some("7"), "DEFAULT survived rebuild");
|
||||
// Composite UNIQUE survived: (1,1) again is rejected.
|
||||
assert!(r.block_on(ins("1", "1", "5")).is_err(), "composite UNIQUE survived rebuild");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_not_exists_noop_is_journalled() {
|
||||
// A successful no-op is still a submission and belongs in the
|
||||
@@ -218,6 +380,7 @@ fn if_not_exists_noop_is_journalled() {
|
||||
"T".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -228,6 +391,7 @@ fn if_not_exists_noop_is_journalled() {
|
||||
"T".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
true,
|
||||
Some(noop.to_string()),
|
||||
))
|
||||
@@ -246,6 +410,7 @@ fn plain_create_errors_when_table_exists() {
|
||||
"T".to_string(),
|
||||
specs(),
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -255,6 +420,7 @@ fn plain_create_errors_when_table_exists() {
|
||||
"T".to_string(),
|
||||
specs(),
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false, // no IF NOT EXISTS
|
||||
Some("create table T (id int)".to_string()),
|
||||
));
|
||||
@@ -269,6 +435,7 @@ fn sql_create_table_is_one_undo_step() {
|
||||
"T".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -319,6 +486,7 @@ fn serial_pk_first_column_autoincrements_after_rebuild() {
|
||||
ColumnSpec::new("name", Type::Text),
|
||||
],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (id serial primary key, name text)".to_string()),
|
||||
))
|
||||
@@ -350,6 +518,7 @@ fn serial_pk_non_first_column_autoincrements_after_rebuild() {
|
||||
ColumnSpec::new("id", Type::Serial),
|
||||
],
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
false,
|
||||
Some("create table T (name text, id serial primary key)".to_string()),
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user