diff options
author | Julien Dessaux | 2024-10-18 00:01:50 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-10-18 00:01:50 +0200 |
commit | 1ebf0341dd42fde631eb791358ef70a3331f92fe (patch) | |
tree | eb646e3a828cc1e566f7085db38bad34a0f0e90d /pkg | |
parent | feat(tfstated): garbage collect older states (diff) | |
download | tfstated-1ebf0341dd42fde631eb791358ef70a3331f92fe.tar.gz tfstated-1ebf0341dd42fde631eb791358ef70a3331f92fe.tar.bz2 tfstated-1ebf0341dd42fde631eb791358ef70a3331f92fe.zip |
feat(tfstated): allow configuration of version history limit via environment variable
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/database/db.go | 20 | ||||
-rw-r--r-- | pkg/database/states.go | 3 |
2 files changed, 15 insertions, 8 deletions
diff --git a/pkg/database/db.go b/pkg/database/db.go index 2744570..38265ba 100644 --- a/pkg/database/db.go +++ b/pkg/database/db.go @@ -26,10 +26,11 @@ func initDB(ctx context.Context, url string) (*sql.DB, error) { } type DB struct { - ctx context.Context - dataEncryptionKey scrypto.AES256Key - readDB *sql.DB - writeDB *sql.DB + ctx context.Context + dataEncryptionKey scrypto.AES256Key + readDB *sql.DB + versionsHistoryLimit int + writeDB *sql.DB } func NewDB(ctx context.Context, url string) (*DB, error) { @@ -56,9 +57,10 @@ func NewDB(ctx context.Context, url string) (*DB, error) { writeDB.SetMaxOpenConns(1) db := DB{ - ctx: ctx, - readDB: readDB, - writeDB: writeDB, + ctx: ctx, + readDB: readDB, + versionsHistoryLimit: 64, + writeDB: writeDB, } if _, err = db.Exec("PRAGMA foreign_keys = ON"); err != nil { return nil, err @@ -101,3 +103,7 @@ func (db *DB) QueryRow(query string, args ...any) *sql.Row { func (db *DB) SetDataEncryptionKey(s string) error { return db.dataEncryptionKey.FromBase64(s) } + +func (db *DB) SetVersionsHistoryLimit(n int) { + db.versionsHistoryLimit = n +} diff --git a/pkg/database/states.go b/pkg/database/states.go index 26a1021..5536533 100644 --- a/pkg/database/states.go +++ b/pkg/database/states.go @@ -102,7 +102,8 @@ func (db *DB) SetState(name string, data []byte, lockID string) (bool, error) { JOIN states ON states.id = versions.state_id WHERE states.name = :name ORDER BY versions.id DESC - LIMIT 64));`, + LIMIT :limit));`, + sql.Named("limit", db.versionsHistoryLimit), sql.Named("name", name), ) if err != nil { |