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) } // Upgrade an existing v1 cache, not just an empty database. if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:1]); 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) } if _, e := st.DB.Exec(`UPDATE feed_sources SET etag='old',modified='date',next_ms=999,lease='old-worker',lease_until=999`); e != nil { t.Fatal(e) } broken = append([]db.SQLMigration(nil), feedMigrations...) broken[1].Statements = append(append([]string(nil), feedMigrations[1].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`) if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil { t.Fatal("expected v2 failure") } if e := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('feed_items') WHERE name='body_html'`).Scan(&count); e != nil || count != 0 { t.Fatal("partial v2 schema", count, e) } var oldETag string if e := st.DB.QueryRow(`SELECT etag FROM feed_sources WHERE uuid='s'`).Scan(&oldETag); e != nil || oldETag != "old" { t.Fatal("partial v2 metadata", oldETag, e) } for range 2 { if e := st.ensureSchemaComponent("feeds"); e != nil { t.Fatal(e) } } var body, markup, etag, lease string var version, next int64 if e := st.DB.QueryRow(`SELECT body,body_html FROM feed_items WHERE source_uuid='s'`).Scan(&body, &markup); e != nil || body != "body" || markup != "" { t.Fatal("body migration", body, markup, e) } if e := st.DB.QueryRow(`SELECT version,next_ms,etag,lease FROM feed_sources WHERE uuid='s'`).Scan(&version, &next, &etag, &lease); e != nil || version != 2 || next != 0 || etag != "" || lease != "" { t.Fatal("refetch/fence", version, next, etag, lease, 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) } }