From 4020344eda4d2a988f11dc60ac7c55a97aa39774 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 13 Nov 2024 21:08:48 +0100 Subject: chore(tfstated): rename state "name" to "path" for consistency --- pkg/database/states.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'pkg/database/states.go') diff --git a/pkg/database/states.go b/pkg/database/states.go index 5536533..d1e9c7d 100644 --- a/pkg/database/states.go +++ b/pkg/database/states.go @@ -8,8 +8,8 @@ import ( ) // returns true in case of successful deletion -func (db *DB) DeleteState(name string) (bool, error) { - result, err := db.Exec(`DELETE FROM states WHERE name = ?;`, name) +func (db *DB) DeleteState(path string) (bool, error) { + result, err := db.Exec(`DELETE FROM states WHERE path = ?;`, path) if err != nil { return false, err } @@ -20,16 +20,16 @@ func (db *DB) DeleteState(name string) (bool, error) { return n == 1, nil } -func (db *DB) GetState(name string) ([]byte, error) { +func (db *DB) GetState(path string) ([]byte, error) { var encryptedData []byte err := db.QueryRow( `SELECT versions.data FROM versions JOIN states ON states.id = versions.state_id - WHERE states.name = ? + WHERE states.path = ? ORDER BY versions.id DESC LIMIT 1;`, - name).Scan(&encryptedData) + path).Scan(&encryptedData) if err != nil { if errors.Is(err, sql.ErrNoRows) { return []byte{}, nil @@ -43,7 +43,7 @@ func (db *DB) GetState(name string) ([]byte, error) { } // returns true in case of id mismatch -func (db *DB) SetState(name string, data []byte, lockID string) (bool, error) { +func (db *DB) SetState(path string, data []byte, lockID string) (bool, error) { encryptedData, err := db.dataEncryptionKey.EncryptAES256(data) if err != nil { return false, err @@ -61,10 +61,10 @@ func (db *DB) SetState(name string, data []byte, lockID string) (bool, error) { stateID int64 lockData []byte ) - if err = tx.QueryRowContext(db.ctx, `SELECT id, lock->>'ID' FROM states WHERE name = ?;`, name).Scan(&stateID, &lockData); err != nil { + if err = tx.QueryRowContext(db.ctx, `SELECT id, lock->>'ID' FROM states WHERE path = ?;`, path).Scan(&stateID, &lockData); err != nil { if errors.Is(err, sql.ErrNoRows) { var result sql.Result - result, err = tx.ExecContext(db.ctx, `INSERT INTO states(name) VALUES (?)`, name) + result, err = tx.ExecContext(db.ctx, `INSERT INTO states(path) VALUES (?)`, path) if err != nil { return false, err } @@ -95,16 +95,16 @@ func (db *DB) SetState(name string, data []byte, lockID string) (bool, error) { `DELETE FROM versions WHERE state_id = (SELECT id FROM states - WHERE name = :name) + WHERE path = :path) AND id < (SELECT MIN(id) FROM(SELECT versions.id FROM versions JOIN states ON states.id = versions.state_id - WHERE states.name = :name + WHERE states.path = :path ORDER BY versions.id DESC LIMIT :limit));`, sql.Named("limit", db.versionsHistoryLimit), - sql.Named("name", name), + sql.Named("path", path), ) if err != nil { return false, err -- cgit v1.2.3