feat(sources): replace WatcherGuru with CNBC Business

Add the CNBC Business RSS source, switch supported-source wiring and reporting, and migrate existing WatcherGuru rows to disabled historical status without rewriting references.

Refs #23

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 17:17:15 +01:00
parent 1ff78a7b31
commit 64a7124889
18 changed files with 511 additions and 26 deletions

View File

@ -0,0 +1,3 @@
-- Non-destructive retirement on downgrade: retain CNBC identity/history and
-- permissive historical constraint values. Never auto-reactivate WatcherGuru.
UPDATE sources SET enabled=false WHERE source_key='cnbc_business';

View File

@ -0,0 +1,16 @@
-- Atomic even when executed as a standalone statement. Preserve retired source
-- IDs, source keys, caps and all referencing history; never repurpose a row.
DO $migration$
BEGIN
LOCK TABLE sources IN ACCESS EXCLUSIVE MODE;
ALTER TABLE sources DROP CONSTRAINT sources_type_check;
ALTER TABLE sources ADD CONSTRAINT sources_type_check
CHECK (type IN ('bloomberg','stocktwits','reuters','watcherguru','cnbc_business'));
ALTER TABLE sources DROP CONSTRAINT sources_source_key_check;
ALTER TABLE sources ADD CONSTRAINT sources_source_key_check
CHECK (source_key IN ('yahoo_finance','bloomberg','watcher_guru','cnbc_business'));
UPDATE sources SET enabled=false WHERE source_key='watcher_guru';
INSERT INTO sources(id,name,type,enabled,article_limit,source_key)
VALUES(gen_random_uuid(),'CNBC Business','cnbc_business',true,20,'cnbc_business');
END
$migration$;

View File

@ -0,0 +1,104 @@
package migrations
import (
"os"
"regexp"
"strings"
"testing"
)
func TestCNBCMigrationRetainsWatcherIdentityAndHistoricalReferencesPostgres(t *testing.T) {
db, cleanup := ticket13Database(t)
defer cleanup()
for _, q := range []string{
`ALTER TABLE sources ADD CONSTRAINT sources_type_check CHECK(type IN ('bloomberg','stocktwits','reuters','watcherguru'))`,
`INSERT INTO sources(id,name,type,enabled,article_limit) VALUES('y','Yahoo Finance','stocktwits',true,83),('b','Bloomberg','bloomberg',false,77),('w','Watcher.Guru','watcherguru',true,64)`,
`INSERT INTO articles(id,source_id,title,url) VALUES('old-article','w','historic','https://fixture.invalid/history')`,
} {
if _, err := db.Exec(q); err != nil {
t.Fatal(err)
}
}
applyTicket13(t, db)
migration, err := os.ReadFile("000018_cnbc_business.up.sql")
if err != nil {
t.Fatal(err)
}
if _, err = db.Exec(string(migration)); err != nil {
t.Fatal(err)
}
var name, typ, key string
var enabled bool
var cap int
if err = db.QueryRow(`SELECT name,type,source_key,enabled,article_limit FROM sources WHERE id='w'`).Scan(&name, &typ, &key, &enabled, &cap); err != nil {
t.Fatal(err)
}
if name != "Watcher.Guru" || typ != "watcherguru" || key != "watcher_guru" || enabled || cap != 64 {
t.Fatal("Watcher identity/cap changed or still enabled")
}
var cnbcID string
if err = db.QueryRow(`SELECT id,enabled,article_limit FROM sources WHERE source_key='cnbc_business'`).Scan(&cnbcID, &enabled, &cap); err != nil {
t.Fatal(err)
}
if cnbcID == "w" || !regexp.MustCompile(`^[0-9a-f-]{36}$`).MatchString(cnbcID) || !enabled || cap != 20 {
t.Fatalf("new identity=%s enabled=%v cap=%d", cnbcID, enabled, cap)
}
var source string
if err = db.QueryRow(`SELECT source_id FROM articles WHERE id='old-article'`).Scan(&source); err != nil || source != "w" {
t.Fatalf("historical reference=%s err=%v", source, err)
}
if _, err = db.Exec(`UPDATE sources SET source_key='watcher_guru' WHERE id='w'`); err != nil {
t.Fatalf("legacy key no longer permitted by constraint: %v", err)
}
if _, err = db.Exec(`UPDATE sources SET source_key='unsupported' WHERE id='w'`); err == nil {
t.Fatal("unknown key accepted")
}
if _, err = db.Exec(`UPDATE sources SET type='unsupported' WHERE id='w'`); err == nil {
t.Fatal("unknown type accepted")
}
var count int
if err = db.QueryRow(`SELECT count(*) FROM sources`).Scan(&count); err != nil || count != 4 {
t.Fatalf("unexpected source count=%d %v", count, err)
}
var yahooCap, bloombergCap int
if err = db.QueryRow(`SELECT article_limit FROM sources WHERE id='y'`).Scan(&yahooCap); err != nil {
t.Fatal(err)
}
if err = db.QueryRow(`SELECT article_limit FROM sources WHERE id='b'`).Scan(&bloombergCap); err != nil || yahooCap != 83 || bloombergCap != 77 {
t.Fatal("other caps changed")
}
}
func TestCNBCMigrationFailureRollsBackWatcherAndConstraintChangesPostgres(t *testing.T) {
db, cleanup := ticket13Database(t)
defer cleanup()
for _, q := range []string{
`ALTER TABLE sources ADD CONSTRAINT sources_type_check CHECK(type IN ('bloomberg','stocktwits','reuters','watcherguru'))`,
`INSERT INTO sources(id,name,type,enabled,article_limit) VALUES('y','Yahoo Finance','stocktwits',true,83),('b','Bloomberg','bloomberg',false,77),('w','Watcher.Guru','watcherguru',true,64)`,
`ALTER TABLE sources ADD CONSTRAINT fixture_refuse_cnbc CHECK(name <> 'CNBC Business')`,
} {
if _, err := db.Exec(q); err != nil {
t.Fatal(err)
}
}
applyTicket13(t, db)
migration, err := os.ReadFile("000018_cnbc_business.up.sql")
if err != nil {
t.Fatal(err)
}
if _, err = db.Exec(string(migration)); err == nil {
t.Fatal("expected insert failure")
}
var enabled bool
var count int
var constraint string
if err = db.QueryRow(`SELECT enabled FROM sources WHERE id='w'`).Scan(&enabled); err != nil || !enabled {
t.Fatal("failed migration disabled WatcherGuru")
}
if err = db.QueryRow(`SELECT count(*) FROM sources`).Scan(&count); err != nil || count != 3 {
t.Fatal("failed migration changed source rows")
}
if err = db.QueryRow(`SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid='sources'::regclass AND conname='sources_source_key_check'`).Scan(&constraint); err != nil || strings.Contains(constraint, "cnbc_business") {
t.Fatal("failed migration changed source key constraint")
}
}