railyard/apps/console/db/migrations/0002_scribase_bridge.sql
caelum0x 71336987dd feat(bridge): Scribase bridge creates the bound Railyard org, one-time CLI claims and slot proofs; denylist 2026-09-29.3 (PY1.0)
PY1.0 acceptance (C8 delta check on main f919636; Wave E Y-0 to Y-5 merged):
1. Denylist .3 (gtm/policy, published by this package) adds portfolio-ott-drm
   and portfolio-ott-comparison (DRM, Netflix-style, your own Netflix beyond
   Menivor), addon-unlaunched (ADDON_LIVE), rally-capacity-purchase
   (RALLY_CAPACITY_PURCHASE_LIVE) and at-cost (allowed only next to a live
   price source). Re-vendored byte for byte; every new rule has failing
   fixtures (policy/test/fixtures/denylist-3-new-phrases.json); .2 fixtures kept.
2. apps/console/app/api/v1/bridge/scribase/{orgs,claims,proof}: signed
   (Standard Webhooks, RAILYARD_SCRIBASE_BRIDGE_SECRET), replay-proof calls from
   Scribase control-api. The first call creates the Railyard org bound to the
   Scribase org id (deterministic owner id, no second sign-up) and mirrors the
   add-on into the entitlement (plan "addon"); a lapsed add-on pauses the org
   and keeps its data. Keys go only to the Scribase CLI: a claim is a one-time,
   pre-approved device code (120 s) exchanged at /api/v1/device/token; a grep
   test keeps every bridge handler free of stdout, stderr and log writes.
3. proof answers {slot, state, live, proof:{kind,id,at}, next}; slots whose
   package has not landed answer "arriving".
Contract: tests/contract/fixtures/scribase-bridge/contract.json, run by
tests/contract/scribase-bridge.test.ts; the Scribase carve-out tests the same file.

Migration: apps/console/db/migrations/0002_scribase_bridge.sql (idempotent;
applied twice to a scratch local cluster only by test/migration.test.ts).
Env: RAILYARD_SCRIBASE_BRIDGE_SECRET (secret; unset keeps the bridge off).

Tests: console npm test 48/48, tests/contract 2/2, policy 11/11, cli 9/9, mcp 9/9,
tsc --noEmit clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 18:39:24 +03:00

46 lines
2.3 KiB
SQL

-- Scribase bridge (Wave P PY1.0, RYP-E6) and the portfolio add-on plan (PY1.1).
-- Idempotent: every statement can run again without error or change. Not
-- applied by agents to any hosted database; the owner applies it after 0001
-- (owner step), agents only to a scratch Postgres.
--
-- A Scribase org that turns on the portfolio add-on gets one Railyard org,
-- created by the bridge on first use and bound here. Scribase bills the
-- add-on; the entitlement row mirrors it with plan 'addon'.
create table if not exists railyard.scribase_bindings (
scribase_org_id text primary key check (scribase_org_id ~ '^[A-Za-z0-9_-]{1,128}$'),
org_id uuid not null unique references railyard.orgs (id) on delete cascade,
created_at timestamptz not null default now()
);
-- Replay protection for signed bridge calls: one row per Standard Webhooks id.
create table if not exists railyard.bridge_messages (
message_id text primary key check (length(message_id) between 1 and 200),
received_at timestamptz not null default now()
);
create index if not exists bridge_messages_received_idx on railyard.bridge_messages (received_at);
alter table railyard.entitlements add column if not exists addon_slots text[] not null default '{}';
alter table railyard.entitlements add column if not exists addon_renews_at timestamptz;
-- Widen the plan check to the add-on plan (same name, so a re-run replaces it).
alter table railyard.entitlements drop constraint if exists entitlements_plan_check;
alter table railyard.entitlements
add constraint entitlements_plan_check check (plan in ('none', 'trial', 'railyard', 'addon'));
alter table railyard.scribase_bindings enable row level security;
alter table railyard.bridge_messages enable row level security;
do $$
begin
if exists (select 1 from pg_roles where rolname = 'anon') then
execute 'revoke all on railyard.scribase_bindings, railyard.bridge_messages from anon';
end if;
if exists (select 1 from pg_roles where rolname = 'authenticated') then
execute 'revoke all on railyard.scribase_bindings, railyard.bridge_messages from authenticated';
end if;
if exists (select 1 from pg_roles where rolname = 'service_role') then
execute 'grant all on railyard.scribase_bindings, railyard.bridge_messages to service_role';
end if;
end $$;