631074ff9c
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.
369 lines
12 KiB
Rust
369 lines
12 KiB
Rust
//! Sub-phase 4a integration tests for advanced-mode SQL
|
|
//! `CREATE TABLE` (ADR-0035 §1/§4).
|
|
//!
|
|
//! Worker round-trip: a `Command::SqlCreateTable` executes
|
|
//! **structurally** through the existing `do_create_table` machinery,
|
|
//! so an advanced-mode-created table is a first-class playground
|
|
//! object (metadata + the ten-type vocabulary). Covers:
|
|
//! - Created tables appear in `list_tables` and `describe_table`
|
|
//! reports the playground `user_type` per column.
|
|
//! - A `serial` sole-PK autoincrements even in a multi-column table
|
|
//! (the §6.4 inline-`PRIMARY KEY` extension).
|
|
//! - `IF NOT EXISTS` on an existing table is a no-op (`Skipped`); the
|
|
//! plain form errors when the table exists (§4).
|
|
//! - One SQL `CREATE TABLE` is exactly one undo step (ADR-0006).
|
|
//!
|
|
//! Parsing (text → `Command::SqlCreateTable`) is covered by the
|
|
//! `builder_tests` in `src/dsl/grammar/sql_create_table.rs`; these
|
|
//! tests drive the worker directly, mirroring `tests/sql_insert.rs`.
|
|
|
|
use rdbms_playground::db::{CreateOutcome, Database};
|
|
use rdbms_playground::dsl::{ColumnSpec, Type, Value};
|
|
use rdbms_playground::persistence::Persistence;
|
|
use rdbms_playground::project;
|
|
|
|
fn rt() -> tokio::runtime::Runtime {
|
|
tokio::runtime::Builder::new_current_thread()
|
|
.enable_all()
|
|
.build()
|
|
.expect("tokio rt")
|
|
}
|
|
|
|
fn open(undo: bool) -> (project::Project, Database, tempfile::TempDir) {
|
|
let dir = tempfile::tempdir().expect("create tempdir");
|
|
let project =
|
|
project::open_or_create(None, Some(dir.path())).expect("open or create project");
|
|
let persistence = Persistence::new(project.path().to_path_buf());
|
|
let db = Database::open_with_persistence_and_undo(project.db_path(), persistence, undo)
|
|
.expect("open db with persistence");
|
|
(project, db, dir)
|
|
}
|
|
|
|
#[test]
|
|
fn created_table_appears_with_playground_types() {
|
|
let (_p, db, _d) = open(false);
|
|
let r = rt();
|
|
let out = r
|
|
.block_on(db.sql_create_table(
|
|
"Widget".to_string(),
|
|
vec![
|
|
ColumnSpec::new("id", Type::Int),
|
|
ColumnSpec::new("name", Type::Text),
|
|
],
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table Widget (id int primary key, name text)".to_string()),
|
|
))
|
|
.expect("create should succeed");
|
|
assert!(matches!(out, CreateOutcome::Created(_)));
|
|
|
|
let tables = r.block_on(db.list_tables()).expect("list");
|
|
assert!(tables.contains(&"Widget".to_string()));
|
|
|
|
let desc = r
|
|
.block_on(db.describe_table("Widget".to_string(), None))
|
|
.expect("describe");
|
|
let types: Vec<(String, Option<Type>)> = desc
|
|
.columns
|
|
.iter()
|
|
.map(|c| (c.name.clone(), c.user_type))
|
|
.collect();
|
|
assert_eq!(
|
|
types,
|
|
vec![
|
|
("id".to_string(), Some(Type::Int)),
|
|
("name".to_string(), Some(Type::Text)),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn integer_primary_key_is_plain_int() {
|
|
// ADR-0035 §3: INTEGER PRIMARY KEY maps to plain `int`, not
|
|
// `serial`. The structural object reports `int`.
|
|
let (_p, db, _d) = open(false);
|
|
let r = rt();
|
|
r.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
vec![ColumnSpec::new("id", Type::Int)],
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (id integer primary key)".to_string()),
|
|
))
|
|
.expect("create");
|
|
let desc = r
|
|
.block_on(db.describe_table("T".to_string(), None))
|
|
.expect("describe");
|
|
assert_eq!(desc.columns[0].user_type, Some(Type::Int));
|
|
}
|
|
|
|
#[test]
|
|
fn serial_pk_autoincrements_in_multi_column_table() {
|
|
// §6.4: a `serial` sole-PK in a *multi-column* table must inline
|
|
// `PRIMARY KEY` so it keeps autoincrement (rowid-alias) semantics
|
|
// — the case simple mode never produces in one statement.
|
|
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("name", Type::Text),
|
|
],
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (id serial primary key, name text)".to_string()),
|
|
))
|
|
.expect("create");
|
|
|
|
// Form B inserts (no column list): the serial id is auto-filled.
|
|
for name in ["a", "b"] {
|
|
r.block_on(db.insert(
|
|
"T".to_string(),
|
|
None,
|
|
vec![Value::Text(name.to_string())],
|
|
Some(format!("insert into T (name) values ('{name}')")),
|
|
))
|
|
.expect("insert");
|
|
}
|
|
|
|
let data = r
|
|
.block_on(db.query_data("T".to_string(), None, None, None))
|
|
.expect("query");
|
|
let id_idx = data
|
|
.columns
|
|
.iter()
|
|
.position(|c| c == "id")
|
|
.expect("id column");
|
|
let mut ids: Vec<Option<String>> = data.rows.iter().map(|row| row[id_idx].clone()).collect();
|
|
ids.sort();
|
|
assert_eq!(
|
|
ids,
|
|
vec![Some("1".to_string()), Some("2".to_string())],
|
|
"serial PK autoincremented 1, 2"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn if_not_exists_is_a_noop_when_table_exists() {
|
|
let (_p, db, _d) = open(false);
|
|
let r = rt();
|
|
let specs = || vec![ColumnSpec::new("id", Type::Int)];
|
|
r.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
specs(),
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (id int)".to_string()),
|
|
))
|
|
.expect("first create");
|
|
|
|
let out = r
|
|
.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
specs(),
|
|
vec!["id".to_string()],
|
|
true, // IF NOT EXISTS
|
|
Some("create table if not exists T (id int)".to_string()),
|
|
))
|
|
.expect("second create should succeed as a no-op");
|
|
assert!(
|
|
matches!(out, CreateOutcome::Skipped(_)),
|
|
"IF NOT EXISTS on an existing table is a no-op"
|
|
);
|
|
|
|
let tables = r.block_on(db.list_tables()).expect("list");
|
|
assert_eq!(tables.iter().filter(|t| t.as_str() == "T").count(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn table_without_primary_key_is_allowed() {
|
|
// Advanced mode allows a PK-less table (standard SQL; the
|
|
// "trust the user like SQL" posture, ADR-0035 §7) — unlike simple
|
|
// mode, which requires/defaults a PK. User-confirmed 2026-05-25.
|
|
let (_p, db, _d) = open(false);
|
|
let r = rt();
|
|
let out = r
|
|
.block_on(db.sql_create_table(
|
|
"Notes".to_string(),
|
|
vec![ColumnSpec::new("body", Type::Text)],
|
|
vec![], // no primary key
|
|
false,
|
|
Some("create table Notes (body text)".to_string()),
|
|
))
|
|
.expect("a PK-less table should create");
|
|
assert!(matches!(out, CreateOutcome::Created(_)));
|
|
// And it is usable: a row inserts and reads back.
|
|
r.block_on(db.insert(
|
|
"Notes".to_string(),
|
|
None,
|
|
vec![Value::Text("hello".to_string())],
|
|
Some("insert into Notes (body) values ('hello')".to_string()),
|
|
))
|
|
.expect("insert into PK-less table");
|
|
let data = r
|
|
.block_on(db.query_data("Notes".to_string(), None, None, None))
|
|
.expect("query");
|
|
assert_eq!(data.rows.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn if_not_exists_noop_is_journalled() {
|
|
// A successful no-op is still a submission and belongs in the
|
|
// complete journal (ADR-0034) — like read-only `show table`, and
|
|
// unlike a *failed* duplicate-create (journalled `err`).
|
|
let (p, db, _d) = open(false);
|
|
let r = rt();
|
|
r.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
vec![ColumnSpec::new("id", Type::Int)],
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (id int)".to_string()),
|
|
))
|
|
.expect("first create");
|
|
let noop = "create table if not exists T (id int)";
|
|
let out = r
|
|
.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
vec![ColumnSpec::new("id", Type::Int)],
|
|
vec!["id".to_string()],
|
|
true,
|
|
Some(noop.to_string()),
|
|
))
|
|
.expect("no-op");
|
|
assert!(matches!(out, CreateOutcome::Skipped(_)));
|
|
let log = std::fs::read_to_string(p.path().join("history.log")).expect("read history.log");
|
|
assert!(log.contains(noop), "the no-op skip should be journalled; log:\n{log}");
|
|
}
|
|
|
|
#[test]
|
|
fn plain_create_errors_when_table_exists() {
|
|
let (_p, db, _d) = open(false);
|
|
let r = rt();
|
|
let specs = || vec![ColumnSpec::new("id", Type::Int)];
|
|
r.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
specs(),
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (id int)".to_string()),
|
|
))
|
|
.expect("first create");
|
|
|
|
let err = r.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
specs(),
|
|
vec!["id".to_string()],
|
|
false, // no IF NOT EXISTS
|
|
Some("create table T (id int)".to_string()),
|
|
));
|
|
assert!(err.is_err(), "re-creating an existing table without IF NOT EXISTS errors");
|
|
}
|
|
|
|
#[test]
|
|
fn sql_create_table_is_one_undo_step() {
|
|
let (_p, db, _d) = open(true); // undo enabled
|
|
let r = rt();
|
|
r.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
vec![ColumnSpec::new("id", Type::Int)],
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (id int)".to_string()),
|
|
))
|
|
.expect("create");
|
|
assert!(r.block_on(db.list_tables()).unwrap().contains(&"T".to_string()));
|
|
|
|
let undone = r.block_on(db.undo()).expect("undo call");
|
|
assert!(undone.is_some(), "the CREATE TABLE recorded one undo step");
|
|
assert!(
|
|
!r.block_on(db.list_tables()).unwrap().contains(&"T".to_string()),
|
|
"table is gone after a single undo"
|
|
);
|
|
}
|
|
|
|
/// Sorted `id` column values of table `T`.
|
|
fn ids(db: &Database, r: &tokio::runtime::Runtime) -> Vec<Option<String>> {
|
|
let d = r
|
|
.block_on(db.query_data("T".to_string(), None, None, None))
|
|
.expect("query");
|
|
let idx = d.columns.iter().position(|c| c == "id").expect("id column");
|
|
let mut v: Vec<Option<String>> = d.rows.iter().map(|row| row[idx].clone()).collect();
|
|
v.sort();
|
|
v
|
|
}
|
|
|
|
fn insert_row(db: &Database, r: &tokio::runtime::Runtime, name: &str) {
|
|
r.block_on(db.insert(
|
|
"T".to_string(),
|
|
None,
|
|
vec![Value::Text(name.to_string())],
|
|
Some(format!("insert into T (name) values ('{name}')")),
|
|
))
|
|
.expect("insert");
|
|
}
|
|
|
|
/// `serial` PK as the **first** column must keep autoincrement across a
|
|
/// rebuild: the structural create and the `schema_to_ddl` rebuild both
|
|
/// inline `PRIMARY KEY` on a first-column single PK, so the DDL is
|
|
/// identical and the sequence continues (id 3 after rebuild).
|
|
#[test]
|
|
fn serial_pk_first_column_autoincrements_after_rebuild() {
|
|
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("name", Type::Text),
|
|
],
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (id serial primary key, name text)".to_string()),
|
|
))
|
|
.expect("create");
|
|
insert_row(&db, &r, "a");
|
|
insert_row(&db, &r, "b");
|
|
r.block_on(db.rebuild_from_text(p.path().to_path_buf(), None))
|
|
.expect("rebuild");
|
|
insert_row(&db, &r, "c");
|
|
assert_eq!(
|
|
ids(&db, &r),
|
|
vec![Some("1".to_string()), Some("2".to_string()), Some("3".to_string())]
|
|
);
|
|
}
|
|
|
|
/// `serial` PK as a **non-first** column must also keep autoincrement
|
|
/// across a rebuild. Here the rebuild emits a *table-level* PK (the PK
|
|
/// is not column 0), proving autoincrement does not rely on the
|
|
/// rowid-alias / inline-PK form — the insert path computes the next
|
|
/// value itself (ADR-0035 §6.4). Guards against silent round-trip loss.
|
|
#[test]
|
|
fn serial_pk_non_first_column_autoincrements_after_rebuild() {
|
|
let (p, db, _d) = open(false);
|
|
let r = rt();
|
|
r.block_on(db.sql_create_table(
|
|
"T".to_string(),
|
|
vec![
|
|
ColumnSpec::new("name", Type::Text),
|
|
ColumnSpec::new("id", Type::Serial),
|
|
],
|
|
vec!["id".to_string()],
|
|
false,
|
|
Some("create table T (name text, id serial primary key)".to_string()),
|
|
))
|
|
.expect("create");
|
|
insert_row(&db, &r, "a");
|
|
insert_row(&db, &r, "b");
|
|
assert_eq!(ids(&db, &r), vec![Some("1".to_string()), Some("2".to_string())]);
|
|
r.block_on(db.rebuild_from_text(p.path().to_path_buf(), None))
|
|
.expect("rebuild");
|
|
insert_row(&db, &r, "c");
|
|
assert_eq!(
|
|
ids(&db, &r),
|
|
vec![Some("1".to_string()), Some("2".to_string()), Some("3".to_string())],
|
|
"serial keeps autoincrement after a rebuild even as a non-first column"
|
|
);
|
|
}
|