diff options
author | Julien Dessaux | 2025-01-22 00:46:49 +0100 |
---|---|---|
committer | Julien Dessaux | 2025-01-22 00:46:49 +0100 |
commit | 09885ef1e4b7610d05377596f02d08c5079c0434 (patch) | |
tree | 9c8322ea718f1eb721a4c685cb95386fa84bb797 /pkg/database | |
parent | chore(webui): refactor login and session middleware handling (diff) | |
download | tfstated-09885ef1e4b7610d05377596f02d08c5079c0434.tar.gz tfstated-09885ef1e4b7610d05377596f02d08c5079c0434.tar.bz2 tfstated-09885ef1e4b7610d05377596f02d08c5079c0434.zip |
Diffstat (limited to '')
-rw-r--r-- | pkg/database/db.go | 4 | ||||
-rw-r--r-- | pkg/database/states.go | 30 |
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) |