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/seedwith a reachable database completes successfully and creates exactly 50 restaurants with IDs1through50. - 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
77and nameLeftover, along with its associated item, must be gone after seeding. Exactly one item must remain. - Restaurant
1has item1, namedFarmhouse pizza. - The Farmhouse pizza item has exactly three size prices: R =
19900, M =39900, and L =59900paise. - Item
1has the Crust group with ID1,min_select = 1, andmax_select = 1, and the Toppings group with ID2,min_select = 0, andmax_select = 5. - The Crust group has option
1,Classic hand tossed, in stock, with exactly one*price of0paise. - The Crust group has option
2,Cheese burst, in stock, with exactly two prices: M =9900and L =12900paise. It has no R or*price. - The Toppings group has exactly five options, all in stock, with one
*price each: option3,Olives, at3500paise; option4,Jalapeno, at3500paise; option5,Paneer, at6000paise; option6,Mushroom, at4500paise; and option7,Red paprika, at4000paise. - 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
1through50. - 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.
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.
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
Up next · Module 3 · 3 items · about 2 h 30 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Create a seed command that connects to MySQL and handles failures.
- Replace existing restaurant and menu data safely.
- Generate 50 active restaurants with deterministic locations.
- Seed the Farmhouse pizza item and its three sizes.
- Seed both option groups and all seven options with their prices.
- Verify repeatability and database replacement with the W2B3 tests.
Commands
cd backend && make upcd backend && make test W=W2B3Tests
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/seedagainst 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_SeedCreatesFiftyActiveRestaurantsWithinFiveKminbackend/e2e/w2_data_test.go