package store import ( "context" "testing" "pageswiki/internal/db" ) func TestFeedMigrationAtomicRepeatAndCleanup(t *testing.T) { st := newSchemaTestStore(t) ctx := context.Background() // Fail after creating the tables, and verify the entire migration rolls back. broken := append([]db.SQLMigration(nil), feedMigrations...) broken[0].Statements = append(append([]string(nil), feedMigrations[0].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`) if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil { t.Fatal("expected injected failure") } var count int if e := st.DB.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name='feed_sources'`).Scan(&count); e != nil || count != 0 { t.Fatal("partial schema", count, e) } if e := st.ensureSchemaComponent("feeds"); e != nil { t.Fatal(e) } if _, e := st.DB.Exec(`INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,version,enabled,interval_seconds) VALUES('s','d','u','local','title','','https://example.org',1,1,300); INSERT INTO feed_items VALUES('s','i','title','','body',0,'','',0,0); INSERT INTO feed_states VALUES('s','i',1,1,1)`); e != nil { t.Fatal(e) } for range 2 { if e := st.ensureSchemaComponent("feeds"); e != nil { t.Fatal(e) } } if e := st.DB.QueryRow(`SELECT count(*) FROM feed_items`).Scan(&count); e != nil || count != 1 { t.Fatal("lost data", e) } if _, e := st.DB.Exec(`PRAGMA foreign_keys=OFF; DELETE FROM feed_sources WHERE uuid='s'`); e != nil { t.Fatal(e) } for _, table := range []string{"feed_items", "feed_states"} { if e := st.DB.QueryRow(`SELECT count(*) FROM ` + table).Scan(&count); e != nil || count != 0 { t.Fatal("orphan", table, count, e) } } var result string if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&result); e != nil || result != "ok" { t.Fatal(result, e) } }