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>
105 lines
4.3 KiB
Go
105 lines
4.3 KiB
Go
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")
|
|
}
|
|
}
|