Skip to content

feat(ddl): CREATE SEQUENCE / DROP SEQUENCE + nextval/currval/setval helpers - #163

Merged
productdevbook merged 1 commit into
mainfrom
worktree-agent-a3e4e8ca24aaadd25
May 19, 2026
Merged

feat(ddl): CREATE SEQUENCE / DROP SEQUENCE + nextval/currval/setval helpers#163
productdevbook merged 1 commit into
mainfrom
worktree-agent-a3e4e8ca24aaadd25

Conversation

@productdevbook

Copy link
Copy Markdown
Owner

Summary

  • First-class CREATE SEQUENCE / DROP SEQUENCE DDL on PG and MSSQL — a free-standing monotonic integer source independent of any particular table's lifecycle (auto-increment/IDENTITY columns are table-scoped; sequences are not). Useful for advisory IDs, batch numbers, cross-table counters.
  • PG-only nextval('seq') / currval('seq') / setval('seq', n[, is_called]) runtime helpers wired into eb.ts and exposed as flat sumak exports.
  • MySQL and SQLite refuse both surfaces via the new SEQUENCES / SEQUENCE_FNS feature flags; the runtime gate fires inside BasePrinter.printFunctionCall so the refusal lands at print time with a clear error rather than driver-level parse failure.

Surface (new)

  • db.schema.createSequence(name [, schema])CreateSequenceBuilder with .ifNotExists() .dataType(...) .increment(...) .minValue(...) .noMinValue() .maxValue(...) .noMaxValue() .start(...) .cache(...) .cycle() .noCycle() .ownedBy(table, column) .ownedByNone().
  • db.schema.dropSequence(name [, schema])DropSequenceBuilder with .ifExists() .cascade().
  • Flat exports: nextval(seqName), currval(seqName), setval(seqName, value [, isCalled]). Sequence names are validated against a SQL-identifier regex; numeric value must be a finite integer.

Dialect divergence (handled at print time)

Feature PG MSSQL MySQL SQLite
CREATE SEQUENCE yes yes refused refused
IF NOT EXISTS yes refused (pointer at wrapper pattern)
OWNED BY … yes refused (no analogue)
DROP SEQUENCE … CASCADE yes refused (sequences aren't in the ref graph)
nextval/currval/setval yes refused (use NEXT VALUE FOR) refused refused

Test plan

  • pnpm fmt && pnpm lint && pnpm typecheck && pnpm vitest run all green (2286 passed, 58 skipped — +38 from new file).
  • New file test/ddl/sequences.test.ts with 39 tests covering builder DSL, dialect emit shapes, refusal paths, and a PGlite roundtrip (create → nextval × 2 → currval → setval → nextval → drop).
  • gh pr checks <N> --watch green.

Tradeoffs

  • MSSQL's NEXT VALUE FOR <seq> is intentionally not supported in this cut — it's a sequence-value expression (not a function call), so cleanly bridging it needs a dedicated AST node and printer pass. Callers who need MSSQL sequence access today can fall back to unsafeRawExpr("NEXT VALUE FOR " + quotedName) when the name is trusted.
  • Sequence names land as SQL string literals in the emitted SQL (nextval('order_no_seq')), not bound — PG's plan cache treats them that way, and binding would force per-call replan. The builder rejects names that aren't bare SQL identifiers to keep injection out of the literal slot.

🤖 Generated with Claude Code

…elpers

PG and MSSQL get first-class sequence DDL — a free-standing monotonic
integer source useful for advisory IDs, batch numbers, or counters that
need to outlive a particular table. PG also gets the runtime
`nextval('seq')` / `currval('seq')` / `setval('seq', n[, is_called])`
function-shape helpers; MSSQL's `NEXT VALUE FOR <seq>` grammar isn't a
function call and needs its own AST node (future work).

MySQL and SQLite refuse both surfaces via the new `SEQUENCES` /
`SEQUENCE_FNS` feature flags rather than emitting a statement the
engine will reject. The runtime gate fires inside `BasePrinter.printFunctionCall`
so the refusal lands at print time, pointing at the call site.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@productdevbook
productdevbook force-pushed the worktree-agent-a3e4e8ca24aaadd25 branch from a7baf17 to f25825e Compare May 19, 2026 10:52
@productdevbook
productdevbook merged commit 2c4a512 into main May 19, 2026
1 check passed
@productdevbook
productdevbook deleted the worktree-agent-a3e4e8ca24aaadd25 branch May 19, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant