Skip to content

Commit

Permalink
fix(cd-service): add applications column to the environments table, f…
Browse files Browse the repository at this point in the history
…ix DeleteEnvFromApp (#1983)

Ref: SRX-GJNPF1
  • Loading branch information
AminSlk authored Sep 30, 2024
1 parent 2569696 commit f9538e0
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'environments' AND column_name='applications') THEN
ALTER TABLE IF EXISTS environments ADD COLUMN applications VARCHAR;
END IF;
END $$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS environments_new
(
created TIMESTAMP,
version BIGINT,
name VARCHAR(255),
json VARCHAR,
applications VARCHAR,
PRIMARY KEY(name, version)
);

INSERT INTO environments_new(created, version, name, json)
SELECT created, version, name, json
FROM environments;

DROP TABLE IF EXISTS environments;
ALTER TABLE environments_new RENAME TO environments;
60 changes: 40 additions & 20 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4674,17 +4674,19 @@ type DBAllEnvironmentsRow struct {
}

type DBEnvironment struct {
Created time.Time
Version int64
Name string
Config config.EnvironmentConfig
Created time.Time
Version int64
Name string
Config config.EnvironmentConfig
Applications []string
}

type DBEnvironmentRow struct {
Created time.Time
Version int64
Name string
Config string
Created time.Time
Version int64
Name string
Config string
Applications string
}

func EnvironmentFromRow(ctx context.Context, row *DBEnvironmentRow) (*DBEnvironment, error) {
Expand All @@ -4696,11 +4698,17 @@ func EnvironmentFromRow(ctx context.Context, row *DBEnvironmentRow) (*DBEnvironm
if err != nil {
return nil, fmt.Errorf("unable to unmarshal the JSON in the database, JSON: %s, error: %w", row.Config, err)
}
applications := []string{}
err = json.Unmarshal([]byte(row.Applications), &applications)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal the JSON in the database, JSON: %s, error: %w", row.Applications, err)
}
return &DBEnvironment{
Created: row.Created,
Version: row.Version,
Name: row.Name,
Config: parsedConfig,
Created: row.Created,
Version: row.Version,
Name: row.Name,
Config: parsedConfig,
Applications: applications,
}, nil
}

