Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db): fixed select query that ignored eslversions with deleted flag #2053

Merged
merged 21 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ba42f51
Added/Fixed some traces
diogo-nogueira-freiheit Oct 15, 2024
bc2bc17
Add some logs for dev-env
diogo-nogueira-freiheit Oct 16, 2024
5b934b8
removed bad spans
diogo-nogueira-freiheit Oct 16, 2024
ce0abab
removed useless comment
diogo-nogueira-freiheit Oct 16, 2024
b314140
Merge branch 'main' into dn/double_commits
diogo-nogueira-freiheit Oct 16, 2024
92a1c49
Removed useless call to WorkDir
diogo-nogueira-freiheit Oct 16, 2024
a91fcb5
Merge branch 'dn/double_commits' of github.com:freiheit-com/kuberpult…
diogo-nogueira-freiheit Oct 16, 2024
965d7c1
Solved linting issue
diogo-nogueira-freiheit Oct 16, 2024
b6da59f
New spans
diogo-nogueira-freiheit Oct 16, 2024
cc16567
removed trah
diogo-nogueira-freiheit Oct 16, 2024
e591b23
Useless empty lines removed
diogo-nogueira-freiheit Oct 16, 2024
aff7d6f
Reseted mock flag
diogo-nogueira-freiheit Oct 16, 2024
d3a4a71
fix(db): select query was ignoring recent eslversion if they were del…
diogo-nogueira-freiheit Oct 17, 2024
dd36ff3
Fixed some stuf
diogo-nogueira-freiheit Oct 17, 2024
01569f4
Merge branch 'main' into dn/double_commits
diogo-nogueira-freiheit Oct 17, 2024
da8c7dd
Added new test for deleted release case
diogo-nogueira-freiheit Oct 17, 2024
10313c2
Fixed test cases and added new one
diogo-nogueira-freiheit Oct 17, 2024
4a79c5e
Fixed span names
diogo-nogueira-freiheit Oct 17, 2024
dc97e65
Added ordering to the queries
diogo-nogueira-freiheit Oct 17, 2024
6acc325
Fixed transformer db test
diogo-nogueira-freiheit Oct 17, 2024
6a5c579
Fixed integration test
diogo-nogueira-freiheit Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,26 +789,53 @@ func (h *DBHandler) processReleaseManifestRows(ctx context.Context, err error, r
return result, nil
}

func (h *DBHandler) DBSelectReleasesByApp(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
// DBSelectReleasesByAppLatestEslVersion returns the latest eslversion
// for each release of an app. It includes deleted releases and loads manifests.
func (h *DBHandler) DBSelectReleasesByAppLatestEslVersion(ctx context.Context, tx *sql.Tx, app string, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByApp")
diogo-nogueira-freiheit marked this conversation as resolved.
Show resolved Hide resolved
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, appName, metadata, manifests, releaseVersion, deleted, environments " +
" FROM releases " +
" WHERE appName=? AND deleted=?" +
" ORDER BY releaseVersion DESC, eslVersion DESC, created DESC;"))
selectQuery := h.AdaptQuery(
`SELECT
releases.eslVersion,
releases.created,
releases.appName,
releases.metadata,
releases.manifests,
releases.releaseVersion,
releases.deleted,
releases.environments
FROM (
SELECT
MAX(eslVersion) AS latestEslVersion,
appname,
releaseversion
FROM
releases
WHERE
appname=?
GROUP BY
appname, releaseversion
) as currentEslReleases
JOIN
releases
ON
currentEslReleases.appname = releases.appname
AND
currentEslReleases.latesteslversion = releases.eslversion
AND
currentEslReleases.releaseversion = releases.releaseversion;`,
diogo-nogueira-freiheit marked this conversation as resolved.
Show resolved Hide resolved
)
span.SetTag("query", selectQuery)
rows, err := tx.QueryContext(
ctx,
selectQuery,
app,
deleted,
)

return h.processReleaseRows(ctx, err, rows, ignorePrepublishes, true)
}

func (h *DBHandler) DBSelectReleasesByAppLatestEslVersion(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
func (h *DBHandler) DBSelectReleasesByAppOrderedByEslVersion(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByApp")
diogo-nogueira-freiheit marked this conversation as resolved.
Show resolved Hide resolved
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
Expand Down
30 changes: 29 additions & 1 deletion pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,34 @@ func TestReadReleasesByApp(t *testing.T) {
},
},
},
{
Name: "Retrieve deleted release",
Releases: []DBReleaseWithMetaData{
{
EslVersion: 1,
ReleaseNumber: 10,
App: "app1",
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}},
},
{
EslVersion: 2,
ReleaseNumber: 10,
App: "app1",
Deleted: true,
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}},
},
},
AppName: "app1",
Expected: []*DBReleaseWithMetaData{
{
EslVersion: 2,
ReleaseNumber: 10,
App: "app1",
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}},
Environments: []string{"dev"},
},
},
},
{
Name: "Retrieve multiple releases",
Releases: []DBReleaseWithMetaData{
Expand Down Expand Up @@ -2525,7 +2553,7 @@ func TestReadReleasesByApp(t *testing.T) {
return fmt.Errorf("error while writing release, error: %w", err)
}
}
releases, err := dbHandler.DBSelectReleasesByApp(ctx, transaction, tc.AppName, false, !tc.RetrievePrepublishes)
releases, err := dbHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, tc.AppName, !tc.RetrievePrepublishes)
if err != nil {
return fmt.Errorf("error while selecting release, error: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func (c *CreateApplicationVersion) Transform(
sortedKeys := sorting.SortKeys(c.Manifests)

if state.DBHandler.ShouldUseOtherTables() {
prevRelease, err := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, c.Application, false, false)
prevRelease, err := state.DBHandler.DBSelectReleasesByAppOrderedByEslVersion(ctx, transaction, c.Application, false, false)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -1711,7 +1711,7 @@ func (u *DeleteEnvFromApp) Transform(
return "", err
}
if state.DBHandler.ShouldUseOtherTables() {
releases, err := state.DBHandler.DBSelectReleasesByApp(ctx, transaction, u.Application, false, true)
releases, err := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, u.Application, true)
if err != nil {
return "", err
}
Expand Down
Loading