aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/data/update.go
diff options
context:
space:
mode:
authorJP Appel <jeanpierre.appel01@gmail.com>2025-07-19 19:01:39 -0400
committerJP Appel <jeanpierre.appel01@gmail.com>2025-07-19 19:01:39 -0400
commitfaf35ef54885bc48b897508ce3cb40b868ff505b (patch)
tree5ff54c8a73567df3c139e0471f7882971fb0f91e /pkg/data/update.go
parent08434926f5eda975808f66b45b3a828458d5ba7e (diff)
Add key-val db table for metadata
Diffstat (limited to 'pkg/data/update.go')
-rw-r--r--pkg/data/update.go32
1 files changed, 23 insertions, 9 deletions
diff --git a/pkg/data/update.go b/pkg/data/update.go
index a606256..a8a4b3d 100644
--- a/pkg/data/update.go
+++ b/pkg/data/update.go
@@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"fmt"
+ "log/slog"
+ "time"
"github.com/jpappel/atlas/pkg/index"
)
@@ -17,19 +19,15 @@ type Update struct {
type UpdateMany struct {
Docs map[int64]*index.Document
- pathDocs map[string]*index.Document
+ PathDocs map[string]*index.Document
tx *sql.Tx
- db *sql.DB
+ Db *sql.DB
}
func NewUpdate(ctx context.Context, db *sql.DB, doc index.Document) Update {
return Update{Doc: doc, db: db}
}
-func NewUpdateMany(db *sql.DB, docs map[string]*index.Document) UpdateMany {
- return UpdateMany{pathDocs: docs, db: db}
-}
-
// Replace a document if its filetime is newer than the one in the database.
func (u *Update) Update(ctx context.Context) error {
var err error
@@ -59,37 +57,53 @@ func (u *Update) Update(ctx context.Context) error {
return err
}
+ if _, err := u.tx.Exec("INSERT OR REPLACE INTO Info(key,value,updated) VALUES (?,?,?)",
+ "lastUpdate", "singleUpdate", time.Now().UTC().Unix(),
+ ); err != nil {
+ return err
+ }
+
return u.tx.Commit()
}
func (u *UpdateMany) Update(ctx context.Context) error {
var err error
- u.tx, err = u.db.BeginTx(ctx, nil)
+ u.tx, err = u.Db.BeginTx(ctx, nil)
if err != nil {
return err
}
hasUpdates, err := u.documents()
if !hasUpdates || err != nil {
+ slog.Debug("Error updating documents")
u.tx.Rollback()
return err
}
if err := u.tags(); err != nil {
+ slog.Debug("Error updating tags")
u.tx.Rollback()
return err
}
if err := u.links(); err != nil {
+ slog.Debug("Error updating links")
u.tx.Rollback()
return err
}
if err := u.authors(); err != nil {
+ slog.Debug("Error updating authors")
u.tx.Rollback()
return err
}
+ if _, err := u.tx.Exec("INSERT OR REPLACE INTO Info(key,value,updated) VALUES (?,?,?)",
+ "lastUpdate", "multiUpdate", time.Now().UTC().Unix(),
+ ); err != nil {
+ return err
+ }
+
return u.tx.Commit()
}
@@ -159,7 +173,7 @@ func (u *UpdateMany) documents() (bool, error) {
}
defer tempInsertStmt.Close()
- for path, doc := range u.pathDocs {
+ for path, doc := range u.PathDocs {
filetime := sql.NullInt64{
Int64: doc.FileTime.Unix(),
Valid: !doc.FileTime.IsZero(),
@@ -223,7 +237,7 @@ func (u *UpdateMany) documents() (bool, error) {
if err := updates.Scan(&id, &path); err != nil {
return false, err
}
- u.Docs[id] = u.pathDocs[path]
+ u.Docs[id] = u.PathDocs[path]
hasUpdate = true
}