- packages/openfeature-railyard/src/targeting.mjs: one dependency-free
evaluator (sync SHA-1, rollout hash = parseInt(sha1("<flag>.<id>")[0:15])
/ 0xfffffffffffffff, inside when <= rollout/100; ordered rules, all
conditions of a rule must match, first match wins with its own value).
Property operators follow the reference matcher of posthog-node (MIT):
exact/is_not (lists, true/false by truthiness), icontains, regex, gt/gte/
lt/lte (numeric, else text), is_set/is_not_set. Segments are named sets of
property conditions per environment (Flagsmith segment shape, BSD-3);
cohorts are product analytics cohort ids. Byte copies with .d.mts types in
apps/console/lib/flags and apps/product-analytics/src/flags (drift test).
- Console: rules and segments live in Railyard's store for both backends
(migration 0009: railyard.flag_targeting, railyard.flag_segments, bounded
checks, cascade, RLS, no anon), so the table fallback and the Flagsmith
backend have the same rule features. setFlag validates rules and segment
references; a used segment cannot be removed; branches copy rules and
segments. The read endpoint takes ?id, ?props (JSON, 2 KB) and ?cohorts,
answers each flag for that person and ships {on, value, rules} plus the
segments used, so SDKs evaluate locally; without a person a targeted flag
reads off (old clients stay safe). Flags screen: rules per flag (everyone,
a property, a segment; cohorts only with RAILYARD_REPLAY_LIVE) with a
share, and segments. POST /api/flags/segments.
- Product analytics /flags: evaluates targeted flags with the person's
stored properties (request person_properties override) and cohort
membership (through person overrides).
- OpenFeature providers and the client: evaluation context targetingKey +
attributes (+ cohorts) resolve per person; cached starts keep rules.
- Contract fixture: flags items carry targeting (additive, version 2).
Tests
- openfeature 16/16: SHA-1 vs node crypto, rollout shares within bounds
over 10 000 ids and monotonic growth, operators, rules, validation, read
answer, client and both providers vs the evaluator, cached start, drift.
- console 142/142 incl. both backends with the same 20 rule sets, segments,
isolation, read-endpoint context, 0009 migration on a scratch PG, and
10 000 ids x 20 rule sets: provider local == read endpoint (0 mismatches).
- product analytics test/qy 10/10 incl. 10 000 ids x 20 rule sets: provider
local == /flags from stored people and cohorts (0 mismatches), and the
unmodified posthog-node local evaluation agreeing on all 18 non-cohort
sets for 10 000 ids (0 mismatches, 0 undecided); a3 experiments unchanged.
- contract 6/6; console and PA tsc clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
2.4 KiB
SQL
46 lines
2.4 KiB
SQL
-- Flag targeting and percentage rollouts (Wave Q QY1.3, I10 and A19).
|
|
-- Rules and segments live in Railyard's tables for every flag environment,
|
|
-- whichever backend holds the on/off and value (table or the flags server),
|
|
-- so one hash and one rule engine answer everywhere. The rule documents are
|
|
-- validated by the application (lib/flags/targeting.mjs); the checks here keep
|
|
-- the shape and size bounded. Additive and idempotent. Not applied by agents
|
|
-- to any hosted database; the owner applies it after 0008.
|
|
|
|
create table if not exists railyard.flag_targeting (
|
|
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}$'),
|
|
targeting jsonb not null check (jsonb_typeof(targeting) = 'object' and jsonb_typeof(targeting -> 'rules') = 'array'
|
|
and jsonb_array_length(targeting -> 'rules') between 1 and 20
|
|
and pg_column_size(targeting) <= 65536),
|
|
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)
|
|
);
|
|
|
|
create table if not exists railyard.flag_segments (
|
|
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}$'),
|
|
name text not null check (length(name) between 1 and 80),
|
|
conditions jsonb not null check (jsonb_typeof(conditions) = 'array' and jsonb_array_length(conditions) between 1 and 10
|
|
and pg_column_size(conditions) <= 32768),
|
|
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_targeting enable row level security;
|
|
alter table railyard.flag_segments enable row level security;
|
|
|
|
do $$
|
|
begin
|
|
if exists (select 1 from pg_roles where rolname = 'anon') then
|
|
execute 'revoke all on railyard.flag_targeting, railyard.flag_segments from anon';
|
|
end if;
|
|
if exists (select 1 from pg_roles where rolname = 'authenticated') then
|
|
execute 'revoke all on railyard.flag_targeting, railyard.flag_segments from authenticated';
|
|
end if;
|
|
if exists (select 1 from pg_roles where rolname = 'service_role') then
|
|
execute 'grant all on railyard.flag_targeting, railyard.flag_segments to service_role';
|
|
end if;
|
|
end $$;
|