Migration 0006: order tables

You add migration 0006 with the orders, order_lines, order_events and outbox tables used to store orders and their events. The schema enforces global event ID uniqueness, order references, required defaults and indexed query paths.

Ticket

Create migration 0006 with the database schema for orders, order lines, order events and the outbox. The schema must preserve the relationships between an order and its dependent records, while supporting the queries used by later steps.

The product needs these tables before checkout can persist an order and its first event safely. W3B4 depends on this schema to write an order, its lines, its first event and an outbox row in one transaction. Later exercises use the same records to track order transitions and publish events.

Acceptance criteria

  • Applying the migrations creates orders, order_lines, order_events and outbox with every column checked by the schema test. The existing users table also retains every column checked by that test. The order-related columns include:
    • orders: id, user_id, restaurant_id, rider_id, status, version, total_paise, created_at.
    • order_lines: id, order_id, item_id, config_json, unit_paise, qty.
    • order_events: id, event_id, order_id, from_status, to_status, actor, created_at.
    • outbox: id, topic, key_, payload, created_at, sent_at.
  • Inserting an order with a valid user and restaurant, status PLACED and total_paise 100, while omitting version, rider_id and created_at, produces version = 1, rider_id IS NULL and a non-null created_at.
  • Inserting an order_events row with event_id same-event-id succeeds for the first order. Inserting another row with that same event_id for a different order fails.
  • Inserting an order_lines row with order_id 999999 fails. Inserting an order_events row with event_id orphan and order_id 999999 also fails.
  • Inserting an outbox row with topic order-events, key 1 and JSON payload {"order_id":1}, while omitting sent_at and created_at, produces sent_at IS NULL and a non-null created_at.
  • EXPLAIN finds a usable index for each of these queries:
    • Customer order history: filter user_id = 7, use (created_at, id) < ('2026-01-01', 99), order by created_at DESC, id DESC, limit 20.
    • Order event history: filter order_id = 7, order by id.
    • Unsent outbox polling: filter sent_at IS NULL, order by id, limit 100.
    • Restaurant order filtering: filter restaurant_id = 3 and status = 'PLACED'.

Constraints

  • Use migration 0006 and the table and column names checked by the schema test.
  • A new order must default to version = 1, rider_id IS NULL and a non-null created_at.
  • order_events.event_id must be unique across the entire table.
  • order_lines.order_id and order_events.order_id must reference existing orders.id values.
  • A new outbox row must default to sent_at IS NULL and a non-null created_at.
  • All four tested query patterns must have a usable index according to EXPLAIN. The tests do not prescribe index names or exact column sequences.

Design direction

Keep the four tables in one migration pair. Make the dependency between orders and its dependent tables clear. Keep outbox independent of those foreign-key relationships. Use database migrations so the schema change can be reproduced.

Start index design from the actual queries rather than from the table names. Customer history combines an equality filter with a cursor condition and descending order. Restaurant filtering uses two equality conditions. Event history and outbox polling both filter before reading rows in ID order. A composite index may support more than one part of a query, but its column order affects which query conditions it can serve. Check the plans with EXPLAIN.

Use a primary key to identify each row. Use a foreign key where a dependent record must not exist without its order. Keep the global event identifier separate from the order relationship. An event ID reused for another order must still fail.

The version column gives later order updates a value they can check when concurrent transitions occur. The outbox stores committed records for a later relay, rather than requiring the order transaction to publish to Kafka itself. Any layout that meets the tested contract passes.

How to check your work

From the project root, run:

bash
cd backend && make up
cd backend && make test W=W3B3

Read the first failing assertion first. It identifies the schema behaviour that failed, and the associated database error or query plan points you toward the relevant table, constraint or index.

Finished Exercise 3.6?

Implementation

Run locally

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

  1. Create migration 0006 for the four order-related tables.
  2. Add every required column to the tables.
  3. Set the required order defaults.
  4. Enforce unique event IDs and order references.
  5. Make new outbox rows unsent by default.
  6. Verify all four query patterns with EXPLAIN.

Commands

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

Tests

  1. the migrated schema contains every required column in the checked tables.

    Input Apply the migrations to a scratch database, then inspect the columns of users, orders, order_lines, order_events and outbox.

    Expected Every listed column exists in its corresponding table.

    TestW3B3_MigrationsCreateTheOrderTables in backend/e2e/w3_schema_test.go