caelum0x 332446652a feat(flags): Railyard Flags behind the add-on, the Flags screen and the OpenFeature SDK (PY2.1)
RYP-E2 and SP-E6, dispatch 4.2 PY2.1 (Railyard side):
- lib/flags: flag environments per app environment (a Scribase environment
  or branch), each with a public client key that reads that one environment.
  Two backends, one surface: the unmodified Flagsmith server (one Flagsmith
  organisation per Railyard org, a project per app, an environment per env;
  FLAGS_API_URL + FLAGS_ADMIN_TOKEN) and, as the fallback the dispatch names
  when the box 2 gate keeps Flagsmith down, the console's own tables
  (migration 0005). An environment keeps the backend it was made on.
- A branch environment starts with its parent's flags (copy_from), then
  changes alone.
- GET /api/v1/flags/client?key=: CORS-open, cached 15 s when keyed by URL;
  the first read by an app is the slot's proof (first_flag_read).
- Bridge route flags (contract: id_format, three cases, flags_item_keys);
  BRIDGE_SLOTS.flags landed.
- The Flags screen (nav item) flips with one click and adds a flag by key;
  POST /api/flags writes only the signed-in org's environments.
- packages/openfeature-railyard: a zero-dependency polling client (30 s,
  offline start from storage, back-off) and OpenFeature client and server
  providers, with Expo and Next.js examples.

Tests: flags core 11/11 (both backends, a fake of the Flagsmith API, tenant
isolation, branch copies, a 502 that leaves nothing half made), bridge 19/19,
contract 3/3, migration 6/6 (0005 twice on a scratch cluster), console 85/85
with the bundle build, tsc clean, SDK 7/7 (a flip reaches the app within 60 s
in the worst cache timing on a fake clock; copy lint on docs and the screen).

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

55 lines
2.8 KiB
SQL

-- Railyard Flags (Wave P PY2.1, RYP-E2, SP-E6). Idempotent: every statement can
-- run again without error or change. Not applied by agents to any hosted
-- database; the owner applies it after 0004 (owner step), agents only to a
-- scratch Postgres.
--
-- flag_envs: one row per app environment (a Scribase project environment or
-- branch). The client key is public (it ships in the app) and reads that one
-- environment only. With the Flagsmith backend, backend_* hold the Flagsmith
-- organisation, project and environment key; with the table backend (the
-- fallback) the values live in railyard.flags.
create table if not exists railyard.flag_envs (
id uuid primary key default gen_random_uuid(),
org_id uuid not null references railyard.orgs (id) on delete cascade,
app text not null check (app ~ '^[a-z0-9][a-z0-9-]{0,39}$'),
env text not null check (env ~ '^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$'),
client_key text not null unique check (client_key ~ '^ry_flags_[A-Za-z0-9_-]{24,64}$'),
backend text not null default 'table' check (backend in ('table', 'flagsmith')),
backend_ref text check (backend_ref is null or length(backend_ref) between 1 and 200),
backend_project text check (backend_project is null or length(backend_project) between 1 and 64),
backend_org text check (backend_org is null or length(backend_org) between 1 and 64),
copied_from uuid references railyard.flag_envs (id) on delete set null,
first_read_at timestamptz,
created_at timestamptz not null default now(),
unique (org_id, app, env)
);
create index if not exists flag_envs_org_idx on railyard.flag_envs (org_id);
create table if not exists railyard.flags (
env_id uuid not null references railyard.flag_envs (id) on delete cascade,
key text not null check (key ~ '^[a-z][a-z0-9_]{0,63}$'),
enabled boolean not null default false,
value text check (value is null or length(value) <= 2000),
description text check (description is null or length(description) <= 200),
updated_at timestamptz not null default now(),
updated_by text check (updated_by is null or length(updated_by) <= 200),
primary key (env_id, key)
);
alter table railyard.flag_envs enable row level security;
alter table railyard.flags enable row level security;
do $$
begin
if exists (select 1 from pg_roles where rolname = 'anon') then
execute 'revoke all on railyard.flag_envs, railyard.flags from anon';
end if;
if exists (select 1 from pg_roles where rolname = 'authenticated') then
execute 'revoke all on railyard.flag_envs, railyard.flags from authenticated';
end if;
if exists (select 1 from pg_roles where rolname = 'service_role') then
execute 'grant all on railyard.flag_envs, railyard.flags to service_role';
end if;
end $$;