Write the restaurant and menu migrations
Create migration 0001 with the six tables, their keys and the two indexes, then apply and roll it back.
Ticket
Build the initial MySQL schema for restaurants and their menus. The database must represent restaurants, menu items, item sizes and prices, option groups, options, and option prices. Enforce relationships and pricing rules in the database so invalid menu data cannot enter the system.
This is the first implementation step of Week 2. You have completed the lessons on InnoDB keys and indexes and on golang-migrate. Every later step reads or writes these tables, so the schema and its migration history must be reliable from the beginning.
Acceptance criteria
- The
backend/db/migrationsdirectory contains at least one migration, and every migration version has both a matching.up.sqlfile and.down.sqlfile. - Applying the migrations creates all six menu tables with the required columns:
restaurants:id,name,lat,lng,is_activeitems:id,restaurant_id,name,sortitem_sizes:item_id,size,price_paiseoption_groups:id,item_id,name,min_select,max_selectoptions:id,group_id,name,in_stockoption_prices:option_id,size,price_paise
- Running every down migration in reverse order leaves no tables. Running the up migrations again successfully recreates at least the six menu tables.
- Foreign key constraints reject child rows that reference missing parents. This applies to items without restaurants, item sizes and option groups without items, options without groups, and option prices without options. Deleting a restaurant that still has items must also fail.
- Each item has at most one price for a given size, and each option has at most one price for a given size. Duplicate
(item_id, size)and(option_id, size)combinations must be rejected, including duplicate*prices. - Negative
price_paisevalues are rejected for both item sizes and option prices. - New rows receive these defaults when the values are omitted:
restaurants.is_active = 1,items.sort = 0,option_groups.min_select = 0,option_groups.max_select = 1, andoptions.in_stock = 1. - MySQL can use an index for a restaurant latitude and longitude range query.
EXPLAINmust report a non-emptypossible_keysvalue forrestaurants.
Constraints
- Enforce parent-child relationships with database foreign keys, not application checks alone.
- Do not allow a restaurant to be deleted while it still has items. Do not bypass this dependency with cascading deletion.
- Prevent duplicate prices for the same item or option and size, including the
*size. - Reject negative prices.
Design direction
A layout that works well is to keep your migrations in backend/db/migrations, with one migration per logical change and matching NNNN_name.up.sql and NNNN_name.down.sql files. In the up migration, create parent tables before their children. In the down migration, reverse that dependency order so child tables are removed first.
Choose column types to fit the values: integer identifiers, coordinates with six decimal places, and money stored as integer paise. Think about where composite primary keys provide both uniqueness and useful indexes. For the nearby restaurant query, consider which composite index supports the latitude and longitude range and why the order of its columns matters.
There are trade-offs worth considering. A composite key can enforce uniqueness while also supporting foreign-key lookups, but its usefulness depends on the leading column. Restrictive foreign keys protect menu data from accidental deletion, while requiring you to manage dependencies explicitly. Any migration layout that satisfies the acceptance criteria passes the tests.
How to check your work
Run these commands from the project setup:
make upmake test W=W2B1
A passing make test run means all W2B1 checks passed; when a check fails, read the reported assertion to identify the database behaviour or migration that 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.
Database schema and migrations
3 items
- 2.1MySQL tables, keys and InnoDB indexesnot completed
- 2.2Versioned migrations with golang-migratenot completed
- 2.3Write the restaurant and menu migrationsnot completed
Up next · Module 2 · 4 items · about 2 h 45 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Add paired up and down migration files for every version.
- Create all six menu tables and their required columns.
- Enforce foreign keys and unique prices per size.
- Reject negative prices and apply the documented defaults.
- Verify complete rollback and successful recreation.
- Confirm the nearby restaurant query can use an index.
Commands
cd backend && make upcd backend && make test W=W2B1Tests
Every migration version has both an up migration and a down migration.
Input The migration files present in
backend/db/migrations.Expected At least one migration exists, and every version has both its
.up.sqland.down.sqlfile.TestW2B1_EveryMigrationHasAnUpAndADownFileinbackend/e2e/w2_schema_test.go