summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2025-01-12 00:15:52 +0100
committerJulien Dessaux2025-01-12 00:15:52 +0100
commitaef0b00fb9c2236bbc3fa85e486e2b44cba494d3 (patch)
treea40dfe2ae1f71eed6dcb1f0bc8b94428d1b1b770
parentchore(tfstated): fix test and update dependencies (diff)
downloadtfstated-aef0b00fb9c2236bbc3fa85e486e2b44cba494d3.tar.gz
tfstated-aef0b00fb9c2236bbc3fa85e486e2b44cba494d3.tar.bz2
tfstated-aef0b00fb9c2236bbc3fa85e486e2b44cba494d3.zip
feat(tfstated): store created and updated timestamps for states
-rw-r--r--pkg/database/sql/000_init.sql4
-rw-r--r--pkg/database/states.go16
2 files changed, 15 insertions, 5 deletions
diff --git a/pkg/database/sql/000_init.sql b/pkg/database/sql/000_init.sql
index e14142b..0bcdee1 100644
--- a/pkg/database/sql/000_init.sql
+++ b/pkg/database/sql/000_init.sql
@@ -26,7 +26,9 @@ CREATE TABLE sessions (
CREATE TABLE states (
id INTEGER PRIMARY KEY,
path TEXT NOT NULL,
- lock TEXT
+ lock TEXT,
+ created INTEGER DEFAULT (unixepoch()),
+ updated INTEGER DEFAULT (unixepoch())
) STRICT;
CREATE UNIQUE INDEX states_path on states(path);
diff --git a/pkg/database/states.go b/pkg/database/states.go
index 74839da..d88e717 100644
--- a/pkg/database/states.go
+++ b/pkg/database/states.go
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"slices"
+ "time"
)
// returns true in case of successful deletion
@@ -46,7 +47,7 @@ func (db *DB) GetState(path string) ([]byte, error) {
func (db *DB) SetState(path string, accountID int, data []byte, lockID string) (bool, error) {
encryptedData, err := db.dataEncryptionKey.EncryptAES256(data)
if err != nil {
- return false, err
+ return false, fmt.Errorf("failed to encrypt state data: %w", err)
}
ret := false
return ret, db.WithTransaction(func(tx *sql.Tx) error {
@@ -59,11 +60,11 @@ func (db *DB) SetState(path string, accountID int, data []byte, lockID string) (
var result sql.Result
result, err = tx.ExecContext(db.ctx, `INSERT INTO states(path) VALUES (?)`, path)
if err != nil {
- return err
+ return fmt.Errorf("failed to insert new state: %w", err)
}
stateID, err = result.LastInsertId()
if err != nil {
- return err
+ return fmt.Errorf("failed to get last insert id for new state: %w", err)
}
} else {
return err
@@ -84,7 +85,14 @@ func (db *DB) SetState(path string, accountID int, data []byte, lockID string) (
sql.Named("stateID", stateID),
sql.Named("data", encryptedData))
if err != nil {
- return err
+ return fmt.Errorf("failed to insert new state version: %w", err)
+ }
+ _, err = tx.ExecContext(db.ctx,
+ `UPDATE states SET updated = ? WHERE id = ?;`,
+ time.Now().UTC().Unix(),
+ stateID)
+ if err != nil {
+ return fmt.Errorf("failed to touch updated for state: %w", err)
}
_, err = tx.ExecContext(db.ctx,
`DELETE FROM versions