Skip to content

fix(generator): driver-aware Atlas HCL, GraphQL-style REST bodies, invalid ids - #15

Merged
fadymondy merged 2 commits into
mainfrom
fix/driver-aware-atlas-and-rest-bodies
Oct 13, 2026
Merged

fadymondy merged 2 commits into
mainfrom
fix/driver-aware-atlas-and-rest-bodies

Conversation

@fadymondy

Copy link
Copy Markdown
Contributor

Fixes three of the framework bugs the D3 worker left out of scope in #14.
Depends on togo-framework/togo#1 and togo-framework/orm#2; pairs with togo-framework/create-togo-app#8.

Atlas table.hcl.tmpl is driver-aware

  • Renders MySQL, Postgres or SQLite types and defaults for the driver resolved the same way as fix(make): write resource SQL for the project's database driver (#11) #14 (DB_DRIVER → togo.yaml database.driver → sqlite).
  • MySQL: sql("char(36)") + (uuid()), datetime(6) + CURRENT_TIMESTAMP(6).
  • SQLite: text, lower(hex(randomblob(16))), datetime('now').
  • Postgres output is unchanged.
  • Atlas only applies HCL whose schema matches the database the URL connects to, so the table uses the schema declared in the project's db/atlas/schema/schema.hcl. If there isn't one it falls back to public, the app database, or main. The create-togo-app PR makes that file per driver.
  • make:migration writes the HCL again; since edd8532 it had been a silent no-op. togo migrate doesn't depend on it.

REST bodies match GraphQL

  • validate:"required" is dropped from create/update bodies, so 0, false and "" are accepted. A non-null field is still required as a key (Huma's default).
  • Nullable fields are required:"false", so the key can be omitted.

Invalid input isn't a 500

Handlers map orm.ErrInvalidInput (orm PR) to:

Case REST GraphQL
Invalid path id (GET/PUT/DELETE) 404 null / false
Invalid body value 422 BAD_USER_INPUT

All use Huma's standard problem+json envelope.

Tests

  • Goldens for table.hcl on sqlite/postgres/mysql.
  • TestAtlasTableHasNoPostgresSyntax, TestAtlasSchema, TestRestHandlerBodyTags, TestResolverMapsInvalidInput.
  • The golden test now strips \r, so it passes on CRLF checkouts too.

Verified for real

On Docker Postgres 16, MySQL 8.4 and SQLite, using a CLI built from this branch with local togo/orm, I ran togo new → make:resource with every field type → make:migration → generate → go build → migrate → REST + GraphQL CRUD.

  • Create/update with 0/false/"" and the nullable keys omitted works on every driver.
  • A missing required key gives 422.
  • Invalid uuid on Postgres: 404 for a path id, 422 / BAD_USER_INPUT for a body value.
  • atlas schema apply --dry-run shows no drift for the generated table on all three drivers.

Notes

  • Release order: togo → orm → cli. Generated apps import orm.ErrInvalidInput, so cli must not ship before that orm release. Bump the create-togo-app pin after its PR is released.
  • atlas.hcl reads DATABASE_URL. For MySQL and SQLite that is a Go DSN, not an Atlas URL, so pass --url until that is aligned. Not changed here.
  • Found during verification, out of scope and not fixed here:
    • An int32? field doesn't compile on SQLite: sqlc gives *int64, the transform expects *int32.
    • On MySQL the auth and settings plugins fail their migrations: a TEXT default on roles and a TEXT key on scope.
    • DELETE of an unknown (well-formed) id stays idempotent: 204 / true.

…valid ids

- table.hcl.tmpl renders MySQL, Postgres or SQLite types and uses the schema declared in db/atlas/schema/schema.hcl (falling back to public / the app database / main). make:migration writes it again; it was a silent no-op.
- REST create/update bodies no longer use validate:"required": a non-null field is required as a key but accepts 0/false/"", and nullable fields are optional (required:"false"), matching the GraphQL input.
- Values the database can't parse (orm.ErrInvalidInput, e.g. a malformed uuid on Postgres) are 404 for path ids and 422 for body values in REST; null/false or BAD_USER_INPUT in GraphQL, instead of 500.
@fadymondy
fadymondy merged commit 8ab0cdf into main Oct 13, 2026
3 checks passed
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