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>
31 lines
1.8 KiB
SQL
31 lines
1.8 KiB
SQL
-- 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$;
|