diff options
Diffstat (limited to '')
-rw-r--r-- | cmd/tfstated/post_test.go | 2 | ||||
-rw-r--r-- | pkg/database/locks.go | 12 | ||||
-rw-r--r-- | pkg/database/sql/000_init.sql | 4 | ||||
-rw-r--r-- | pkg/database/states.go | 22 |
4 files changed, 20 insertions, 20 deletions
diff --git a/cmd/tfstated/post_test.go b/cmd/tfstated/post_test.go index 6eb68f2..fd66dc4 100644 --- a/cmd/tfstated/post_test.go +++ b/cmd/tfstated/post_test.go @@ -53,7 +53,7 @@ func TestPost(t *testing.T) { err := db.QueryRow(`SELECT COUNT(versions.id) FROM versions JOIN states ON states.id = versions.state_id - WHERE states.name = "/test_post"`).Scan(&n) + WHERE states.path = "/test_post"`).Scan(&n) if err != nil { t.Fatalf("failed to count versions for the /test_post state: %s", err) } diff --git a/pkg/database/locks.go b/pkg/database/locks.go index 82b66cd..6951337 100644 --- a/pkg/database/locks.go +++ b/pkg/database/locks.go @@ -9,7 +9,7 @@ import ( // Atomically check the lock status of a state and lock it if unlocked. Returns // true if the function locked the state, otherwise returns false and the lock // parameter is updated to the value of the existing lock -func (db *DB) SetLockOrGetExistingLock(name string, lock any) (bool, error) { +func (db *DB) SetLockOrGetExistingLock(path string, lock any) (bool, error) { tx, err := db.Begin() if err != nil { return false, err @@ -20,12 +20,12 @@ func (db *DB) SetLockOrGetExistingLock(name string, lock any) (bool, error) { } }() var lockData []byte - if err = tx.QueryRowContext(db.ctx, `SELECT lock FROM states WHERE name = ?;`, name).Scan(&lockData); err != nil { + if err = tx.QueryRowContext(db.ctx, `SELECT lock FROM states WHERE path = ?;`, path).Scan(&lockData); err != nil { if errors.Is(err, sql.ErrNoRows) { if lockData, err = json.Marshal(lock); err != nil { return false, err } - _, err = tx.ExecContext(db.ctx, `INSERT INTO states(name, lock) VALUES (?, json(?))`, name, lockData) + _, err = tx.ExecContext(db.ctx, `INSERT INTO states(path, lock) VALUES (?, json(?))`, path, lockData) if err != nil { return false, err } @@ -43,7 +43,7 @@ func (db *DB) SetLockOrGetExistingLock(name string, lock any) (bool, error) { if lockData, err = json.Marshal(lock); err != nil { return false, err } - _, err = tx.ExecContext(db.ctx, `UPDATE states SET lock = json(?) WHERE name = ?;`, lockData, name) + _, err = tx.ExecContext(db.ctx, `UPDATE states SET lock = json(?) WHERE path = ?;`, lockData, path) if err != nil { return false, err } @@ -51,12 +51,12 @@ func (db *DB) SetLockOrGetExistingLock(name string, lock any) (bool, error) { return true, err } -func (db *DB) Unlock(name, lock any) (bool, error) { +func (db *DB) Unlock(path, lock any) (bool, error) { data, err := json.Marshal(lock) if err != nil { return false, err } - result, err := db.Exec(`UPDATE states SET lock = NULL WHERE name = ? and lock = json(?);`, name, data) + result, err := db.Exec(`UPDATE states SET lock = NULL WHERE path = ? and lock = json(?);`, path, data) if err != nil { return false, err } diff --git a/pkg/database/sql/000_init.sql b/pkg/database/sql/000_init.sql index 9c9c07c..ab40746 100644 --- a/pkg/database/sql/000_init.sql +++ b/pkg/database/sql/000_init.sql @@ -4,10 +4,10 @@ CREATE TABLE schema_version ( CREATE TABLE states ( id INTEGER PRIMARY KEY, - name TEXT NOT NULL, + path TEXT NOT NULL, lock TEXT ) STRICT; -CREATE UNIQUE INDEX states_name on states(name); +CREATE UNIQUE INDEX states_path on states(path); CREATE TABLE versions ( id INTEGER PRIMARY KEY, 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 |