diff --git a/core/source.go b/core/source.go index dd4fcc5..d7521f7 100644 --- a/core/source.go +++ b/core/source.go @@ -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. diff --git a/core/source_test.go b/core/source_test.go index 3799d90..3aae051 100644 --- a/core/source_test.go +++ b/core/source_test.go @@ -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) + } +} diff --git a/core/sql/0001_initial_schema.sql b/core/sql/0001_initial_schema.sql index 6b53563..7c3fd67 100644 --- a/core/sql/0001_initial_schema.sql +++ b/core/sql/0001_initial_schema.sql @@ -1,5 +1,6 @@ create table sources( name text not null, + state blob, primary key (name) ) strict; create table actions(