Generate the restaurant and menu queries
Write the restaurant and menu queries in SQL, configure sqlc, and commit the generated internal/db package.
Ticket
Establish a type-safe Go database access layer using sqlc and the existing MySQL migrations. Your query definitions and generated code will provide a consistent way for the application to access restaurant and menu data without maintaining handwritten database models and scanning logic.
This work builds on the schema from W2B1. W2B3 through W2B7 depend on these queries, so generation must be reproducible and the committed generated code must stay consistent with the SQL definitions.
Acceptance criteria
backend/sqlc.yamlexists, andsqlc compilesucceeds against the existing migration schema.sqlc diffsucceeds, confirming that the committed generated code matches the current query definitions and configuration.
Constraints
- No runtime query-count, pagination, or request-size limits are imposed by the W2B2 checks.
- Keep the existing migrations as the schema source rather than maintaining a separate schema definition.
Design direction
A layout that works well is backend/sqlc.yaml for sqlc configuration, backend/db/queries/ for SQL files grouped by area, and backend/internal/db/ for generated code in its own package. Treat generated files as output that nobody edits manually. Keep the application-facing database boundary small: let services depend on an interface containing the queries they use rather than on the generated Queries struct. Any layout passes the tests as long as sqlc compiles and generated code stays up to date.
Think about a few trade-offs:
- Select only the columns callers need to avoid unnecessary database work and unnecessarily broad result structures.
- Use slice-based ID filters for batch lookups instead of making one database query per parent record.
- Keep the interface narrow enough to make service code easy to test without turning it into a second copy of the generated query API.
How to check your work
Run these commands from the repository root:
make upmake test W=W2B2
A passing make test reports the W2B2 checks as passing; when a check fails, read its output to identify whether sqlc compilation or generated-code consistency needs attention.
No hints for this exercise.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Configure sqlc to use the existing MySQL migrations.
- Organize SQL query files by area.
- Generate the Go database package.
- Keep generated files out of manual edits.
- Verify sqlc compilation succeeds.
- Verify generated code has no differences.
Commands
cd backend && make upcd backend && make test W=W2B2Tests
Confirms that the sqlc configuration and query definitions compile against the existing migration schema.
Input The test checks that
backend/sqlc.yamlexists, then runssqlc compilewithbackend/as the working directory.Expected The configuration file exists and
sqlc compileexits successfully.TestW2B2_QueriesCompileAgainstTheMigrationsinbackend/e2e/w2_data_test.go