Add lastUpdated to sources

This commit is contained in:
Tim Van Baak 2025-02-18 19:50:19 -08:00
parent fc68d313b1
commit ee63ee32b0
3 changed files with 51 additions and 2 deletions

View File

@ -12,8 +12,8 @@ import (
func AddSource(db DB, name string) error {
_, err := db.Exec(`
insert into sources (name)
values (?)
insert into sources (name, lastUpdated)
values (?, 0)
`, name)
return err
@ -70,6 +70,22 @@ func SetState(db DB, source string, state []byte) error {
return err
}
func GetLastUpdated(db DB, source string) (time.Time, error) {
row := db.QueryRow("select lastUpdated from sources where name = ?", source)
var updated int
err := row.Scan(&updated)
return time.Unix(int64(updated), 0).UTC(), err
}
func BumpLastUpdated(db DB, source string, now time.Time) error {
_, err := db.Exec(`
update sources
set lastUpdated = ?
where name = ?
`, now.Unix(), source)
return err
}
func getSourceTtx(db DB, source string, env string) (int, error) {
row := db.QueryRow(`
select value
@ -268,5 +284,9 @@ func updateWithFetchedItemsTx(
return 0, 0, err
}
if err = BumpLastUpdated(db, source, now); err != nil {
return 0, 0, err
}
return len(newItems), len(idsToDelete), nil
}

View File

@ -322,3 +322,31 @@ func TestSourceTtx(t *testing.T) {
t.Fatalf("Missing value after postProcess: ttl = %d, ttd = %d, tts = %d", after.Ttl, after.Ttd, after.Tts)
}
}
func TestSourceLastUpdated(t *testing.T) {
db := EphemeralDb(t)
if err := AddSource(db, "s"); err != nil {
t.Fatal(err)
}
updated, err := GetLastUpdated(db, "s")
if err != nil {
t.Fatalf("failed to get lastUpdated: %v", err)
}
if updated != time.Unix(0, 0).UTC() {
t.Fatalf("expected epoch time, got %v", updated)
}
now := time.Now().UTC().Round(time.Second)
_, _, err = UpdateWithFetchedItems(db, "s", nil, nil, now)
if err != nil {
t.Fatal(err)
}
updated, err = GetLastUpdated(db, "s")
if err != nil {
t.Fatalf("failed to get lastUpdated: %v", err)
}
if updated != now {
t.Fatalf("incorrect last updated time\nnow: %v\ngot: %v", now, updated)
}
}

View File

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