Repository navigation
Conversation
`hanami generate struct` generates structs inheriting from the app's or slice's `DB::Struct` base class, but did not create that class when it was missing (e.g. in an app generated with `--skip-db` or a slice generated before the db layer was added), leaving the new struct referencing an undefined constant. Create `app/db/struct.rb` (and, for a slice, `slices/<slice>/db/struct.rb`) when it does not exist yet, matching what `hanami new` and `hanami generate slice` produce, as `generate part` already does for the base part. Fixes hanami#450
anyingiit
force-pushed
the
generate-struct-base-class
branch
from
October 22, 2026 20:32
92b8e64 to
dc2e3c7
Compare
Member
|
See #464 (comment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
hanami generate structgenerates a struct inheriting from the app's or slice'sDB::Structbase class, but it didn't create that base class when it was missing, so the new struct referenced an undefined constant. This happens for example in an app generated with--skip-db, or a slice created before the db layer was added (#450).This creates the base class when it doesn't exist yet, in the same way
generate partalready creates the base part:app/db/struct.rb(Test::DB::Struct < Hanami::DB::Struct, withrequire "hanami/db/struct"), identical to whathanami newgeneratesslices/<slice>/db/struct.rb(Main::DB::Struct < Test::DB::Struct), identical to whathanami generate slicegeneratesExisting base classes are left untouched.
Two notes:
require "hanami/db/struct"still needs hanami-db added. That's no worse than before, when the struct referenced an undefined constant.generate part, the app base class path is relative to the current directory. So running the generator from inside a slice directory (where the slice is detected from the cwd) would put it under that slice's folder. I kept the behaviour consistent withgenerate partrather than changing it in one generator only. Happy to fix both if you'd like.I kept this to structs as the issue asks.
generate relationandgenerate repohave the same gap withDB::Relation/DB::Repo. If you'd like, I can extend this PR to them or do it in a follow-up.Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to help projects with open good-first-issues / help-wanted issues. The change was prepared with Claude Code and checked as listed below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it, please feel free to close it, no hard feelings at all 🙂
Related issue
Fixes #450
Checklist
spec/unit/hanami/cli/commands/app/generate/struct_spec.rb(app and slice, with and without existing base classes). The 2 "without base struct" examples fail before the change and pass after; the struct spec file passes in full (11 examples)spec/unitgives the same results before and after the change (locally, without MySQL/Postgres)CHANGELOG.mdentry under Unreleased