Skip to content

Commit

Permalink
feat: update all apps table when removing an app (#1566)
Browse files Browse the repository at this point in the history
plus some smaller changes like adding transactions to the custom
migration
  • Loading branch information
sven-urbanski-freiheit-com authored May 2, 2024
1 parent 3aedddf commit bcd285c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
72 changes: 41 additions & 31 deletions services/cd-service/pkg/repository/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"errors"
"fmt"
"github.com/freiheit-com/kuberpult/pkg/logger"
"go.uber.org/zap"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"path"
"reflect"
Expand Down Expand Up @@ -168,17 +167,25 @@ func PostgresToSqliteQuery(query string) string {

type DBFunction func(ctx context.Context) error

func Remove(s []string, r string) []string {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}

// WithTransaction opens a transaction, runs `f` and then calls either Commit or Rollback
func (h *DBHandler) WithTransaction(ctx context.Context, f DBFunction) error {
tx, err := h.DB.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func(tx *sql.Tx) {
err := tx.Rollback()
if err != nil {
logger.FromContext(ctx).Warn("db.rollback.failed", zap.Error(err))
}
_ = tx.Rollback()
// we ignore the error returned from Rollback() here,
// because it is always set when Commit() was successful
}(tx)

err = f(ctx)
Expand All @@ -199,6 +206,7 @@ func (h *DBHandler) DBWriteAllApplications(ctx context.Context, previousVersion
if err != nil {
return fmt.Errorf("could not begin transaction. Error: %w", err)
}
slices.Sort(applications) // we don't really *need* the sorting, it's just for convenience
jsonToInsert, _ := json.Marshal(AllApplicationsJson{
Apps: applications,
})
Expand Down Expand Up @@ -263,34 +271,36 @@ func (h *DBHandler) RunCustomMigrations(ctx context.Context, repo Repository) er
}

func (h *DBHandler) RunCustomMigrationAllTables(ctx context.Context, repo Repository) error {
l := logger.FromContext(ctx).Sugar()
allAppsDb, err := h.DBSelectAllApplications(ctx)
if err != nil {
l.Warnf("could not get applications from database - assuming the manifest repo is correct: %v", err)
allAppsDb = nil
}
return h.WithTransaction(ctx, func(ctx context.Context) error {
l := logger.FromContext(ctx).Sugar()
allAppsDb, err := h.DBSelectAllApplications(ctx)
if err != nil {
l.Warnf("could not get applications from database - assuming the manifest repo is correct: %v", err)
allAppsDb = nil
}

allAppsRepo, err := repo.State().GetApplicationsFromFile()
if err != nil {
return fmt.Errorf("could not get applications to run custom migrations: %v", err)
}
var version int64
if allAppsDb != nil {
slices.Sort(allAppsDb.Apps)
version = allAppsDb.Version
} else {
version = 1
}
slices.Sort(allAppsRepo)
allAppsRepo, err := repo.State().GetApplicationsFromFile()
if err != nil {
return fmt.Errorf("could not get applications to run custom migrations: %v", err)
}
var version int64
if allAppsDb != nil {
slices.Sort(allAppsDb.Apps)
version = allAppsDb.Version
} else {
version = 1
}
slices.Sort(allAppsRepo)

if allAppsDb != nil && reflect.DeepEqual(allAppsDb.Apps, allAppsRepo) {
// nothing to do
logger.FromContext(ctx).Sugar().Infof("Nothing to do, all apps are equal")
return nil
}
// if there is any difference, we assume the manifest wins over the database state,
// so we use `allAppsRepo`:
return h.DBWriteAllApplications(ctx, version, allAppsRepo)
if allAppsDb != nil && reflect.DeepEqual(allAppsDb.Apps, allAppsRepo) {
// nothing to do
logger.FromContext(ctx).Sugar().Infof("Nothing to do, all apps are equal")
return nil
}
// if there is any difference, we assume the manifest wins over the database state,
// so we use `allAppsRepo`:
return h.DBWriteAllApplications(ctx, version, allAppsRepo)
})
}

type AllApplicationsJson struct {
Expand Down
17 changes: 15 additions & 2 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,13 @@ func (c *CreateApplicationVersion) Transform(
err = state.DBHandler.WithTransaction(ctx, func(ctx context.Context) error {
allApps, err := state.DBHandler.DBSelectAllApplications(ctx)
if err != nil {
return GetCreateReleaseGeneralFailure(err)
return err
}
if !slices.Contains(allApps.Apps, c.Application) {
allApps.Apps = append(allApps.Apps, c.Application)
err := state.DBHandler.DBWriteAllApplications(ctx, allApps.Version, allApps.Apps)
if err != nil {
return GetCreateReleaseGeneralFailure(err)
return err
}
}
return nil
Expand Down Expand Up @@ -1071,6 +1071,19 @@ func (u *UndeployApplication) Transform(
return "", fmt.Errorf("UndeployApplication: unexpected error application '%v' environment '%v': '%w'", u.Application, env, err)
}
}
if state.DBHandler != nil {
err = state.DBHandler.WithTransaction(ctx, func(ctx context.Context) error {
applications, err := state.DBHandler.DBSelectAllApplications(ctx)
if err != nil {
return err
}
applications.Apps = Remove(applications.Apps, u.Application)
return state.DBHandler.DBWriteAllApplications(ctx, applications.Version, applications.Apps)
})
if err != nil {
return "", fmt.Errorf("UndeployApplication: could not apply transaction for application '%v': '%w'", u.Application, err)
}
}
return fmt.Sprintf("application '%v' was deleted successfully", u.Application), nil
}

Expand Down

0 comments on commit bcd285c

Please sign in to comment.