feat: ADR-0035 4a — SQL CREATE TABLE command, worker, and exit gate
Command + builder + worker for advanced-mode SQL CREATE TABLE (sub-phase 4a), executed structurally through do_create_table: - Command::SqlCreateTable + build_sql_create_table (ddl.rs): aliases via from_sql_name (incl. double precision), column- and table-level PRIMARY KEY, redundant-flag de-dup off a sole PK, IF NOT EXISTS. Advanced REGISTRY entry on the shared `create` word (SQL-first, DSL fallback); no-PK tables allowed (user-confirmed). - Worker (db.rs): Request::SqlCreateTable + CreateOutcome + snapshot_then (one undo step); IF NOT EXISTS no-op (no snapshot, but journalled, like read-only commands). do_create_table inline-PK rule aligned with the rebuild generator schema_to_ddl — no round-trip DDL drift; serial autoincrement is independent of inline-PK (verified by round-trip tests). - Runtime/App: dispatch + CommandOutcome::SchemaSkipped + AppEvent::DslCreateSkipped (structure + "already exists — skipped" note). Friendly catalog keys added (engine-neutral). DEFAULT/CHECK/table-level UNIQUE are absent from the 4a grammar (parse error with usage skeleton; friendly message + support land in the 4a.2 constraint slice) — user-confirmed. Tests: type resolver, grammar shape, builder (incl. the PK detection bug they caught), and tests/sql_create_table.rs (worker round-trip, serial autoincrement first/non-first across rebuild, IF NOT EXISTS no-op + journalling, no-PK table, one undo step) + a replay-as- write test. 1739 pass / 0 fail / 1 ignored; clippy clean. Exit gate: ADR-0035 Proposed -> Accepted (validated end-to-end by 4a); README + requirements.md Q1 updated.
This commit is contained in:
@@ -367,10 +367,177 @@ mod tests {
|
||||
fn deferred_constraints_are_not_accepted_in_4a() {
|
||||
// DEFAULT / CHECK / table-level UNIQUE belong to the 4a.2
|
||||
// constraint slice; their shapes are absent here, so they do
|
||||
// not walk (the builder turns this into a friendly
|
||||
// "not yet supported" — tested there).
|
||||
// not walk (surfacing as a parse error with the usage
|
||||
// skeleton, which lists the supported surface).
|
||||
bad("table t (id int default 0)");
|
||||
bad("table t (id int check (id > 0))");
|
||||
bad("table t (a int, b int, unique (a, b))");
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Builder tests — `parse_command` (advanced mode) lowers the SQL
|
||||
// `CREATE TABLE` to `Command::SqlCreateTable` with the right fields
|
||||
// (ADR-0035 §1/§3, sub-phase 4a).
|
||||
// =================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod builder_tests {
|
||||
use crate::dsl::command::Command;
|
||||
use crate::dsl::parser::{parse_command, parse_command_in_mode};
|
||||
use crate::dsl::types::Type;
|
||||
use crate::mode::Mode;
|
||||
|
||||
/// Parse in advanced mode and unwrap the `SqlCreateTable` fields.
|
||||
fn sct(input: &str) -> (String, Vec<(String, Type)>, Vec<String>, bool) {
|
||||
match parse_command(input).expect("should parse as SQL CREATE TABLE") {
|
||||
Command::SqlCreateTable {
|
||||
name,
|
||||
columns,
|
||||
primary_key,
|
||||
if_not_exists,
|
||||
} => (
|
||||
name,
|
||||
columns.into_iter().map(|c| (c.name, c.ty)).collect(),
|
||||
primary_key,
|
||||
if_not_exists,
|
||||
),
|
||||
other => panic!("expected SqlCreateTable, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimal_columns_and_types() {
|
||||
let (name, cols, pk, ine) = sct("create table t (id int, name text)");
|
||||
assert_eq!(name, "t");
|
||||
assert_eq!(
|
||||
cols,
|
||||
vec![("id".to_string(), Type::Int), ("name".to_string(), Type::Text)]
|
||||
);
|
||||
assert!(pk.is_empty(), "no PK declared");
|
||||
assert!(!ine);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integer_primary_key_is_plain_int_not_serial() {
|
||||
// ADR-0035 §3: the type map is lexical; INTEGER PRIMARY KEY is
|
||||
// a plain int PK, NOT auto-increment (that is `serial`).
|
||||
let (_, cols, pk, _) = sct("create table t (id integer primary key)");
|
||||
assert_eq!(cols, vec![("id".to_string(), Type::Int)]);
|
||||
assert_eq!(pk, vec!["id".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn standard_sql_aliases_map_to_playground_types() {
|
||||
let (_, cols, _, _) = sct(
|
||||
"create table t (a bigint, b varchar, c boolean, d timestamp, \
|
||||
e numeric, f float, g binary)",
|
||||
);
|
||||
assert_eq!(
|
||||
cols,
|
||||
vec![
|
||||
("a".to_string(), Type::Int),
|
||||
("b".to_string(), Type::Text),
|
||||
("c".to_string(), Type::Bool),
|
||||
("d".to_string(), Type::DateTime),
|
||||
("e".to_string(), Type::Decimal),
|
||||
("f".to_string(), Type::Real),
|
||||
("g".to_string(), Type::Blob),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn double_precision_maps_to_real() {
|
||||
let (_, cols, _, _) = sct("create table t (id int, x double precision)");
|
||||
assert_eq!(
|
||||
cols,
|
||||
vec![("id".to_string(), Type::Int), ("x".to_string(), Type::Real)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn length_args_are_ignored() {
|
||||
let (_, cols, _, _) = sct("create table t (a varchar(255), b numeric(10, 2))");
|
||||
assert_eq!(
|
||||
cols,
|
||||
vec![("a".to_string(), Type::Text), ("b".to_string(), Type::Decimal)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn column_level_primary_key_populates_pk() {
|
||||
let (_, _, pk, _) = sct("create table t (id serial primary key, name text)");
|
||||
assert_eq!(pk, vec!["id".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_level_compound_primary_key() {
|
||||
let (_, _, pk, _) = sct("create table t (a int, b int, primary key (a, b))");
|
||||
assert_eq!(pk, vec!["a".to_string(), "b".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn if_not_exists_sets_the_flag() {
|
||||
let (name, _, _, ine) = sct("create table if not exists t (id int)");
|
||||
assert_eq!(name, "t");
|
||||
assert!(ine);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_null_and_unique_attach_to_their_column() {
|
||||
match parse_command("create table t (id int primary key, code text not null unique)")
|
||||
.expect("parses")
|
||||
{
|
||||
Command::SqlCreateTable { columns, .. } => {
|
||||
let code = columns.iter().find(|c| c.name == "code").expect("code col");
|
||||
assert!(code.not_null && code.unique);
|
||||
}
|
||||
other => panic!("expected SqlCreateTable, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redundant_constraints_deduped_off_sole_pk_column() {
|
||||
// ADR-0035 §6.5: advanced mode accepts the redundant spelling
|
||||
// and silently drops the flags off the sole PK column.
|
||||
match parse_command("create table t (id int primary key not null unique)")
|
||||
.expect("parses")
|
||||
{
|
||||
Command::SqlCreateTable {
|
||||
columns,
|
||||
primary_key,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(primary_key, vec!["id".to_string()]);
|
||||
let id = &columns[0];
|
||||
assert!(!id.not_null && !id.unique, "redundant flags deduped off PK");
|
||||
}
|
||||
other => panic!("expected SqlCreateTable, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_create_still_parses_as_dsl_in_both_modes() {
|
||||
// The shared `create` entry word: the DSL `with pk` form falls
|
||||
// back to `Command::CreateTable` even in advanced mode, and is
|
||||
// the only shape in simple mode (ADR-0035 §2 dispatch).
|
||||
for mode in [Mode::Simple, Mode::Advanced] {
|
||||
let cmd = parse_command_in_mode("create table T with pk id(serial)", mode)
|
||||
.unwrap_or_else(|e| panic!("{mode:?} should parse the DSL form: {e:?}"));
|
||||
assert!(
|
||||
matches!(cmd, Command::CreateTable { .. }),
|
||||
"{mode:?}: expected DSL CreateTable, got {cmd:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sql_create_is_advanced_only() {
|
||||
// The SQL `( … )` form is not available in simple mode.
|
||||
assert!(
|
||||
parse_command_in_mode("create table t (id int)", Mode::Simple).is_err(),
|
||||
"SQL CREATE TABLE must not parse in simple mode"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user