-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.sql
More file actions
51 lines (40 loc) · 1.9 KB
/
schema.sql
File metadata and controls
51 lines (40 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- Saga Engine Schema
-- Run this in your Postgres database
CREATE TABLE IF NOT EXISTS transactions (
-- Primary key: Transaction ID
id VARCHAR(255) PRIMARY KEY,
-- Workflow status: 'pending', 'completed', 'failed', 'dead_letter'
-- TERMINAL states: completed, failed, dead_letter
-- dead_letter has NO API recovery - use saga-admin CLI only
status VARCHAR(50) NOT NULL DEFAULT 'pending',
-- The "Checkpoint": Array of completed step names & results
-- Format: [{ name: string, idempotencyKey: string, result: any, status: 'completed' }]
step_stack JSONB NOT NULL DEFAULT '[]',
-- Immutable original input (for replay/debugging)
-- Stored on first save, never modified
input JSONB NOT NULL DEFAULT '{}',
-- Safety cap for dead_letter retries (max: 10)
-- Incremented by saga-admin retry command
retry_count INT NOT NULL DEFAULT 0,
-- Error context if status is 'failed' or 'dead_letter'
-- Format: { stepName: string, error: string, compensationError?: string, timestamp: string }
-- Error messages are truncated to 2KB for data hygiene
error JSONB,
-- Timestamps
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- Performance Indexes
-- Fast lookups by status (for monitoring/alerting)
CREATE INDEX IF NOT EXISTS idx_transactions_status
ON transactions(status);
-- Fast lookups by update time (for stale detection)
CREATE INDEX IF NOT EXISTS idx_transactions_updated_at
ON transactions(updated_at);
-- GIN index for querying specific error types or failed steps
-- Enables queries like: WHERE error->>'stepName' = 'charge-payment'
CREATE INDEX IF NOT EXISTS idx_transactions_error
ON transactions USING GIN (error);
-- Composite index for status + updated_at (common query pattern)
CREATE INDEX IF NOT EXISTS idx_transactions_status_updated
ON transactions(status, updated_at DESC);