feat(sources): enforce supported source allowlist

Assign stable keys to the three supported sources, preserve excluded historical rows, and expose history read-only in admin. Add migration, coverage, and responsive browser spec.

Refs #13

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 00:11:26 +01:00
parent 4459a81206
commit ac17d27f51
26 changed files with 924 additions and 61 deletions

View File

@ -0,0 +1,30 @@
-- One SQL statement: validation, DDL and backfill share a transaction even when
-- executed without a caller-owned transaction. Never merge historical sources.
DO $migration$
BEGIN
LOCK TABLE sources IN ACCESS EXCLUSIVE MODE;
IF (SELECT count(*) FROM sources WHERE type = 'stocktwits'
AND lower(btrim(regexp_replace(name, '[[:space:]]+', ' ', 'g'))) = 'yahoo finance') <> 1 THEN
RAISE EXCEPTION 'source_identity_invalid: expected exactly one Yahoo Finance';
END IF;
IF (SELECT count(*) FROM sources WHERE type = 'bloomberg'
AND lower(btrim(regexp_replace(name, '[[:space:]]+', ' ', 'g'))) = 'bloomberg') <> 1 THEN
RAISE EXCEPTION 'source_identity_invalid: expected exactly one Bloomberg';
END IF;
IF (SELECT count(*) FROM sources WHERE type = 'watcherguru'
AND lower(btrim(regexp_replace(name, '[[:space:]]+', ' ', 'g'))) = 'watcher.guru') <> 1 THEN
RAISE EXCEPTION 'source_identity_invalid: expected exactly one Watcher.Guru';
END IF;
ALTER TABLE sources ADD COLUMN source_key TEXT NULL
CONSTRAINT sources_source_key_check CHECK (source_key IN ('yahoo_finance', 'bloomberg', 'watcher_guru'));
CREATE UNIQUE INDEX sources_source_key_unique ON sources(source_key) WHERE source_key IS NOT NULL;
UPDATE sources SET source_key = CASE
WHEN type = 'stocktwits' AND lower(btrim(regexp_replace(name, '[[:space:]]+', ' ', 'g'))) = 'yahoo finance' THEN 'yahoo_finance'
WHEN type = 'bloomberg' AND lower(btrim(regexp_replace(name, '[[:space:]]+', ' ', 'g'))) = 'bloomberg' THEN 'bloomberg'
WHEN type = 'watcherguru' AND lower(btrim(regexp_replace(name, '[[:space:]]+', ' ', 'g'))) = 'watcher.guru' THEN 'watcher_guru'
ELSE NULL END;
UPDATE sources SET enabled = false WHERE source_key IS NULL;
END
$migration$;