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 all 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
48 changes: 38 additions & 10 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,27 +789,55 @@ 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) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByApp")
// 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, "DBSelectReleasesByAppLatestEslVersion")
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
ORDER BY currentEslReleases.releaseversion DESC;`,
)
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) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByApp")
func (h *DBHandler) DBSelectReleasesByAppOrderedByEslVersion(ctx context.Context, tx *sql.Tx, app string, deleted bool, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByAppOrderedByEslVersion")
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, appName, metadata, releaseVersion, deleted, environments " +
Expand Down
31 changes: 30 additions & 1 deletion pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,35 @@ 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,
Deleted: true,
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 +2554,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
7 changes: 4 additions & 3 deletions services/cd-service/pkg/repository/transformer_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/lib/pq"
"regexp"
"testing"
gotime "time"

"github.com/lib/pq"

"github.com/freiheit-com/kuberpult/pkg/api/v1"
"github.com/freiheit-com/kuberpult/pkg/event"

Expand Down Expand Up @@ -2264,7 +2265,7 @@ func TestDeleteEnvFromAppWithDB(t *testing.T) {
if err != nil {
return fmt.Errorf("error: %v", err)
}
releases, err2 := state.DBHandler.DBSelectReleasesByApp(ctx, transaction, appName, false, true)
releases, err2 := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, appName, true)
if err2 != nil {
return fmt.Errorf("error retrieving release: %v", err2)
}
Expand Down Expand Up @@ -3649,7 +3650,7 @@ func TestTimestampConsistency(t *testing.T) {
t.Fatalf("error mismatch on envAcceptance(-want, +got):\n%s", diff)
}
//Release
releases, err := state.DBHandler.DBSelectReleasesByApp(ctx, transaction, testAppName, false, true)
releases, err := state.DBHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, testAppName, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func callDBForLock(t *testing.T, dbHandler *db.DBHandler, ctx context.Context, e

func callDBForReleases(t *testing.T, dbHandler *db.DBHandler, ctx context.Context, appName string) []*db.DBReleaseWithMetaData {
release, err := db.WithTransactionMultipleEntriesT(dbHandler, ctx, true, func(ctx context.Context, transaction *sql.Tx) ([]*db.DBReleaseWithMetaData, error) {
return dbHandler.DBSelectReleasesByApp(ctx, transaction, appName, false, true)
return dbHandler.DBSelectReleasesByAppLatestEslVersion(ctx, transaction, appName, true)
})
if err != nil {
t.Fatalf("DBSelectReleasesByApp failed %s", err)
Expand Down
Loading