feat(backend): add age condition for state versions retention
All checks were successful
main / main (push) Successful in 2m47s
main / deploy (push) Has been skipped
main / publish (push) Has been skipped

Closes #59
This commit is contained in:
Julien Dessaux 2025-05-01 14:27:49 +02:00
parent 5d7b540718
commit 0a63e1f593
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 32 additions and 20 deletions

View file

@ -28,12 +28,13 @@ func initDB(ctx context.Context, url string) (*sql.DB, error) {
}
type DB struct {
ctx context.Context
dataEncryptionKey scrypto.AES256Key
readDB *sql.DB
sessionsSalt scrypto.AES256Key
versionsHistoryLimit int
writeDB *sql.DB
ctx context.Context
dataEncryptionKey scrypto.AES256Key
readDB *sql.DB
sessionsSalt scrypto.AES256Key
versionsHistoryLimit int
versionsHistoryMinimumDays int
writeDB *sql.DB
}
func NewDB(ctx context.Context, url string, getenv func(string) string) (*DB, error) {
@ -60,10 +61,11 @@ func NewDB(ctx context.Context, url string, getenv func(string) string) (*DB, er
writeDB.SetMaxOpenConns(1)
db := DB{
ctx: ctx,
readDB: readDB,
versionsHistoryLimit: 64,
writeDB: writeDB,
ctx: ctx,
readDB: readDB,
versionsHistoryLimit: 128,
versionsHistoryMinimumDays: 28,
writeDB: writeDB,
}
pragmas := []struct {
key string
@ -103,6 +105,12 @@ func NewDB(ctx context.Context, url string, getenv func(string) string) (*DB, er
return nil, fmt.Errorf("failed to parse the TFSTATED_VERSIONS_HISTORY_LIMIT environment variable, expected an integer: %w", err)
}
}
versionsHistoryMinimumDays := getenv("TFSTATED_VERSIONS_HISTORY_MINIMUM_DAYS")
if versionsHistoryMinimumDays != "" {
if db.versionsHistoryMinimumDays, err = strconv.Atoi(versionsHistoryMinimumDays); err != nil {
return nil, fmt.Errorf("failed to parse the TFSTATED_VERSIONS_HISTORY_MINIMUM_DAYS environment variable, expected an integer: %w", err)
}
}
return &db, nil
}