railyard/apps/console/db/migrations/0004_bridge_order.sql
caelum0x 1bbce8d338 fix(bridge): the signed message id names its route and a late orgs sync never flips an entitlement (P-Y1)
Contract scribase-bridge version 2:
- webhook-id is msg_<route>_<24 hex>; the id is inside the Standard Webhooks
  signed content, so a signature made for one route is refused on another
  (401, fixture wrong_route). ai and ai_usage cannot be confused.
- every orgs request carries addon.as_of (Scribase's epoch ms of the state).
  Railyard stores it (migration 0004, addon_as_of) and applies a sync only
  when no newer state is stored: one guarded PATCH through PostgREST. An older
  sync answers 200 with stale: true and changes nothing.
- RAILYARD_BRIDGE_ACCEPT_LEGACY=1 (default off) accepts the P1 sender for the
  minutes between the console and control-api deploys; it never lets an id
  that names another route through.

Tests: bridge 15/15 (3 new), contract 3/3 with the new cases, postgrest-repo
and migration 11/11 (0004 applied twice on a scratch cluster, guarded update
proven in SQL), console suite 66/66, bundle 3/3, tsc clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 23:33:47 +03:00

24 lines
963 B
SQL

-- Scribase bridge ordering (Wave P P-Y1). Idempotent: every statement can run
-- again without error or change. Not applied by agents to any hosted
-- database; the owner applies it after 0003 (owner step), agents only to a
-- scratch Postgres.
--
-- Each orgs sync from Scribase carries addon.as_of, the Scribase time (epoch
-- milliseconds) of the add-on state it mirrors. The bridge stores it here and
-- refuses to apply an older state, so a late or out-of-order delivery inside
-- the signature tolerance can never flip an entitlement back.
alter table railyard.entitlements add column if not exists addon_as_of bigint;
do $$
begin
if not exists (
select 1 from pg_constraint
where conname = 'entitlements_addon_as_of_check'
and conrelid = 'railyard.entitlements'::regclass
) then
alter table railyard.entitlements
add constraint entitlements_addon_as_of_check check (addon_as_of is null or addon_as_of > 0);
end if;
end $$;