Skip to content

Commit

Permalink
feat(db): string lists inserted into the databse are sorted (#2090)
Browse files Browse the repository at this point in the history
## Features:

The following columns have lists of strings which are now sorted:
* `environments.applications`
* `releases.environments`
* `all_environments.json`
The column `all_apps.json` was already being previously sorted.

## Tests:
4 new test cases were added for each of the tables testing that string
lists are ordered when inserted.
The `all_apps` table had no write and read test suite so a new one was
created with the corresponding test case.

Ref: SRX-FEUSCN
  • Loading branch information
diogo-nogueira-freiheit authored Nov 7, 2024
1 parent a56e5f7 commit ca221a2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@ func (h *DBHandler) DBInsertRelease(ctx context.Context, transaction *sql.Tx, re
envs = append(envs, env)
}
release.Environments = envs
slices.Sort(release.Environments) // we don't really *need* the sorting, it's just for convenience
environmentStr, err := json.Marshal(release.Environments)
if err != nil {
return fmt.Errorf("could not marshal release environments: %w", err)
Expand Down Expand Up @@ -5392,6 +5393,7 @@ func (h *DBHandler) DBWriteEnvironment(ctx context.Context, tx *sql.Tx, environm
if err != nil {
return fmt.Errorf("error while selecting environment %s from database, error: %w", environmentName, err)
}
slices.Sort(applications) // we don't really *need* the sorting, it's just for convenience
applicationsJson, err := json.Marshal(applications)
if err != nil {
return fmt.Errorf("could not marshal the application names list %v, error: %w", applicationsJson, err)
Expand Down Expand Up @@ -5495,6 +5497,7 @@ func (h *DBHandler) DBWriteAllEnvironments(ctx context.Context, transaction *sql
span, ctx := tracer.StartSpanFromContext(ctx, "DBWriteAllEnvironments")
defer span.Finish()

slices.Sort(environmentNames) // we don't really *need* the sorting, it's just for convenience
jsonToInsert, err := json.Marshal(environmentNames)
if err != nil {
return fmt.Errorf("could not marshal the environment names list %v, error: %w", environmentNames, err)
Expand Down
113 changes: 110 additions & 3 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,23 @@ func TestReadWriteEnvironment(t *testing.T) {
},
EnvToQuery: "development",
},
{
Name: "write one environment and read it, test that it orders applications",
EnvsToWrite: []EnvAndConfig{
{
EnvironmentName: "development",
EnvironmentConfig: testutil.MakeEnvConfigLatest(nil),
Applications: []string{"zapp", "app1", "capp"},
},
},
EnvToQuery: "development",
ExpectedEntry: &DBEnvironment{
Version: 1,
Name: "development",
Config: testutil.MakeEnvConfigLatest(nil),
Applications: []string{"app1", "capp", "zapp"},
},
},
}
for _, tc := range testCases {
tc := tc
Expand Down Expand Up @@ -2233,12 +2250,22 @@ func TestReadWriteAllEnvironments(t *testing.T) {
Name: "create entries with increasing length",
AllEnvsToWrite: [][]string{
{"development"},
{"development", "staging"},
{"development", "staging", "production"},
{"development", "production"},
{"development", "production", "staging"},
},
ExpectedEntry: &DBAllEnvironments{
Version: 3,
Environments: []string{"development", "staging", "production"},
Environments: []string{"development", "production", "staging"},
},
},
{
Name: "ensure that environments are sorted",
AllEnvsToWrite: [][]string{
{"staging", "development", "production"},
},
ExpectedEntry: &DBAllEnvironments{
Version: 1,
Environments: []string{"development", "production", "staging"},
},
},
}
Expand Down Expand Up @@ -2280,6 +2307,65 @@ func TestReadWriteAllEnvironments(t *testing.T) {
}
}

func TestReadWriteAllApplications(t *testing.T) {
type TestCase struct {
Name string
AllAppsToWrite [][]string
ExpectedEntry *AllApplicationsGo
}

testCases := []TestCase{
{
Name: "test that app are ordered",
AllAppsToWrite: [][]string{
{"my_app", "ze_app", "the_app"},
},
ExpectedEntry: &AllApplicationsGo{
Version: 1,
AllApplicationsJson: AllApplicationsJson{
Apps: []string{"my_app", "the_app", "ze_app"},
},
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
ctx := testutil.MakeTestContext()
dbHandler := setupDB(t)

for version, allApps := range tc.AllAppsToWrite {
err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error {
err := dbHandler.DBWriteAllApplications(ctx, transaction, int64(version), allApps)
if err != nil {
return fmt.Errorf("error while writing application, error: %w", err)
}
return nil
})
if err != nil {
t.Fatalf("error while running the transaction for writing all applications %v to the database, error: %v", allApps, err)
}
}

allAppsEntry, err := WithTransactionT(dbHandler, ctx, DefaultNumRetries, true, func(ctx context.Context, transaction *sql.Tx) (*AllApplicationsGo, error) {
allAppsEntry, err := dbHandler.DBSelectAllApplications(ctx, transaction)
if err != nil {
return nil, fmt.Errorf("error while selecting application entry, error: %w", err)
}
return allAppsEntry, nil
})

if err != nil {
t.Fatalf("error while running the transaction for selecting the target all applications, error: %v", err)
}
if diff := cmp.Diff(allAppsEntry, tc.ExpectedEntry, cmpopts.IgnoreFields(AllApplicationsGo{}, "Created")); diff != "" {
t.Fatalf("the received entry is different from expected\n expected: %v\n received: %v\n diff: %s\n", tc.ExpectedEntry, allAppsEntry, diff)
}
})
}
}

func TestReadReleasesByApp(t *testing.T) {

tcs := []struct {
Expand Down Expand Up @@ -2311,6 +2397,27 @@ func TestReadReleasesByApp(t *testing.T) {
},
},
},
{
Name: "Retrieved release has ordered environments",
Releases: []DBReleaseWithMetaData{
{
EslVersion: 1,
ReleaseNumber: 10,
App: "app1",
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1", "staging": "manfest2", "production": "manfest2"}},
},
},
AppName: "app1",
Expected: []*DBReleaseWithMetaData{
{
EslVersion: 1,
ReleaseNumber: 10,
App: "app1",
Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1", "staging": "manfest2", "production": "manfest2"}},
Environments: []string{"dev", "production", "staging"},
},
},
},
{
Name: "Retrieve deleted release",
Releases: []DBReleaseWithMetaData{
Expand Down

0 comments on commit ca221a2

Please sign in to comment.