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_eventsandoutboxwith every column checked by the schema test. The existinguserstable 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,
statusPLACEDandtotal_paise100, while omittingversion,rider_idandcreated_at, producesversion = 1,rider_id IS NULLand a non-nullcreated_at. - Inserting an
order_eventsrow withevent_idsame-event-idsucceeds for the first order. Inserting another row with that sameevent_idfor a different order fails. - Inserting an
order_linesrow withorder_id999999fails. Inserting anorder_eventsrow withevent_idorphanandorder_id999999also fails. - Inserting an
outboxrow with topicorder-events, key1and JSON payload{"order_id":1}, while omittingsent_atandcreated_at, producessent_at IS NULLand a non-nullcreated_at. EXPLAINfinds a usable index for each of these queries:- Customer order history: filter
user_id = 7, use(created_at, id) < ('2026-01-01', 99), order bycreated_at DESC, id DESC, limit20. - Order event history: filter
order_id = 7, order byid. - Unsent outbox polling: filter
sent_at IS NULL, order byid, limit100. - Restaurant order filtering: filter
restaurant_id = 3andstatus = 'PLACED'.
- Customer order history: filter
Constraints
- Use migration
0006and the table and column names checked by the schema test. - A new order must default to
version = 1,rider_id IS NULLand a non-nullcreated_at. order_events.event_idmust be unique across the entire table.order_lines.order_idandorder_events.order_idmust reference existingorders.idvalues.- A new outbox row must default to
sent_at IS NULLand a non-nullcreated_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:
cd backend && make up
cd backend && make test W=W3B3Read 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.
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.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Create migration 0006 for the four order-related tables.
- Add every required column to the tables.
- Set the required order defaults.
- Enforce unique event IDs and order references.
- Make new outbox rows unsent by default.
- Verify all four query patterns with
EXPLAIN.
Commands
cd backend && make upcd backend && make test W=W3B3Tests
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_eventsandoutbox.Expected Every listed column exists in its corresponding table.
TestW3B3_MigrationsCreateTheOrderTablesinbackend/e2e/w3_schema_test.go