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/migrations directory contains at least one migration, and every migration version has both a matching .up.sql file and .down.sql file.
  • Applying the migrations creates all six menu tables with the required columns:
    • restaurants: id, name, lat, lng, is_active
    • items: id, restaurant_id, name, sort
    • item_sizes: item_id, size, price_paise
    • option_groups: id, item_id, name, min_select, max_select
    • options: id, group_id, name, in_stock
    • option_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_paise values 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, and options.in_stock = 1.
  • MySQL can use an index for a restaurant latitude and longitude range query. EXPLAIN must report a non-empty possible_keys value for restaurants.

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 up
  • make 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.

Finished Exercise 2.3?

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

SQL queries and data access

Up next · Module 2 · 4 items · about 2 h 45 min

Start module 2

Implementation

Run locally

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

  1. Add paired up and down migration files for every version.
  2. Create all six menu tables and their required columns.
  3. Enforce foreign keys and unique prices per size.
  4. Reject negative prices and apply the documented defaults.
  5. Verify complete rollback and successful recreation.
  6. Confirm the nearby restaurant query can use an index.

Commands

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

Tests

  1. 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.sql and .down.sql file.

    TestW2B1_EveryMigrationHasAnUpAndADownFile in backend/e2e/w2_schema_test.go