From 4c2032e1f03ab060f7b72cd321aa4b9341460d47 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 17 Oct 2024 09:18:40 +0200 Subject: feat(tfstated): garbage collect older states --- pkg/database/states.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/database/states.go b/pkg/database/states.go index 36011ab..26a1021 100644 --- a/pkg/database/states.go +++ b/pkg/database/states.go @@ -22,7 +22,14 @@ func (db *DB) DeleteState(name string) (bool, error) { func (db *DB) GetState(name 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 = ? ORDER BY versions.id DESC LIMIT 1;`, name).Scan(&encryptedData) + err := db.QueryRow( + `SELECT versions.data + FROM versions + JOIN states ON states.id = versions.state_id + WHERE states.name = ? + ORDER BY versions.id DESC + LIMIT 1;`, + name).Scan(&encryptedData) if err != nil { if errors.Is(err, sql.ErrNoRows) { return []byte{}, nil @@ -84,7 +91,23 @@ func (db *DB) SetState(name string, data []byte, lockID string) (bool, error) { if err != nil { return false, err } - // TODO delete old states + _, err = tx.ExecContext(db.ctx, + `DELETE FROM versions + WHERE state_id = (SELECT id + FROM states + WHERE name = :name) + AND id < (SELECT MIN(id) + FROM(SELECT versions.id + FROM versions + JOIN states ON states.id = versions.state_id + WHERE states.name = :name + ORDER BY versions.id DESC + LIMIT 64));`, + sql.Named("name", name), + ) + if err != nil { + return false, err + } err = tx.Commit() return false, err } -- cgit v1.2.3