feat(backend): add age condition for state versions retention
Closes #59
This commit is contained in:
parent
5d7b540718
commit
0a63e1f593
3 changed files with 32 additions and 20 deletions
|
@ -36,6 +36,8 @@ func TestMain(m *testing.M) {
|
||||||
return "a528D1m9q3IZxLinSmHmeKxrx3Pmm7GQ3nBzIDxjr0A="
|
return "a528D1m9q3IZxLinSmHmeKxrx3Pmm7GQ3nBzIDxjr0A="
|
||||||
case "TFSTATED_VERSIONS_HISTORY_LIMIT":
|
case "TFSTATED_VERSIONS_HISTORY_LIMIT":
|
||||||
return "3"
|
return "3"
|
||||||
|
case "TFSTATED_VERSIONS_HISTORY_MINIMUM_DAYS":
|
||||||
|
return "0"
|
||||||
default:
|
default:
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,12 +28,13 @@ func initDB(ctx context.Context, url string) (*sql.DB, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
dataEncryptionKey scrypto.AES256Key
|
dataEncryptionKey scrypto.AES256Key
|
||||||
readDB *sql.DB
|
readDB *sql.DB
|
||||||
sessionsSalt scrypto.AES256Key
|
sessionsSalt scrypto.AES256Key
|
||||||
versionsHistoryLimit int
|
versionsHistoryLimit int
|
||||||
writeDB *sql.DB
|
versionsHistoryMinimumDays int
|
||||||
|
writeDB *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDB(ctx context.Context, url string, getenv func(string) string) (*DB, error) {
|
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)
|
writeDB.SetMaxOpenConns(1)
|
||||||
|
|
||||||
db := DB{
|
db := DB{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
readDB: readDB,
|
readDB: readDB,
|
||||||
versionsHistoryLimit: 64,
|
versionsHistoryLimit: 128,
|
||||||
writeDB: writeDB,
|
versionsHistoryMinimumDays: 28,
|
||||||
|
writeDB: writeDB,
|
||||||
}
|
}
|
||||||
pragmas := []struct {
|
pragmas := []struct {
|
||||||
key string
|
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)
|
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
|
return &db, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,19 +244,21 @@ func (db *DB) SetState(path string, accountId uuid.UUID, data []byte, lock strin
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to touch updated for state: %w", err)
|
return fmt.Errorf("failed to touch updated for state: %w", err)
|
||||||
}
|
}
|
||||||
|
min := time.Now().Add(time.Duration(db.versionsHistoryMinimumDays) * -24 * time.Hour)
|
||||||
_, err = tx.ExecContext(db.ctx,
|
_, err = tx.ExecContext(db.ctx,
|
||||||
`DELETE FROM versions
|
`DELETE FROM versions
|
||||||
WHERE state_id = (SELECT id
|
WHERE state_id = (SELECT id
|
||||||
FROM states
|
FROM states
|
||||||
WHERE path = :path)
|
WHERE path = :path)
|
||||||
AND id < (SELECT MIN(id)
|
AND id < (SELECT MIN(id)
|
||||||
FROM(SELECT versions.id
|
FROM(SELECT versions.id
|
||||||
FROM versions
|
FROM versions
|
||||||
JOIN states ON states.id = versions.state_id
|
JOIN states ON states.id = versions.state_id
|
||||||
WHERE states.path = :path
|
WHERE states.path = :path AND versions.created < :min
|
||||||
ORDER BY versions.id DESC
|
ORDER BY versions.id DESC
|
||||||
LIMIT :limit));`,
|
LIMIT :limit));`,
|
||||||
sql.Named("limit", db.versionsHistoryLimit),
|
sql.Named("limit", db.versionsHistoryLimit),
|
||||||
|
sql.Named("min", min),
|
||||||
sql.Named("path", path),
|
sql.Named("path", path),
|
||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue