add item.last_arrived field

This commit is contained in:
nkanaev
2026-04-27 21:05:25 +01:00
parent 49c704037b
commit 760f611007
3 changed files with 52 additions and 3 deletions

View File

@@ -135,14 +135,15 @@ func (s *Storage) CreateItems(items []Item) bool {
insert into items ( insert into items (
guid, feed_id, title, link, date, guid, feed_id, title, link, date,
content, media_links, content, media_links,
date_arrived, status date_arrived, last_arrived, status
) )
values ( values (
:guid, :feed_id, :title, :link, strftime('%Y-%m-%d %H:%M:%f', :date), :guid, :feed_id, :title, :link, strftime('%Y-%m-%d %H:%M:%f', :date),
:content, :media_links, :content, :media_links,
:date_arrived, :status :date_arrived, :last_arrived, :status
) )
on conflict (feed_id, guid) do nothing`, on conflict (feed_id, guid) do update set
last_arrived = :last_arrived`,
sql.Named("guid", item.GUID), sql.Named("guid", item.GUID),
sql.Named("feed_id", item.FeedId), sql.Named("feed_id", item.FeedId),
sql.Named("title", item.Title), sql.Named("title", item.Title),
@@ -151,6 +152,7 @@ func (s *Storage) CreateItems(items []Item) bool {
sql.Named("content", item.Content), sql.Named("content", item.Content),
sql.Named("media_links", item.MediaLinks), sql.Named("media_links", item.MediaLinks),
sql.Named("date_arrived", now), sql.Named("date_arrived", now),
sql.Named("last_arrived", now),
sql.Named("status", UNREAD), sql.Named("status", UNREAD),
) )
if err != nil { if err != nil {

View File

@@ -6,6 +6,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"testing" "testing"
"testing/synctest"
"time" "time"
) )
@@ -374,3 +375,42 @@ func TestDeleteOldItems(t *testing.T) {
) )
} }
} }
func TestCreateItemsLastArrived(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
db := testDB()
defer db.db.Close()
feed := db.CreateFeed("test feed", "", "", "http://example.com/feed", nil)
item := Item{
GUID: "item1",
FeedId: feed.Id,
Title: "Title 1",
Date: time.Now(),
}
// 1. Initial creation
db.CreateItems([]Item{item})
var lastArrived1 time.Time
err := db.db.QueryRow("select last_arrived from items where guid = ?", item.GUID).Scan(&lastArrived1)
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second * 10)
// 2. Update on conflict
db.CreateItems([]Item{item})
var lastArrived2 time.Time
err = db.db.QueryRow("select last_arrived from items where guid = ?", item.GUID).Scan(&lastArrived2)
if err != nil {
t.Fatal(err)
}
if !lastArrived2.After(lastArrived1) {
t.Errorf("expected last_arrived to be updated. old: %v, new: %v", lastArrived1, lastArrived2)
}
})
}

View File

@@ -18,6 +18,7 @@ var migrations = []func(*sql.Tx) error{
m08_normalize_datetime, m08_normalize_datetime,
m09_change_item_index, m09_change_item_index,
m10_add_item_medialinks, m10_add_item_medialinks,
m11_add_item_last_arrived,
} }
var maxVersion = int64(len(migrations)) var maxVersion = int64(len(migrations))
@@ -332,3 +333,9 @@ func m10_add_item_medialinks(tx *sql.Tx) error {
_, err := tx.Exec(sql) _, err := tx.Exec(sql)
return err return err
} }
func m11_add_item_last_arrived(tx *sql.Tx) error {
sql := `alter table items add column last_arrived datetime`
_, err := tx.Exec(sql)
return err
}