Database for testers¶
The UI and the API show you what the application says it stored. The database shows what it really stored, which is how many bugs are found: an order total that does not add up, stock that two services disagree on, an email that was never queued. This page shows how to get in, what is where, and which queries are worth running.
Every query on this page runs as the read-only tester login, so you can
copy them freely: none of them can change data.
Ways in¶
| Tool | Best for | Lightweight profile | Full profile |
|---|---|---|---|
| pgweb (browser) | Quick look, no installation | localhost:8081/db/ | localhost:8081/db/ |
| DBeaver (free) | Daily work, diagrams, exporting results | 127.0.0.1:15432 |
localhost:5432 |
| DataGrip (paid, JetBrains) | If you already use IntelliJ or PyCharm | 127.0.0.1:15432 |
localhost:5432 |
| psql (terminal) | Scripts, quick checks | 127.0.0.1:15432 |
localhost:5432 |
docker compose exec |
When nothing else is installed | see psql without installing anything | same |
The database is testdb on every profile. There are two logins:
| Login | Password | Can | Use it for |
|---|---|---|---|
tester |
tester |
read every table in every schema | exploring, checking results, everything on this page |
postgres |
postgres |
everything (superuser) | preparing test data, such as an expired coupon or an old order |
Start with tester. Switch to postgres only when you mean to change data, and
remember that you then bypass every rule the API enforces: a row the API could
never create is a test of your own making, not a bug in the application.
Only on your laptop
The public sites (awesome.byst.re, aitesters.byst.re) give no database access. Run a local profile to use this page.
Port 15432 is taken, or you want a different one
The lightweight profile publishes PostgreSQL on 127.0.0.1:15432 so that a
PostgreSQL of your own on 5432 does not get in its way. To move it, start
the stack with DB_HOST_PORT:
The port listens on 127.0.0.1 only: other machines on your network cannot
reach it. pgweb is served through the gateway on 8081.
Setting up a client¶
Open localhost:8081/db/: it is already
connected as tester, and cannot switch to another login.
- Rows shows a table; the filter bar above it builds a
WHEREfor you. - Structure, Indexes and Constraints show how a table is built (the constraints include the allowed status values).
- Query runs your own SQL; History keeps what you ran.
- Tables of every schema are listed on the left; write the schema in your
queries (
warehouse.shipments), except for the backend'spublictables. - Any statement that changes data is refused.
- Database → New Database Connection → PostgreSQL.
- Host
127.0.0.1, port15432(full profile:5432), databasetestdb. - User
tester, passwordtester. - Test Connection (on first use DBeaver offers to download the PostgreSQL driver: accept), then Finish.
Expand testdb → Schemas to see public, warehouse, payment,
notification and delivery. Right-click a schema → View Diagram draws
its tables and keys. SQL Editor → New SQL script opens an editor for the
queries below.
- + → Data Source → PostgreSQL.
- Host
127.0.0.1, port15432(full profile:5432), usertester, passwordtester, databasetestdb. - On the Schemas tab pick all five schemas (DataGrip shows only
publicby default). - Test Connection, then OK.
IntelliJ IDEA Ultimate and PyCharm Professional have the same tool in their Database window.
With psql installed (brew install libpq, apt install postgresql-client):
psql postgresql://tester:[email protected]:15432/testdb
\dn lists the schemas, \dt warehouse.* the tables of one schema,
\d orders one table, \x switches to one column per line for wide rows.
psql without installing anything¶
This logs in as the superuser inside the container. Type SET ROLE tester;
first if you only want to look.
What is where¶
The five services share one PostgreSQL database, but each owns one schema and writes only to its own (why: Data and databases).
| Schema | Owner | What it holds |
|---|---|---|
public |
backend | users, products, carts, orders, coupons, gift cards, loyalty points, VAT rates, the event feed, emails of the backend, the traffic monitor |
warehouse |
warehouse-service | the stock ledger and reservations, shipments and their tracking, returns, suppliers, purchase orders |
payment |
payment-service | card payments and refunds |
notification |
notification-service | commerce emails and user webhooks |
delivery |
delivery-service | courier deliveries and their events |
There are no foreign keys between schemas. The services connect their
data through plain ids and usernames: order_id, product_id and username
mean the same thing in every schema. That is what makes the cross-service
queries below possible, and also why the database itself cannot stop two
services from disagreeing.
Table diagrams¶
The main tables and how they connect. Columns are a selection; every column, type and rule is in the table reference.
erDiagram
orders ||--|{ order_items : contains
products ||--o{ order_items : "ordered as"
orders ||--o{ order_payments : "paid by"
orders ||--o{ order_refunds : "refunded by"
orders ||--o{ coupon_redemptions : uses
coupons ||--o{ coupon_redemptions : "redeemed in"
orders ||--o{ gift_card_transactions : "paid with"
gift_cards ||--o{ gift_card_transactions : "spent in"
orders {
bigint id PK
varchar username "owner (no FK)"
varchar status "PENDING PAID SHIPPED DELIVERED CANCELLED"
numeric subtotal_amount
numeric discount_amount
numeric total_amount "gross, incl. VAT"
numeric tax_amount
numeric gift_card_amount
numeric points_amount
uuid stock_reservation_id "warehouse reservation"
}
order_items {
bigint order_id FK
bigint product_id FK
int quantity
numeric price "unit price when ordered"
}
products {
bigint id PK
varchar name
numeric price
int stock_quantity "copy of the warehouse ledger"
}
order_payments {
bigint order_id FK
uuid payment_id "payment-service id"
varchar status
numeric amount
}
Loyalty points live in loyalty_accounts and loyalty_transactions
(keyed by username); VAT rates in vat_rates.
erDiagram
app_user ||--o{ user_entity_roles : "has role"
app_user ||--o{ refresh_token : "signs in with"
app_user ||--o{ password_reset_token : "resets with"
app_user ||--o{ mfa_credential : "protected by"
products ||--o{ cart_items : "in cart"
products ||--o{ product_reviews : "reviewed in"
products ||--o{ wishlist_items : "wished in"
app_user {
int id PK
varchar username UK
varchar email UK
}
user_entity_roles {
int user_entity_id FK
smallint roles "0 admin, 1 client, 2 warehouse"
}
cart_items {
varchar username
bigint product_id FK
int quantity
numeric price "price when added"
}
domain_events {
bigint sequence PK
varchar type "order.created, order.paid, ..."
bigint order_id
bigint feed_position "order other services read in"
}
Carts, reviews and wishlists point at the user by username, not by a
foreign key. domain_events has no keys to other tables: it is the event
feed that warehouse-, payment-, notification- and delivery-service read
(Event feed).
erDiagram
stock_items ||--o{ stock_movements : "changed by"
stock_reservations ||--|{ stock_reservation_lines : reserves
stock_items {
bigint product_id PK "backend product id"
int quantity "the stock that counts"
bigint ledger_version
}
stock_movements {
bigint product_id FK
varchar type "ORDER_DEDUCTED ORDER_RESTORED ..."
int delta
int quantity_after
bigint order_id
varchar actor
}
stock_reservations {
uuid reservation_id PK "orders.stock_reservation_id"
varchar status
bigint order_id
}
stock_reservation_lines {
uuid reservation_id FK
bigint product_id
int quantity
}
warehouse-service owns the stock: stock_items.quantity is the real stock,
and products.stock_quantity in the backend is a copy it pushes
(Inventory ownership).
erDiagram
warehouse_locations ||--o{ shipments : "ships from"
shipments ||--|{ shipment_lines : contains
shipments ||--o{ tracking_events : "tracked by"
return_requests ||--|{ return_items : contains
suppliers ||--o{ purchase_orders : supplies
purchase_orders ||--|{ purchase_order_lines : contains
purchase_orders ||--o{ purchase_receipts : "received in"
shipments {
bigint id PK
bigint order_id "backend order id"
varchar status "AWAITING_PICK ... DELIVERED"
varchar carrier "INPOST DPD DHL UPS"
varchar tracking_number
varchar location_code FK
}
tracking_events {
bigint shipment_id FK
varchar status
varchar location
}
return_requests {
bigint id PK
bigint order_id
varchar status
numeric refund_amount
}
erDiagram
payments ||--o{ refunds : "refunded by"
payments ||--o{ payment_idempotency_keys : "deduplicated by"
payments ||--o{ payment_outbox : "reported through"
payments {
uuid id PK
bigint order_id "backend order id"
varchar username
varchar status "REQUIRES_ACTION PROCESSING CAPTURED FAILED ..."
numeric amount
numeric refunded_amount
varchar card_brand
varchar card_last4 "never the full number"
varchar failure_code
}
refunds {
uuid id PK
uuid payment_id FK
bigint order_id
numeric amount
}
The backend keeps its own record of each payment result in
public.order_payments; comparing the two is a good check.
erDiagram
webhook_subscriptions ||--o{ webhook_deliveries : "delivered as"
webhook_inboxes ||--o{ webhook_inbox_requests : receives
notification_emails {
uuid id PK
varchar username
varchar template "PAYMENT_RECEIVED ORDER_SHIPPED ..."
varchar recipient_email
varchar status "QUEUED SENT FAILED"
int attempts
}
webhook_subscriptions {
bigint id PK
varchar username
varchar url
varchar events
bool active
}
webhook_deliveries {
uuid id PK
bigint subscription_id FK
varchar event_type
varchar status
int last_status_code
}
The backend's public schema still has webhook_* tables from before
notification-service took webhooks over; the live ones are here.
erDiagram
deliveries ||--o{ delivery_events : "moves through"
deliveries {
bigint id PK
bigint order_id "backend order id"
varchar username
text tracking_code
text status "BOOKED ... DELIVERED RETURNED CANCELLED"
int attempts
}
delivery_events {
bigint delivery_id FK
text status
text location
text actor "shop, courier, admin"
timestamptz occurred_at
}
Before you query: things that surprise people¶
- Times are UTC.
created_atand friends are stored in UTC; the UI shows your local time. An order placed at 10:00 in Warsaw in winter is stored as 09:00. - Money is gross. Prices and
total_amountinclude VAT;tax_amountandnet_amountsay how much of the total is tax (Checkout, VAT and tenders). - The first orders are demo data. Orders created when the stack starts have
inventory_state = 'LEGACY_UNTRACKED'and no payment or stock reservation; leave them out of consistency checks. - Some things happen a few seconds later. Emails, refunds, restored stock, shipments and deliveries follow the event feed. A check run right after a click can be early: wait 5–10 seconds and run it again before reporting a bug.
- Allowed values are in the database. The status columns are guarded by
CHECKrules; the table reference lists them per table. - Lightweight data is temporary. Every recreate of the
postgrescontainer starts from the demo data again.
Sample queries¶
Each query says what it checks and what a healthy result looks like. Where a healthy result is no rows, any row it returns is worth a closer look.
Did the application save what I entered?¶
Newest users with their role (after a sign-up or an admin edit):
SELECT u.id, u.username, u.email, u.first_name, u.last_name,
CASE r.roles WHEN 0 THEN 'ADMIN' WHEN 1 THEN 'CLIENT' WHEN 2 THEN 'WAREHOUSE' END AS role
FROM app_user u
LEFT JOIN user_entity_roles r ON r.user_entity_id = u.id
ORDER BY u.id DESC
LIMIT 10;
The order items of one user, with line totals (change 'client' to your user):
SELECT o.id AS order_id, o.status, o.created_at, p.name AS product,
i.quantity, i.price, i.quantity * i.price AS line_total
FROM orders o
JOIN order_items i ON i.order_id = o.id
JOIN products p ON p.id = i.product_id
WHERE o.username = 'client'
ORDER BY o.id DESC, i.id;
What is in the carts right now, with the price when added next to today's price:
SELECT c.username, p.name AS product, c.quantity, c.price AS price_in_cart,
p.price AS price_now
FROM cart_items c
JOIN products p ON p.id = c.product_id
ORDER BY c.username, c.id;
Do the numbers add up?¶
Orders whose subtotal is not the sum of their items. Healthy: no rows.
SELECT o.id, o.subtotal_amount, SUM(i.price * i.quantity) AS items_sum
FROM orders o
JOIN order_items i ON i.order_id = o.id
GROUP BY o.id
HAVING o.subtotal_amount <> SUM(i.price * i.quantity);
Orders whose total is not subtotal minus discount. Healthy: no rows.
SELECT id, subtotal_amount, discount_amount, total_amount
FROM orders
WHERE total_amount <> subtotal_amount - discount_amount;
VAT that does not match the rate. Prices are gross, so
tax = total − total / (1 + rate / 100), rounded to cents. Healthy: no rows.
SELECT id, tax_country, tax_rate, total_amount, tax_amount, net_amount,
ROUND(total_amount - total_amount / (1 + tax_rate / 100), 2) AS expected_tax
FROM orders
WHERE tax_rate IS NOT NULL
AND (tax_amount <> ROUND(total_amount - total_amount / (1 + tax_rate / 100), 2)
OR net_amount <> total_amount - tax_amount);
What is left to pay by card after a gift card and loyalty points:
SELECT id, status, total_amount,
COALESCE(gift_card_amount, 0) AS gift_card,
COALESCE(points_amount, 0) AS points,
total_amount - COALESCE(gift_card_amount, 0) - COALESCE(points_amount, 0) AS amount_due
FROM orders
ORDER BY id DESC
LIMIT 20;
Do the services agree?¶
Products where the shop shows a different stock than the warehouse ledger. Healthy: no rows (after a few seconds; the copy is pushed asynchronously).
SELECT p.id, p.name, p.stock_quantity AS shop_shows, s.quantity AS warehouse_ledger,
p.stock_ledger_version, s.ledger_version
FROM products p
LEFT JOIN warehouse.stock_items s ON s.product_id = p.id
WHERE s.quantity IS DISTINCT FROM p.stock_quantity;
The latest stock movements: every deduction, restore and adjustment, with the order and who caused it:
SELECT m.created_at, p.name AS product, m.type, m.delta, m.quantity_after,
m.order_id, m.actor, m.reason
FROM warehouse.stock_movements m
JOIN products p ON p.id = m.product_id
ORDER BY m.id DESC
LIMIT 50;
Products whose ledger quantity differs from what its last movement says. Healthy: no rows.
SELECT s.product_id, s.quantity,
(SELECT m.quantity_after FROM warehouse.stock_movements m
WHERE m.product_id = s.product_id ORDER BY m.id DESC LIMIT 1) AS last_movement_says
FROM warehouse.stock_items s
WHERE s.quantity <> (SELECT m.quantity_after FROM warehouse.stock_movements m
WHERE m.product_id = s.product_id ORDER BY m.id DESC LIMIT 1);
One row per order with its state in every service: the quickest way to see where an order got stuck.
SELECT o.id, o.status AS order_status, o.fulfilment_state,
(SELECT string_agg(p.status, ', ' ORDER BY p.created_at) FROM payment.payments p WHERE p.order_id = o.id) AS payments,
(SELECT string_agg(s.status, ', ' ORDER BY s.id) FROM warehouse.shipments s WHERE s.order_id = o.id) AS shipments,
(SELECT string_agg(d.status, ', ' ORDER BY d.id) FROM delivery.deliveries d WHERE d.order_id = o.id) AS deliveries
FROM orders o
ORDER BY o.id DESC
LIMIT 20;
Paid orders with nothing paid by card, although a card had to pay part of them. Healthy: no rows.
SELECT o.id, o.total_amount, o.gift_card_amount, o.points_amount
FROM orders o
WHERE o.status = 'PAID'
AND o.inventory_state <> 'LEGACY_UNTRACKED'
AND o.total_amount - COALESCE(o.gift_card_amount, 0) - COALESCE(o.points_amount, 0) > 0
AND NOT EXISTS (SELECT 1 FROM payment.payments p
WHERE p.order_id = o.id AND p.status IN ('CAPTURED', 'PARTIALLY_REFUNDED'));
Cancelled orders whose card payment was never refunded. Healthy: no rows (about 10 seconds after the cancellation).
SELECT o.id, o.status, p.status AS payment_status, p.amount, p.refunded_amount
FROM orders o
JOIN payment.payments p ON p.order_id = o.id
WHERE o.status = 'CANCELLED' AND p.status = 'CAPTURED';
Did the asynchronous part happen?¶
Every domain event of one order, in the order the other services read them
(change the 9 to your order id):
SELECT feed_position, type, occurred_at, username, request_id
FROM domain_events
WHERE order_id = 9
ORDER BY sequence;
The latest commerce emails, with their status and errors:
SELECT created_at, username, template, recipient_email, subject, status, attempts, last_error
FROM notification.notification_emails
ORDER BY created_at DESC
LIMIT 20;
The courier timeline of each delivery (delivery-service) and the carrier tracking of each shipment (warehouse-service):
SELECT d.order_id, d.tracking_code, e.occurred_at, e.status, e.location, e.actor
FROM delivery.deliveries d
JOIN delivery.delivery_events e ON e.delivery_id = d.id
ORDER BY d.order_id, e.occurred_at;
SELECT s.order_id, s.carrier, s.tracking_number, t.occurred_at, t.status, t.location
FROM warehouse.shipments s
JOIN warehouse.tracking_events t ON t.shipment_id = s.id
ORDER BY s.order_id, t.occurred_at;
Is old data cleaned up?¶
Unpaid orders whose stock reservation has run out but which were not cancelled. Healthy: no rows (a reservation lasts 30 minutes and the expiry job runs every 15 seconds).
SELECT id, username, status, reservation_expires_at
FROM orders
WHERE status = 'PENDING' AND reservation_expires_at < now() - interval '1 minute';
Password reset tokens and their state (used, expired, valid):
SELECT u.username, t.requested_at, t.expires_at, t.consumed_at,
CASE WHEN t.consumed_at IS NOT NULL THEN 'used'
WHEN t.expires_at < now() THEN 'expired'
ELSE 'valid' END AS state
FROM password_reset_token t
JOIN app_user u ON u.id = t.user_id
ORDER BY t.requested_at DESC;
Quick overviews¶
How many orders are in each status:
Full table reference¶
Every table, column, type, key and rule of all five schemas:
Table reference. It is generated from a running stack
(scripts/generate-db-tables.py), so it matches the migrations of the pinned
images.