Expand All @@ -4710,7 +4718,7 @@ func (h *DBHandler) DBSelectEnvironment(ctx context.Context, tx *sql.Tx, environ

selectQuery := h.AdaptQuery(
`
SELECT created, version, name, json
SELECT created, version, name, json, applications
FROM environments
WHERE name=?
ORDER BY version DESC
Expand Down Expand Up @@ -4739,7 +4747,7 @@ LIMIT 1;
if rows.Next() {
//exhaustruct:ignore
row := DBEnvironmentRow{}
err := rows.Scan(&row.Created, &row.Version, &row.Name, &row.Config)
err := rows.Scan(&row.Created, &row.Version, &row.Name, &row.Config, &row.Applications)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand Down Expand Up @@ -4771,7 +4779,8 @@ SELECT
environments.created AS created,
environments.version AS version,
environments.name AS name,
environments.json AS json
environments.json AS json,
environments.applications AS applications
FROM (
SELECT
MAX(version) AS latest,
Expand Down Expand Up @@ -4817,7 +4826,7 @@ LIMIT ?
for rows.Next() {
//exhaustruct:ignore
row := DBEnvironmentRow{}
err := rows.Scan(&row.Created, &row.Version, &row.Name, &row.Config)
err := rows.Scan(&row.Created, &row.Version, &row.Name, &row.Config, &row.Applications)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand All @@ -4833,8 +4842,8 @@ LIMIT ?
return &envs, nil
}

func (h *DBHandler) DBWriteEnvironment(ctx context.Context, tx *sql.Tx, environmentName string, environmentConfig config.EnvironmentConfig) error {
span, ctx := tracer.StartSpanFromContext(ctx, "DBWriteEnvironment")
func (h *DBHandler) DBWriteEnvironment(ctx context.Context, tx *sql.Tx, environmentName string, environmentConfig config.EnvironmentConfig, applications []string) error {
span, _ := tracer.StartSpanFromContext(ctx, "DBWriteEnvironment")
defer span.Finish()

if h == nil {
Expand All @@ -4852,6 +4861,10 @@ 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)
}
applicationsJson, err := json.Marshal(applications)
if err != nil {
return fmt.Errorf("could not marshal the application names list %v, error: %w", applicationsJson, err)
}

var existingEnvironmentVersion int64
if existingEnvironment == nil {
Expand All @@ -4861,7 +4874,7 @@ func (h *DBHandler) DBWriteEnvironment(ctx context.Context, tx *sql.Tx, environm
}

insertQuery := h.AdaptQuery(
"INSERT Into environments (created, version, name, json) VALUES (?, ?, ?, ?);",
"INSERT Into environments (created, version, name, json, applications) VALUES (?, ?, ?, ?, ?);",
)
now, err := h.DBReadTransactionTimestamp(ctx, tx)
if err != nil {
Expand All @@ -4874,6 +4887,7 @@ func (h *DBHandler) DBWriteEnvironment(ctx context.Context, tx *sql.Tx, environm
existingEnvironmentVersion+1,
environmentName,
jsonToInsert,
string(applicationsJson),
)
if err != nil {
return fmt.Errorf("could not write environment %s with config %v to environments table, error: %w", environmentName, environmentConfig, err)
Expand Down Expand Up @@ -5060,10 +5074,16 @@ func (h *DBHandler) RunCustomMigrationEnvironments(ctx context.Context, getAllEn
return fmt.Errorf("could not get environments, error: %w", err)
}

allApplications, err := h.DBSelectAllApplications(ctx, transaction)
if err != nil {
return fmt.Errorf("could not get all applications, error: %w", err)
}

allEnvironmentNames := make([]string, 0)
for envName, config := range allEnvironments {
allEnvironmentNames = append(allEnvironmentNames, envName)
err = h.DBWriteEnvironment(ctx, transaction, envName, config)

err = h.DBWriteEnvironment(ctx, transaction, envName, config, allApplications.Apps)
if err != nil {
return fmt.Errorf("unable to write manifest for environment %s to the database, error: %w", envName, err)
}
Expand Down
19 changes: 12 additions & 7 deletions pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,7 @@ func TestReadWriteEnvironment(t *testing.T) {
type EnvAndConfig struct {
EnvironmentName string
EnvironmentConfig config.EnvironmentConfig
Applications []string
}
type TestCase struct {
Name string
Expand All @@ -1833,13 +1834,15 @@ func TestReadWriteEnvironment(t *testing.T) {
{
EnvironmentName: "development",
EnvironmentConfig: testutil.MakeEnvConfigLatest(nil),
Applications: []string{"app1", "app2", "app3"},
},
},
EnvToQuery: "development",
ExpectedEntry: &DBEnvironment{
Version: 1,
Name: "development",
Config: testutil.MakeEnvConfigLatest(nil),
Version: 1,
Name: "development",
Config: testutil.MakeEnvConfigLatest(nil),
Applications: []string{"app1", "app2", "app3"},
},
},
{
Expand All @@ -1848,13 +1851,15 @@ func TestReadWriteEnvironment(t *testing.T) {
{
EnvironmentName: "development",
EnvironmentConfig: testutil.MakeEnvConfigLatestWithGroup(nil, conversion.FromString("development-group")), // "elaborate config" being the env group
Applications: []string{"app1"},
},
},
EnvToQuery: "development",
ExpectedEntry: &DBEnvironment{
Version: 1,
Name: "development",
Config: testutil.MakeEnvConfigLatestWithGroup(nil, conversion.FromString("development-group")),
Version: 1,
Name: "development",
Config: testutil.MakeEnvConfigLatestWithGroup(nil, conversion.FromString("development-group")),
Applications: []string{"app1"},
},
},
{
Expand Down Expand Up @@ -1943,7 +1948,7 @@ func TestReadWriteEnvironment(t *testing.T) {

for _, envToWrite := range tc.EnvsToWrite {
err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error {
err := dbHandler.DBWriteEnvironment(ctx, transaction, envToWrite.EnvironmentName, envToWrite.EnvironmentConfig)
err := dbHandler.DBWriteEnvironment(ctx, transaction, envToWrite.EnvironmentName, envToWrite.EnvironmentConfig, envToWrite.Applications)
if err != nil {
return fmt.Errorf("error while writing environment, error: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2070,14 +2070,14 @@ func (s *State) GetEnvironmentApplicationsFromManifest(environment string) ([]st
}

func (s *State) GetEnvironmentApplicationsFromDB(ctx context.Context, transaction *sql.Tx, environment string) ([]string, error) {
applications, err := s.DBHandler.DBSelectAllApplications(ctx, transaction)
envInfo, err := s.DBHandler.DBSelectEnvironment(ctx, transaction, environment)
if err != nil {
return nil, err
}
if applications == nil {
if envInfo.Applications == nil {
return make([]string, 0), nil
}
return applications.Apps, nil
return envInfo.Applications, nil
}

// GetApplicationsFromFile returns all apps that exist in any env
Expand Down
52 changes: 51 additions & 1 deletion services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,28 @@ func (c *CreateApplicationVersion) Transform(
for i := range sortedKeys {
env := sortedKeys[i]
man := c.Manifests[env]
// Add application to the environment if not exist
if state.DBHandler.ShouldUseOtherTables() {
envInfo, err := state.DBHandler.DBSelectEnvironment(ctx, transaction, env)
if err != nil {
return "", GetCreateReleaseGeneralFailure(err)
}
found := false
if envInfo != nil && envInfo.Applications != nil {
for _, app := range envInfo.Applications {
if app == c.Application {
found = true
break
}
}
}
if envInfo != nil && !found {
err = state.DBHandler.DBWriteEnvironment(ctx, transaction, env, envInfo.Config, append(envInfo.Applications, c.Application))
if err != nil {
return "", GetCreateReleaseGeneralFailure(err)
}
}
}

err := state.checkUserPermissions(ctx, transaction, env, c.Application, auth.PermissionCreateRelease, c.Team, c.RBACConfig, true)
if err != nil {
Expand Down Expand Up @@ -1697,6 +1719,26 @@ func (u *DeleteEnvFromApp) Transform(
return "", err
}
}

env, err := state.DBHandler.DBSelectEnvironment(ctx, transaction, u.Environment)
if err != nil {
return "", fmt.Errorf("Couldn't read environment: %s from environments table, error: %w", u.Environment, err)
}
if env == nil {
return "", fmt.Errorf("Attempting to delete an environment that doesn't exist in the environments table")
}
newApps := make([]string, 0)
if env.Applications != nil {
for _, app := range env.Applications {
if app != u.Application {
newApps = append(newApps, app)
}
}
}
err = state.DBHandler.DBWriteEnvironment(ctx, transaction, env.Name, env.Config, newApps)
if err != nil {
return "", fmt.Errorf("Couldn't write environment: %s into environments table, error: %w", u.Environment, err)
}
} else {

fs := state.Filesystem
Expand Down Expand Up @@ -2673,7 +2715,15 @@ func (c *CreateEnvironment) Transform(
}
if state.DBHandler.ShouldUseOtherTables() {
// write to environments table
err := state.DBHandler.DBWriteEnvironment(ctx, transaction, c.Environment, c.Config)
allApplications, err := state.DBHandler.DBSelectAllApplications(ctx, transaction)
if err != nil {
return "", fmt.Errorf("unable to read all applications, error: %w", err)
}
environmentApplications := make([]string, 0)
if allApplications != nil {
environmentApplications = allApplications.Apps
}
err = state.DBHandler.DBWriteEnvironment(ctx, transaction, c.Environment, c.Config, environmentApplications)
if err != nil {
return "", fmt.Errorf("unable to write to the environment table, error: %w", err)
}
Expand Down
Loading

0 comments on commit f9538e0

Please sign in to comment.