Commit 00d1e4e
feat(ddl): DEFAULT clause, DROP TABLE/INDEX, ALTER TABLE (#86)
* feat(ddl): DEFAULT clause for CREATE TABLE columns
Honour `DEFAULT <literal>` on column definitions. Literal expressions
only — text, integer, real, boolean, NULL, and unary +/- on numerics.
Function calls and other non-literal expressions are rejected at
CREATE TABLE time so users see the limit upfront.
Default fires only when the column is omitted from INSERT (matches
SQLite — explicit NULL is preserved as NULL). Persists through save
and reopen via `table_to_create_sql` emitting the DEFAULT clause and
`parse_create_sql` propagating it back into Column.
Refactors `CreateQuery::new`'s per-column body into a free
`parse_one_column` helper so ALTER TABLE ADD COLUMN can reuse the
same column-shape parsing in a follow-up commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(ddl): DROP TABLE and DROP INDEX
Mirror SQLite's DROP semantics on the in-memory engine:
- DROP TABLE [IF EXISTS] <name>; — single target, rejects the reserved
catalog name `sqlrite_master`. The dropped table's
secondary/HNSW/FTS indexes ride along with the Table struct.
- DROP INDEX [IF EXISTS] <name>; — single target. Walks every table
looking across all three index families. Refuses to drop
auto-indexes (`sqlrite_autoindex_*` from PK / UNIQUE columns) —
same rule SQLite enforces.
The full-rebuild-on-save pager naturally cascades drops: the next
`save_database` call regenerates `sqlrite_master` from current state
and simply doesn't write rows for the dropped objects. Pages
previously occupied become orphans on disk (no free-list yet — file
size doesn't shrink until a future VACUUM lands).
Replaces the existing `process_command_unsupported_statement_test`
which used DROP TABLE as the canary; switched to CREATE VIEW since
DROP TABLE now executes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* feat(ddl): ALTER TABLE — RENAME / ADD COLUMN / DROP COLUMN
Implement the four SQLite-style ALTER TABLE sub-operations, one per
statement:
- ALTER TABLE [IF EXISTS] <t> RENAME TO <new>;
- ALTER TABLE [IF EXISTS] <t> RENAME COLUMN <old> TO <new>;
- ALTER TABLE [IF EXISTS] <t> ADD COLUMN <coldef>;
- ALTER TABLE [IF EXISTS] <t> DROP COLUMN <col>;
RENAME TO updates `tb_name`, every secondary index's `table_name`,
and any auto-index whose name embedded the old table name. RENAME
COLUMN re-keys row storage, fixes up the `primary_key` pointer if
the renamed column was the PK, and updates dependent indexes
(secondary / HNSW / FTS) including auto-index names.
ADD COLUMN reuses the new `parse_one_column` helper from the parser
refactor in the DEFAULT-clause commit. Allows NOT NULL only when a
DEFAULT is supplied (matches SQLite — without DEFAULT there's no
backfill source for existing rows). Rejects PRIMARY KEY / UNIQUE on
the added column; both would need backfill+uniqueness against
existing rows. With a DEFAULT, every existing rowid gets backfilled
in place so the new column reads consistently from the start.
DROP COLUMN refuses the PK column and refuses to leave the table
columnless; cascades through every dependent index family.
Docs: rewrote the schema-modification section of supported-sql.md
to enumerate the new surface (ALTER TABLE, DROP TABLE, DROP INDEX),
the DEFAULT clause expansion to CREATE TABLE, and the residual
not-yet-supported gaps.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* fix(ddl): JSON validation on DEFAULT + DROP TABLE rollback test
Two findings from review of feat/alter-drop-tables:
- JSON DEFAULT validation gap: ALTER TABLE ADD COLUMN ... JSON DEFAULT
'<garbage>' would silently backfill every existing row with invalid
JSON because insert_row's per-row validation is bypassed during the
add_column backfill path. Tighten eval_literal_default to parse JSON
literals at CREATE TABLE / ALTER TABLE time so the check fires
before any rows are written.
- Add a transaction-rollback test for DROP TABLE to lock down the
Database::deep_clone snapshot path. The other ALTER ops are covered
via the snapshot semantics implicitly, but DROP TABLE is the most
destructive new statement and warrants explicit coverage.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 80664b8 commit 00d1e4e
6 files changed
Lines changed: 1784 additions & 118 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| |||
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
44 | | - | |
| 46 | + | |
| 47 | + | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
48 | 51 | | |
49 | | - | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| |||
208 | 210 | | |
209 | 211 | | |
210 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
211 | 279 | | |
212 | 280 | | |
213 | 281 | | |
| |||
405 | 473 | | |
406 | 474 | | |
407 | 475 | | |
408 | | - | |
409 | | - | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
410 | 479 | | |
411 | 480 | | |
412 | | - | |
| 481 | + | |
413 | 482 | | |
414 | 483 | | |
415 | 484 | | |
| |||
0 commit comments