Seed 50 restaurants and one full menu

Write an idempotent seed command: 50 restaurants within 5 km of Gurgaon and one Farmhouse pizza menu, the same data on every run.

Ticket

Build a seed utility that gives the development database a known, repeatable dataset. The restaurant and menu endpoints need predictable records for integration testing, and later exercises depend on fixed restaurant, item, group, and option IDs. Your seed command must replace existing data rather than append to it.

This builds on the database schema and query work from W2B1 and W2B2. Later steps run their tests against this dataset, and the menu pricing work relies on its fixed IDs and prices. Keep the dataset deterministic so those tests can make reliable assertions.

Acceptance criteria

  • Running go run ./cmd/seed with a reachable database completes successfully and creates exactly 50 restaurants with IDs 1 through 50.
  • All 50 restaurants are active, have distinct names, and are within 5 km of (28.46, 77.03).
  • Running the seed command twice produces identical data across the tables, excluding timestamp and datetime columns.
  • Seeding replaces existing data. A pre-existing restaurant with ID 77 and name Leftover, along with its associated item, must be gone after seeding. Exactly one item must remain.
  • Restaurant 1 has item 1, named Farmhouse pizza.
  • The Farmhouse pizza item has exactly three size prices: R = 19900, M = 39900, and L = 59900 paise.
  • Item 1 has the Crust group with ID 1, min_select = 1, and max_select = 1, and the Toppings group with ID 2, min_select = 0, and max_select = 5.
  • The Crust group has option 1, Classic hand tossed, in stock, with exactly one * price of 0 paise.
  • The Crust group has option 2, Cheese burst, in stock, with exactly two prices: M = 9900 and L = 12900 paise. It has no R or * price.
  • The Toppings group has exactly five options, all in stock, with one * price each: option 3, Olives, at 3500 paise; option 4, Jalapeno, at 3500 paise; option 5, Paneer, at 6000 paise; option 6, Mushroom, at 4500 paise; and option 7, Red paprika, at 4000 paise.
  • No menu items belong to restaurants other than restaurant 1.
  • Running the seed command with an unreachable database exits with a non-zero status.

Constraints

  • Create exactly 50 restaurants with explicit IDs from 1 through 50.
  • Keep every restaurant active and within 5 km of (28.46, 77.03).
  • Use deterministic restaurant data so repeated runs produce identical records, apart from timestamp and datetime columns.
  • Keep the menu data fixed: one Farmhouse pizza item, two option groups, and exactly seven options with the specified IDs and prices.
  • Replace the existing dataset rather than retaining pre-existing restaurant or menu rows.

Design direction

A layout that works well is a thin backend/cmd/seed package entry point that reads configuration, opens the database, and invokes a seed package. Keep the dataset, reset process, and insertions in backend/internal/seed. The command should handle configuration and connection failures, while the seed package should own the database changes.

Think carefully about three boundaries. First, MySQL session settings belong to one connection, so use a dedicated connection for foreign-key settings and table resets. Second, table resets happen before the insertion transaction because TRUNCATE causes an implicit commit; perform the inserts together in one transaction. Third, a fixed random seed makes generated coordinates repeatable, while fixed menu data keeps the pricing tests predictable. Any package or file layout passes the tests as long as the required behaviour holds.

How to check your work

From the backend directory, run make up to start the dependencies, then run make test W=W2B3 to execute this exercise's tests. Read the output of make test: each reported failure identifies a behaviour that does not yet meet the acceptance criteria.

Finished Exercise 2.7?

SQL queries and data access

4 items

  • 2.4database/sql: pools, contexts and timeoutsnot completed
  • 2.5sqlc: SQL first, Go generatednot completed
  • 2.6Generate the restaurant and menu queriesnot completed
  • 2.7Seed 50 restaurants and one full menunot completed

Listing API and pagination

Up next · Module 3 · 3 items · about 2 h 30 min

Start module 3

Implementation

Run locally

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

  1. Create a seed command that connects to MySQL and handles failures.
  2. Replace existing restaurant and menu data safely.
  3. Generate 50 active restaurants with deterministic locations.
  4. Seed the Farmhouse pizza item and its three sizes.
  5. Seed both option groups and all seven options with their prices.
  6. Verify repeatability and database replacement with the W2B3 tests.

Commands

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

Tests

  1. The seed command creates exactly 50 restaurants with IDs 1 through 50, distinct names, active status, and locations within 5 km of the specified centre.

    Input Run go run ./cmd/seed against a migrated scratch database.

    Expected 50 restaurants exist, all IDs 1 through 50 are present, all are active and within 5 km of (28.46, 77.03), and all 50 names are distinct.

    TestW2B3_SeedCreatesFiftyActiveRestaurantsWithinFiveKm in backend/e2e/w2_data_test.go