feat(tfstated): preserve lock information in states versions

This commit is contained in:
Julien Dessaux 2024-10-16 00:17:12 +02:00
parent 5959766cbd
commit 0e58781daa
Signed by: adyxax
GPG key ID: F92E51B86E07177E
2 changed files with 8 additions and 1 deletions

View file

@ -13,6 +13,7 @@ CREATE TABLE versions (
id INTEGER PRIMARY KEY,
state_id INTEGER,
data BLOB,
lock TEXT,
created INTEGER DEFAULT (unixepoch()),
FOREIGN KEY(state_id) REFERENCES states(id) ON DELETE CASCADE
) STRICT;

View file

@ -74,7 +74,13 @@ func (db *DB) SetState(name string, data []byte, lockID string) (bool, error) {
err = fmt.Errorf("failed to update state, lock ID does not match")
return true, err
}
_, err = tx.ExecContext(db.ctx, `INSERT INTO versions(state_id, data) VALUES (?, ?);`, stateID, encryptedData)
_, err = tx.ExecContext(db.ctx,
`INSERT INTO versions(state_id, data, lock)
SELECT :stateID, :data, lock
FROM states
WHERE states.id = :stateID;`,
sql.Named("stateID", stateID),
sql.Named("data", encryptedData))
if err != nil {
return false, err
}