CREATE TABLE orders (
id uuid PRIMARY KEY,
status text NOT NULL,
total_cents bigint NOT NULL,
created_at timestamptz NOT NULL default now()
);
CREATE TABLE outbox (
event_id uuid PRIMARY KEY,
event_type text NOT NULL,
payload jsonb NOT NULL,
status text NOT NULL CHECK (status IN ('NEW','SENT','ERROR')),
retry_count int NOT NULL default 0,
retry_at timestamptz,
occurred_at timestamptz NOT NULL default now(),
sent_at timestamptz
);
CREATE TABLE processed_events (
event_id uuid NOT NULL,
handler text NOT NULL, -- имя consumer-group/хендлера
processed_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (event_id, handler)
);