package migrations import ( "os" "reflect" "testing" ) func TestTicket42RetirementPreservesIDsCapsFeedsAndHistoryPostgres(t *testing.T) { db, cleanup := ticket13Database(t) defer cleanup() for _, q := range []string{ `ALTER TABLE sources ADD COLUMN source_key TEXT CHECK(source_key IN ('yahoo_finance','cnbc_business','bloomberg','investing_com'))`, `INSERT INTO sources(id,name,type,source_key,enabled,article_limit) VALUES('y','Yahoo Finance','stocktwits','yahoo_finance',true,83),('c','CNBC Business','cnbc_business','cnbc_business',true,31),('b','Bloomberg','bloomberg','bloomberg',false,77),('i','Investing.com','investing_com','investing_com',true,20)`, `CREATE TABLE source_feeds(source_id TEXT REFERENCES sources(id),position INT,url TEXT)`, `INSERT INTO source_feeds VALUES('b',0,'https://feed.quanwenrss.com/feed/bloomberg-latest.xml'),('i',0,'https://feed.quanwenrss.com/feed/investing-news.xml')`, `INSERT INTO articles(id,source_id,title,url) VALUES('old-y','y','Synthetic historical Yahoo','https://fixture.invalid/y'),('old-c','c','Synthetic historical CNBC','https://fixture.invalid/c')`, `CREATE TABLE fixture_reports(id TEXT PRIMARY KEY,source_id TEXT REFERENCES sources(id),content TEXT)`, `INSERT INTO fixture_reports VALUES('report-y','y','Synthetic historical report'),('report-c','c','Synthetic historical report')`, `CREATE TABLE fixture_outcomes(payload JSONB)`, `INSERT INTO fixture_outcomes VALUES('{"source_stats":[{"source_key":"yahoo_finance","source_name":"Yahoo Finance"},{"source_key":"cnbc_business","source_name":"CNBC Business"}]}')`, } { if _, err := db.Exec(q); err != nil { t.Fatal(err) } } before := ticket13ReadSources(t, db) schema := ticket13SchemaSnapshot(t, db) refs := ticket13ReadArticleSources(t, db) var history, feeds, reports string snapshots := []struct { query string value *string }{ {`SELECT payload::text FROM fixture_outcomes`, &history}, {`SELECT string_agg(source_id||':'||position||':'||url,',' ORDER BY source_id,position) FROM source_feeds`, &feeds}, {`SELECT string_agg(id||':'||source_id||':'||content,',' ORDER BY id) FROM fixture_reports`, &reports}, } for _, s := range snapshots { if err := db.QueryRow(s.query).Scan(s.value); err != nil { t.Fatal(err) } } for _, file := range []string{"000022_retire_yahoo_cnbc.up.sql", "000022_retire_yahoo_cnbc.down.sql"} { raw, err := os.ReadFile(file) if err != nil { t.Fatal(err) } if _, err = db.Exec(string(raw)); err != nil { t.Fatal(err) } got := ticket13ReadSources(t, db) expected := append([]ticket13SourceRow(nil), before...) for n := range expected { if expected[n].id == "y" || expected[n].id == "c" { expected[n].enabled = false } } if !reflect.DeepEqual(got, expected) || ticket13SchemaSnapshot(t, db) != schema || !reflect.DeepEqual(ticket13ReadArticleSources(t, db), refs) { t.Fatal("retirement/rollback changed more than legacy switches") } for _, s := range snapshots { var after string if err := db.QueryRow(s.query).Scan(&after); err != nil || after != *s.value { t.Fatal("history/feeds/report payload changed") } } var keys int if err := db.QueryRow(`SELECT count(*) FROM sources WHERE (id='y' AND source_key='yahoo_finance') OR (id='c' AND source_key='cnbc_business')`).Scan(&keys); err != nil || keys != 2 { t.Fatal("legacy keys were removed") } } }