remora/crates/remora-durable/migrations/0001_substrate.sql
ms2k 957d170150 Add PostgreSQL DurableStore substrate for WP-03
Introduce remora-durable with pool/migrations, a RemoraApiMutation-shaped
ledger, claim-fenced outbox projection, CAS helpers, and keyset cursors,
plus a kind Job acceptance suite against the hermetic fixture.
2026-07-16 03:56:44 +09:00

38 lines
1.5 KiB
SQL

-- DurableStore substrate tables owned by remora-durable.
-- Feature packages add their own migrations; they do not alter these tables.
CREATE TABLE mutation_records (
scope_subject TEXT NOT NULL,
scope_operation TEXT NOT NULL,
operation_key TEXT NOT NULL,
kind TEXT NOT NULL,
-- Opaque RemoraApiMutation eligibility / authority binding (attempt, fence,
-- worker incarnation, or other claim material). Compared for exact replay.
eligibility_claim BYTEA NOT NULL,
request_fingerprint BYTEA NOT NULL CHECK (octet_length(request_fingerprint) = 32),
response_status TEXT NOT NULL CHECK (response_status IN ('accepted', 'rejected')),
response_body JSONB NOT NULL,
effect_applied BOOLEAN NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (scope_subject, scope_operation, operation_key),
CHECK (effect_applied = (response_status = 'accepted'))
);
CREATE TABLE outbox_messages (
id BIGSERIAL PRIMARY KEY,
topic TEXT NOT NULL,
payload JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
available_at TIMESTAMPTZ NOT NULL DEFAULT now(),
claimed_by TEXT,
claimed_until TIMESTAMPTZ,
-- Monotonic per-row claim epoch; with claimed_by forms the ownership token.
claim_generation BIGINT NOT NULL DEFAULT 0,
projected_at TIMESTAMPTZ,
attempt_count INTEGER NOT NULL DEFAULT 0,
last_error TEXT
);
CREATE INDEX outbox_messages_claimable_idx
ON outbox_messages (available_at, id)
WHERE projected_at IS NULL;