summaryrefslogtreecommitdiff
path: root/pkg/database/states.go
diff options
context:
space:
mode:
authorJulien Dessaux2025-01-22 00:46:49 +0100
committerJulien Dessaux2025-01-22 00:46:49 +0100
commit09885ef1e4b7610d05377596f02d08c5079c0434 (patch)
tree9c8322ea718f1eb721a4c685cb95386fa84bb797 /pkg/database/states.go
parentchore(webui): refactor login and session middleware handling (diff)
downloadtfstated-09885ef1e4b7610d05377596f02d08c5079c0434.tar.gz
tfstated-09885ef1e4b7610d05377596f02d08c5079c0434.tar.bz2
tfstated-09885ef1e4b7610d05377596f02d08c5079c0434.zip
feat(webui): implement states listHEADmain
Diffstat (limited to 'pkg/database/states.go')
-rw-r--r--pkg/database/states.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/pkg/database/states.go b/pkg/database/states.go
index d88e717..1bd3d76 100644
--- a/pkg/database/states.go
+++ b/pkg/database/states.go
@@ -6,6 +6,8 @@ import (
"fmt"
"slices"
"time"
+
+ "git.adyxax.org/adyxax/tfstated/pkg/model"
)
// returns true in case of successful deletion
@@ -43,6 +45,34 @@ func (db *DB) GetState(path string) ([]byte, error) {
return db.dataEncryptionKey.DecryptAES256(encryptedData)
}
+func (db *DB) LoadStatesByPath() ([]model.State, error) {
+ rows, err := db.Query(
+ `SELECT created, id, lock, path, updated FROM states;`)
+ if err != nil {
+ return nil, fmt.Errorf("failed to load states from database: %w", err)
+ }
+ defer rows.Close()
+ states := make([]model.State, 0)
+ for rows.Next() {
+ var state model.State
+ var (
+ created int64
+ updated int64
+ )
+ err = rows.Scan(&created, &state.Id, &state.Lock, &state.Path, &updated)
+ if err != nil {
+ return nil, fmt.Errorf("failed to load states from row: %w", err)
+ }
+ state.Created = time.Unix(created, 0)
+ state.Updated = time.Unix(updated, 0)
+ states = append(states, state)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, fmt.Errorf("failed to load states from rows: %w", err)
+ }
+ return states, nil
+}
+
// returns true in case of id mismatch
func (db *DB) SetState(path string, accountID int, data []byte, lockID string) (bool, error) {
encryptedData, err := db.dataEncryptionKey.EncryptAES256(data)