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:
claude@clouddev1
2026-05-25 10:04:28 +00:00
parent 80310929d7
commit 631074ff9c
18 changed files with 961 additions and 47 deletions
+14 -8
View File
@@ -2,11 +2,14 @@
## Status
Proposed. Design agreed with the user (2026-05-24); implementation
is phased and pending (§13). This is **Phase 4** of the ADR-0030
roadmap (the advanced-mode SQL surface), the peer of ADR-0031
(expression grammar), ADR-0032 (`SELECT`), and ADR-0033 (DML). It
**clarifies ADR-0030 §4** on how DDL is represented and executed.
Accepted. Design agreed with the user (2026-05-24); the approach is
**validated end-to-end by sub-phase 4a** (`CREATE TABLE`, implemented
2026-05-25 — plan `docs/plans/20260524-adr-0035-sql-ddl-4a.md`), so the
decision is accepted while the remaining sub-phases (**4a.2, 4b4i**,
§13) continue. This is **Phase 4** of the ADR-0030 roadmap (the
advanced-mode SQL surface), the peer of ADR-0031 (expression grammar),
ADR-0032 (`SELECT`), and ADR-0033 (DML). It **clarifies ADR-0030 §4**
on how DDL is represented and executed.
**Refinements (2026-05-24, pre-implementation `/runda` round,
user-confirmed).** Two open micro-calls were settled before 4a:
@@ -310,9 +313,12 @@ ADR-0033's structure:
**clean-reuse column constraints only** — `NOT NULL` / `UNIQUE` /
column-level `PRIMARY KEY` — + single/compound table-level
`PRIMARY KEY`, plus `IF NOT EXISTS` (no-op-with-note, §4). Reuses
`do_create_table` (extended so a `serial` sole-PK inlines `PRIMARY
KEY` in a multi-column table, preserving autoincrement). **No FK**
(4b); **no `DEFAULT`/`CHECK`/table-level `UNIQUE`** (4a.2).
`do_create_table`, whose inline-PK rule is aligned with the rebuild
generator `schema_to_ddl` (inline only a first-column single PK) so a
created table and its rebuilt form have identical DDL; `serial`
autoincrement is independent of inline-vs-table-level PK (the insert
path computes the next value), verified by round-trip tests. **No
FK** (4b); **no `DEFAULT`/`CHECK`/table-level `UNIQUE`** (4a.2).
- **4a.2 — The constraint slice.** Split out (2026-05-24,
user-confirmed) for the constraints that are *not* a clean reuse:
(1) **`CHECK`/`DEFAULT`** via the full `sql_expr` surface stored as
+1 -1
View File
File diff suppressed because one or more lines are too long
+34 -9
View File
@@ -292,15 +292,22 @@ the ADR flags).
Single-word aliases + an optional discarded `(len[,len])` cover the
rest. Delivers the ADR §3 item without bending it.
4. **`serial` PK inline emission (implementer call, step 3/worker).**
`do_create_table` today inlines `PRIMARY KEY` (the rowid-alias that
makes `serial` autoincrement) **only when the table has one column**.
SQL mode allows `CREATE TABLE t (id serial primary key, name text)` —
a `serial` sole-PK in a multi-column table — which would otherwise
get a table-level PK and **lose autoincrement**. Step 3 extends the
inline condition to "sole-PK column whose type is `serial` → inline
`PRIMARY KEY` on that column," leaving the simple-mode paths
(single-column, or compound) unchanged. Covered by a worker test.
4. **`do_create_table` inline-PK rule aligned with `schema_to_ddl`
(implementer call, step 3/worker; corrected by the `/runda` probe).**
*Original premise (wrong):* a `serial` sole-PK in a multi-column SQL
table needs an inline `PRIMARY KEY` (rowid-alias) or it loses
autoincrement. **A round-trip probe disproved this:** after a rebuild
the table gets a *table-level* PK (`schema_to_ddl` only inlines a
first-column PK), yet `serial` still autoincremented — the insert
path computes the next value itself, independent of rowid-alias.
*Actual fix:* `do_create_table` now uses the **same inline rule as
`schema_to_ddl`** — inline only a single PK that is the **first
column** — so a freshly-created table and its rebuilt form have
identical DDL (no round-trip drift, the real latent issue a
multi-column SQL create would have exposed). Simple-mode paths
(always single-column create + `add column`) are unchanged. Covered
by two round-trip tests (serial PK as first **and** non-first
column survive a rebuild).
5. **Redundant PK constraints (implementer call).** SQL mode is
**lenient** like real engines: `id int primary key not null` /
@@ -311,6 +318,24 @@ the ADR flags).
mode deliberately, matching the advanced-mode "trust the user like
SQL" posture, ADR-0035 §7.)
6. **Deferred constraints surface as a parse error (user-confirmed
2026-05-25).** `DEFAULT` / `CHECK` / table-level `UNIQUE` are absent
from the 4a grammar, so typing them is an ordinary parse error — the
ADR-0021 usage skeleton lists the supported `CREATE TABLE` form,
implicitly communicating what is not yet available. A bespoke
"not yet supported" message needs the deferred expression-parsing
work and so lands with 4a.2, when those shapes are added.
7. **No-PK tables allowed in advanced mode (user-confirmed
2026-05-25).** `create table t (id int)` with no primary key is
accepted (standard SQL; the §7 trust posture), unlike simple mode
which requires/defaults a PK. Pinned by a worker test.
8. **No-op skip is journalled (`/runda` fix).** A successful
`CREATE TABLE IF NOT EXISTS` no-op appends its line to `history.log`
like other read-only/no-op commands (`show table`) — the complete
journal (ADR-0034) — while taking no undo snapshot.
## 7. Devil's Advocate review of this plan
- **Does it reuse rather than fork execution?** Yes — `do_create_table`
+11 -8
View File
@@ -209,14 +209,17 @@ handoff-14 cleanup; 449 after B2/C2.)
- [ ] **Q1** SQL parsed via `sqlparser-rs`; supported subset is
defined (specifics deferred to a future ADR).
*(Progress: SQL handling in advanced mode is still a
placeholder echo. The architecture is now decided — ADR-0030:
SQL is authored as grammar within the unified grammar tree
(ADR-0024) and parsed by the existing walker, **not** a
separate batch parser — so SQL gets the same completion /
highlighting / hints as the DSL. ADR-0001's `sqlparser-rs`
reservation is superseded. Implementation is phased and
pending.)*
*(Progress: the advanced-mode SQL surface is authored as grammar
within the unified grammar tree (ADR-0030 / ADR-0024) and parsed by
the existing walker — **not** a separate batch parser — so SQL gets
the same completion / highlighting / hints as the DSL (ADR-0001's
`sqlparser-rs` reservation is superseded). Implemented so far: full
`SELECT` (ADR-0032), `INSERT` / `UPDATE` / `DELETE` (ADR-0033), and
`CREATE TABLE` (ADR-0035 sub-phase 4a, 2026-05-25 — columns + types +
`NOT NULL` / `UNIQUE` / `PRIMARY KEY` + `IF NOT EXISTS`, executed
structurally). Remaining DDL — `CREATE TABLE` constraints (4a.2),
FK (4b), `DROP TABLE` (4c), indexes (4d), `ALTER TABLE` (4e4h) — is
phased per ADR-0035 §13.)*
- [ ] **Q2** Non-standard syntax rejected with a clear message
pointing at the supported subset.
*(Design done — ADR-0030 §8: out-of-subset statements are