Add state blob to source

This commit is contained in:
Tim Van Baak 2025-01-30 15:22:57 -08:00
parent 68038839ec
commit 5692f60318
3 changed files with 40 additions and 0 deletions

View File

@ -238,6 +238,18 @@ func GetAllItemsForSource(db *DB, source string) ([]Item, error) {
`, source)
}
func GetState(db *DB, source string) ([]byte, error) {
row := db.QueryRow("select state from sources where name = ?", source)
var state []byte
err := row.Scan(&state)
return state, err
}
func SetState(db *DB, source string, state []byte) error {
_, err := db.Exec("update sources set state = ? where name = ?", state, source)
return err
}
// Given the results of a fetch, add new items, update existing items, and delete expired items.
//
// Returns the number of new and deleted items on success.

View File

@ -291,3 +291,30 @@ func TestOnCreateAction(t *testing.T) {
t.Fatal("unexpected changes to id, active, or created fields")
}
}
func TestSourceState(t *testing.T) {
db := EphemeralDb(t)
if err := AddSource(db, "s"); err != nil {
t.Fatal(err)
}
state, err := GetState(db, "s")
if err != nil {
t.Fatal(err)
}
if len(state) != 0 {
t.Fatal("expected no state on a fresh source")
}
if err = SetState(db, "s", []byte("hello, world")); err != nil {
t.Fatal(err)
}
state, err = GetState(db, "s")
if err != nil {
t.Fatal(err)
}
if string(state) != "hello, world" {
t.Fatalf("expected hello, world, got %s", state)
}
}

View File

@ -1,5 +1,6 @@
create table sources(
name text not null,
state blob,
primary key (name)
) strict;
create table actions(