Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type-safe Go code generation from migration schema #911

Open
mfridman opened this issue Feb 17, 2025 · 0 comments
Open

feat: add type-safe Go code generation from migration schema #911

mfridman opened this issue Feb 17, 2025 · 0 comments
Labels

Comments

@mfridman
Copy link
Collaborator

mfridman commented Feb 17, 2025

Opening this issue in case this might be interesting to others. Feeback welcome.

Extend goose to support type-safe Go code generation from SQL queries using the schema information from migration files.

Note that it's very possible this feature is out-of-scope for the goose project. But at the very least I want to track this request to see where the community stands and whether it's worth pursuing (it's been brought up by a few folks).

goose generate --dir=./sql/migrations --queries=./sql/queries

The idea is to add the ability to read SQL queries from a directory alongside the existing migrations directory, where developers can define their SQL queries that'll be converted to type-safe Go code.

Example

./sql
├── migrations/          # Existing migrations directory
│   ├── 00001_create_users.sql
│   └── 00002_add_user_roles.sql
└── queries/               # New queries directory
    ├── users_read.sql     # Read-only operations
    └── users_write.sql    # Write operations

One property I'd like to have, either through file naming conventions or an opinionated interface, is the ability to separate read-only vs read/write queries, which has benefits like using read-only database replicas out of the box.

Motivation

  • goose already understands the database schema through migration files
  • By adding support for a queries directory, we can provide type-safe query generation while maintaining goose's simple, file-based approach
  • Reduces the need for separate tools in the development workflow
  • Keeps SQL queries close to the schema that defines them

Extras

  • I want some form of dynamic query building. There's a handful of extremely common (and dynamic) SQL boilerplate like WHERE predicates with ORDER BY
  • Support only sql/db and pgx/v5 for postgres

... will update this issue as this gets flushed out a bit more

Related Projects

  • sqlc (for reference on query syntax and code generation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant