summaryrefslogtreecommitdiff
path: root/pkg/database
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/database')
-rw-r--r--pkg/database/db.go4
-rw-r--r--pkg/database/states.go30
2 files changed, 34 insertions, 0 deletions
diff --git a/pkg/database/db.go b/pkg/database/db.go
index 9aeec43..eac31eb 100644
--- a/pkg/database/db.go
+++ b/pkg/database/db.go
@@ -111,6 +111,10 @@ func (db *DB) Exec(query string, args ...any) (sql.Result, error) {
return db.writeDB.ExecContext(db.ctx, query, args...)
}
+func (db *DB) Query(query string, args ...any) (*sql.Rows, error) {
+ return db.readDB.QueryContext(db.ctx, query, args...)
+}
+
func (db *DB) QueryRow(query string, args ...any) *sql.Row {
return db.readDB.QueryRowContext(db.ctx, query, args...)
}
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)