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.yaml exists, and sqlc compile succeeds against the existing migration schema.
  • sqlc diff succeeds, 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 up
  • make 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.

Finished Exercise 2.6?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Configure sqlc to use the existing MySQL migrations.
  2. Organize SQL query files by area.
  3. Generate the Go database package.
  4. Keep generated files out of manual edits.
  5. Verify sqlc compilation succeeds.
  6. Verify generated code has no differences.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=W2B2

Tests

  1. Confirms that the sqlc configuration and query definitions compile against the existing migration schema.

    Input The test checks that backend/sqlc.yaml exists, then runs sqlc compile with backend/ as the working directory.

    Expected The configuration file exists and sqlc compile exits successfully.

    TestW2B2_QueriesCompileAgainstTheMigrations in backend/e2e/w2_data_test.go