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) } } func TestFeedImportReceiptMigrationRollbackAndRepeat(t *testing.T) { st := newSchemaTestStore(t) ctx := context.Background() if err := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:2]); err != nil { t.Fatal(err) } if _, err := 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',7,1,300)`); err != nil { t.Fatal(err) } broken := append([]db.SQLMigration(nil), feedMigrations...) broken[2].Statements = append(append([]string(nil), feedMigrations[2].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`) if err := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); err == nil { t.Fatal("expected v3 failure") } var count int if err := st.DB.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name='feed_import_receipts'`).Scan(&count); err != nil || count != 0 { t.Fatal("partial migration", count, err) } if err := st.ensureSchemaComponent("feeds"); err != nil { t.Fatal(err) } if _, err := st.DB.Exec(`INSERT INTO feed_import_receipts VALUES('d','u','request','hash','["s"]')`); err != nil { t.Fatal(err) } if err := st.ensureSchemaComponent("feeds"); err != nil { t.Fatal(err) } if err := st.DB.QueryRow(`SELECT version FROM feed_sources WHERE uuid='s'`).Scan(&count); err != nil || count != 7 { t.Fatal("unrelated data changed", count, err) } if _, err := st.DB.Exec(`DELETE FROM feed_sources WHERE uuid='s'`); err != nil { t.Fatal(err) } if err := st.DB.QueryRow(`SELECT count(*) FROM feed_import_receipts`).Scan(&count); err != nil || count != 1 { t.Fatal("receipt must survive deletion", count, err) } } func TestFeedCatalogMigrationRollbackAndRepeat(t *testing.T) { st := newSchemaTestStore(t) ctx := context.Background() if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:3]); 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,lease,next_ms) VALUES('s','d','u','freshrss','Title','','https://example.org',7,1,300,'old',999); INSERT INTO feed_items(source_uuid,id,title,url,body,published_ms,feed,folder,is_read,starred,body_html) VALUES('s','i','Title','','Body',1,'','',1,1,'

Body

')`); e != nil { t.Fatal(e) } broken := append([]db.SQLMigration(nil), feedMigrations...) broken[3].Statements = append(append([]string(nil), feedMigrations[3].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`) if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil { t.Fatal("expected failure") } var count int if e := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('feed_items') WHERE name='remote_feed'`).Scan(&count); e != nil || count != 0 { t.Fatal("partial migration", count, e) } if e := st.DB.QueryRow(`SELECT version FROM feed_sources WHERE uuid='s'`).Scan(&count); e != nil || count != 7 { t.Fatal("partial fence", count, e) } for range 2 { if e := st.ensureSchemaComponent("feeds"); e != nil { t.Fatal(e) } } var version, next int var catalog, lease, body string var read, star bool if e := st.DB.QueryRow(`SELECT version,next_ms,lease,remote_catalog FROM feed_sources WHERE uuid='s'`).Scan(&version, &next, &lease, &catalog); e != nil || version != 8 || next != 0 || lease != "" || catalog != "[]" { t.Fatal(version, next, lease, catalog, e) } if e := st.DB.QueryRow(`SELECT body_html,is_read,starred FROM feed_items WHERE source_uuid='s'`).Scan(&body, &read, &star); e != nil || body != "

Body

" || !read || !star { t.Fatal("lost old cache", body, read, star, e) } var integrity string if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&integrity); e != nil || integrity != "ok" { t.Fatal(integrity, e) } } func TestFeedActionsMigrationRollbackAndRepeat(t *testing.T) { st := newSchemaTestStore(t) ctx := context.Background() if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:4]); 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','freshrss','Title','','https://example.org',7,1,300)`); e != nil { t.Fatal(e) } broken := append([]db.SQLMigration(nil), feedMigrations...) broken[4].Statements = append(append([]string(nil), feedMigrations[4].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`) if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil { t.Fatal("expected failure") } var count int if e := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('feed_sources') WHERE name='write_enabled'`).Scan(&count); e != nil || count != 0 { t.Fatal("partial schema", count, e) } for range 2 { if e := st.ensureSchemaComponent("feeds"); e != nil { t.Fatal(e) } } if e := st.DB.QueryRow(`SELECT write_enabled FROM feed_sources WHERE uuid='s'`).Scan(&count); e != nil || count != 0 { t.Fatal("write enabled during upgrade", count, e) } if _, e := st.DB.Exec(`INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES('d','u','r','s','i','read',1,7,'sending',1); DELETE FROM feed_sources WHERE uuid='s'`); e != nil { t.Fatal(e) } var status string if e := st.DB.QueryRow(`SELECT status FROM feed_actions WHERE request_id='r'`).Scan(&status); e != nil || status != "unknown" { t.Fatal("lost uncertain receipt", status, e) } if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&status); e != nil || status != "ok" { t.Fatal(status, e) } } func TestFeedFoldersMigrationRollbackAndRepeat(t *testing.T) { st := newSchemaTestStore(t) ctx := context.Background() if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", feedMigrations[:5]); e != nil { t.Fatal(e) } const catalog = `[{"id":"feed/1","title":"Title","categories":[{"id":"category","label":"Folder"}]}]` if _, e := st.DB.Exec(`INSERT INTO feed_sources(uuid,domain_uuid,owner_uuid,provider,title,folder,url,version,enabled,interval_seconds,lease,next_ms,remote_catalog) VALUES('s','d','u','freshrss','Title','','https://example.org',7,1,300,'worker',999,?); INSERT INTO feed_actions(domain_uuid,owner_uuid,request_id,source_uuid,item_id,field,desired,source_version,status,created_ms) VALUES('d','u','r','s','i','read',1,7,'queued',1)`, catalog); e != nil { t.Fatal(e) } broken := append([]db.SQLMigration(nil), feedMigrations...) broken[5].Statements = append(append([]string(nil), feedMigrations[5].Statements...), `INSERT INTO no_such_feed_table VALUES(1)`) if e := db.ApplySQLiteMigrations(ctx, st.DB, "feeds", broken); e == nil { t.Fatal("expected failure") } var count int if e := st.DB.QueryRow(`SELECT count(*) FROM pragma_table_info('feed_sources') WHERE name='remote_folders'`).Scan(&count); e != nil || count != 0 { t.Fatal("partial schema", count, e) } for range 2 { if e := st.ensureSchemaComponent("feeds"); e != nil { t.Fatal(e) } } var version, next int var got, folders, lease, status string if e := st.DB.QueryRow(`SELECT version,next_ms,lease,remote_catalog,remote_folders FROM feed_sources WHERE uuid='s'`).Scan(&version, &next, &lease, &got, &folders); e != nil || version != 7 || next != 999 || lease != "worker" || got != catalog || folders != "[]" { t.Fatal("existing state changed", version, next, lease, got, folders, e) } if e := st.DB.QueryRow(`SELECT status FROM feed_actions WHERE request_id='r'`).Scan(&status); e != nil || status != "queued" { t.Fatal("pending action changed", status, e) } if e := st.DB.QueryRow(`PRAGMA integrity_check`).Scan(&status); e != nil || status != "ok" { t.Fatal(status, e) } }