feat(dbml): @postgres/@sqlite dialect directives (#19) #27

Merged
warkanum merged 1 commits from issue-19-dbml-directives into master 2026-09-08 14:19:03 +00:00
Owner

Implements #19.

What

Adds parseable @<namespace>[(<target>)]: <args> directives that can be embedded in DBML files to carry dialect-specific features (partitioning, WITHOUT ROWID, tablespaces, index storage parameters, identity columns, collations, …). Directives are:

  • stored losslessly on each model object's Metadata,
  • preserved unchanged through a DBML → model → DBML round-trip (idempotent output),
  • translated to SQL only by the writer for the matching dialect (@postgres: never appears in SQLite output and vice-versa).

Grammar & locations

@<namespace>[(<column>)]: <args> — namespace ^[a-z][a-z0-9_]*$, args verbatim, key = lowercased first token.

Position Attaches to
Before first Table { database
Table body, no target table
Table body, (col) target that column
Inside indexes { } the preceding index entry

Repeatable by default; catalog "singleton" keys error on a second occurrence at the same location. All errors are line-numbered.

Translation

PostgreSQL: PARTITION BY / INHERITS / WITH / TABLESPACE (table), STORAGE / COMPRESSION / GENERATED … AS IDENTITY (column), WITH / TABLESPACE (index) — hooked into both CREATE TABLE paths and both index-generation paths.

SQLite: WITHOUT ROWID / STRICT (table options), COLLATE (column).

Strict mode

New --strict-directives flag (ReaderOptions/WriterOptions): reader errors on unknown namespace/key; each writer errors on an untranslatable key for its own dialect and always ignores other dialects.

Also fixed

A preexisting DBML parser bug where an indexes { } closing brace was mistaken for end-of-table, dropping any trailing table-level content (Note:, and now directives).

Docs & tests

  • docs/DBML_DIRECTIVES.md + sections in the DBML reader/writer and pgsql/sqlite writer READMEs.
  • New directives_test.go in pkg/models, pkg/readers/dbml, pkg/writers/{dbml,pgsql,sqlite}.
  • go build, go test ./..., make lint all pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ss2MY5J11cRGwEz86ZXk7d

Implements #19. ## What Adds parseable `@<namespace>[(<target>)]: <args>` directives that can be embedded in DBML files to carry dialect-specific features (partitioning, `WITHOUT ROWID`, tablespaces, index storage parameters, identity columns, collations, …). Directives are: - stored losslessly on each model object's `Metadata`, - preserved unchanged through a `DBML → model → DBML` round-trip (idempotent output), - translated to SQL **only** by the writer for the matching dialect (`@postgres:` never appears in SQLite output and vice-versa). ## Grammar & locations `@<namespace>[(<column>)]: <args>` — namespace `^[a-z][a-z0-9_]*$`, args verbatim, key = lowercased first token. | Position | Attaches to | |----------|-------------| | Before first `Table {` | database | | Table body, no target | table | | Table body, `(col)` target | that column | | Inside `indexes { }` | the preceding index entry | Repeatable by default; catalog "singleton" keys error on a second occurrence at the same location. All errors are line-numbered. ## Translation **PostgreSQL:** `PARTITION BY` / `INHERITS` / `WITH` / `TABLESPACE` (table), `STORAGE` / `COMPRESSION` / `GENERATED … AS IDENTITY` (column), `WITH` / `TABLESPACE` (index) — hooked into both CREATE TABLE paths and both index-generation paths. **SQLite:** `WITHOUT ROWID` / `STRICT` (table options), `COLLATE` (column). ## Strict mode New `--strict-directives` flag (`ReaderOptions`/`WriterOptions`): reader errors on unknown namespace/key; each writer errors on an untranslatable key for its own dialect and always ignores other dialects. ## Also fixed A preexisting DBML parser bug where an `indexes { }` closing brace was mistaken for end-of-table, dropping any trailing table-level content (`Note:`, and now directives). ## Docs & tests - `docs/DBML_DIRECTIVES.md` + sections in the DBML reader/writer and pgsql/sqlite writer READMEs. - New `directives_test.go` in `pkg/models`, `pkg/readers/dbml`, `pkg/writers/{dbml,pgsql,sqlite}`. - `go build`, `go test ./...`, `make lint` all pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Ss2MY5J11cRGwEz86ZXk7d
warkanum added 1 commit 2026-09-08 14:18:38 +00:00
Add parseable `@<namespace>[(<target>)]: <args>` directives embedded in DBML.
They are stored losslessly on each object's Metadata, round-trip unchanged
through the DBML writer, and are translated to SQL only by the writer for the
matching dialect.

- models: Directive type + catalog; Metadata map added to Column and Index
- dbml reader: parse and attach directives at database/table/column/index
  level; line-numbered errors; repeatable by default with singleton duplicate
  detection. Fixes a preexisting bug where an `indexes {}` closing brace ended
  the table early, dropping trailing Note: and directive lines.
- dbml writer: re-emit directives at their location; idempotent output
- pgsql writer: PARTITION BY / INHERITS / WITH / TABLESPACE (table),
  STORAGE / COMPRESSION / identity (column), WITH / TABLESPACE (index)
- sqlite writer: WITHOUT ROWID / STRICT (table), COLLATE (column)
- --strict-directives flag on ReaderOptions and WriterOptions
- docs/DBML_DIRECTIVES.md + reader/writer READMEs

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ss2MY5J11cRGwEz86ZXk7d
warkanum merged commit 9e9a17d578 into master 2026-09-08 14:19:03 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: wdevs/relspecgo#27