Skip to content

Commit

Permalink
Fixed external config max versions deletion (#205)
Browse files Browse the repository at this point in the history
* Refactor the way of creating request for division into two parts

* translate

* update go.sum
  • Loading branch information
Ismail731404 authored Oct 23, 2024
1 parent cda7f1b commit 70dba8a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/jmoiron/sqlx v1.4.0
github.com/json-iterator/go v1.1.12
github.com/lib/pq v1.10.9
github.com/myrteametrics/myrtea-sdk/v5 v5.1.0
github.com/myrteametrics/myrtea-sdk/v5 v5.1.1
github.com/prataprc/goparsec v0.0.0-20211219142520-daac0e635e7e
github.com/prometheus/client_golang v1.20.2
github.com/robfig/cron/v3 v3.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/myrteametrics/myrtea-sdk/v5 v5.1.0 h1:ezJ8c4pIfRft5/OM1mBB0yb2meU5eVSn+hjy6nvd77k=
github.com/myrteametrics/myrtea-sdk/v5 v5.1.0/go.mod h1:wJJ0R7p8VPtuqvJyYVl+eoxodkYL4IQ06e3zJnjiEIw=
github.com/myrteametrics/myrtea-sdk/v5 v5.1.1 h1:U7PsPEAfIkVneWLybtaB3RhYYATHvtWaXlXnpSxwOb4=
github.com/myrteametrics/myrtea-sdk/v5 v5.1.1/go.mod h1:wJJ0R7p8VPtuqvJyYVl+eoxodkYL4IQ06e3zJnjiEIw=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
Expand Down
24 changes: 17 additions & 7 deletions internals/config/externalconfig/postgres_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,27 @@ func (r *PostgresRepository) Update(id int64, externalConfig models.ExternalConf
maxVersions := viper.GetInt("MAX_EXTERNAL_CONFIG_VERSIONS_TO_KEEP")

if versionCount > maxVersions {

subQuery, _, err := r.newStatement().
Select("created_at").
From("external_generic_config_versions_v1").
Where("config_id = ?", id).
OrderBy("created_at DESC").
Offset(uint64(maxVersions)).
Limit(1).
ToSql()

if err != nil {
tx.Rollback() // Cancel transaction in case of error
return err
}

_, err = r.newStatement().
Delete("external_generic_config_versions_v1").
Where("config_id = ?", id).
Where("created_at < ?", r.newStatement().
Select("created_at").
From("external_generic_config_versions_v1").
Where("config_id = ?", id).
OrderBy("created_at DESC").
Offset(uint64(maxVersions)).
Limit(1)).
Where("created_at < (" + subQuery + ")").
Exec()

if err != nil {
tx.Rollback() // Cancel transaction in case of error
return err
Expand Down

0 comments on commit 70dba8a

Please sign in to comment.