diff --git a/go.mod b/go.mod index 45e6021..0edd5e6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 31ad3d1..b956652 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internals/config/externalconfig/postgres_repository.go b/internals/config/externalconfig/postgres_repository.go index 514c12b..d66c00e 100644 --- a/internals/config/externalconfig/postgres_repository.go +++ b/internals/config/externalconfig/postgres_repository.go @@ -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