Skip to content

Commit

Permalink
chore(db): use deploymentMetadata from Database (#1646)
Browse files Browse the repository at this point in the history
for example in the overview service
  • Loading branch information
sven-urbanski-freiheit-com authored May 29, 2024
1 parent 32f85fd commit bfa62d5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
30 changes: 24 additions & 6 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,27 +199,45 @@ func Remove(s []string, r string) []string {
return s
}

// WithTransaction opens a transaction, runs `f` and then calls either Commit or Rollback
// WithTransaction opens a transaction, runs `f` and then calls either Commit or Rollback.
// Use this if the only thing to return from `f` is an error.
func (h *DBHandler) WithTransaction(ctx context.Context, f DBFunction) error {
tx, err := h.DB.BeginTx(ctx, nil)
_, err := WithTransactionT(h, ctx, func(ctx context.Context, transaction *sql.Tx) (*interface{}, error) {
err2 := f(ctx, transaction)
if err2 != nil {
return nil, err2
}
return nil, nil
})
if err != nil {
return err
}
return nil
}

type DBFunctionT[T any] func(ctx context.Context, transaction *sql.Tx) (*T, error)

// WithTransactionT is the same as WithTransaction, but you can also return data, not just the error.
func WithTransactionT[T any](h *DBHandler, ctx context.Context, f DBFunctionT[T]) (*T, error) {
tx, err := h.DB.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer func(tx *sql.Tx) {
_ = tx.Rollback()
// we ignore the error returned from Rollback() here,
// because it is always set when Commit() was successful
}(tx)

err = f(ctx, tx)
result, err := f(ctx, tx)
if err != nil {
return err
return nil, err
}
err = tx.Commit()
if err != nil {
return err
return nil, err
}
return nil
return result, nil
}

type EventType string
Expand Down
15 changes: 14 additions & 1 deletion services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,20 @@ func (s *State) GetEnvironmentTeamLocks(environment, team string) (map[string]Lo
return result, nil
}
}
func (s *State) GetDeploymentMetaData(environment, application string) (string, time.Time, error) {
func (s *State) GetDeploymentMetaData(ctx context.Context, environment, application string) (string, time.Time, error) {
if s.DBHandler.ShouldUseOtherTables() {
result, err := db.WithTransactionT(s.DBHandler, ctx, func(ctx context.Context, transaction *sql.Tx) (*db.Deployment, error) {
return s.DBHandler.DBSelectDeployment(ctx, transaction, application, environment)
})
if err != nil {
return "", time.Time{}, err
}
return result.Metadata.DeployedByEmail, result.Created, nil
}
return s.GetDeploymentMetaDataFromRepo(environment, application)
}

func (s *State) GetDeploymentMetaDataFromRepo(environment, application string) (string, time.Time, error) {
base := s.Filesystem.Join("environments", environment, "applications", application)
author, err := readFile(s.Filesystem, s.Filesystem.Join(base, "deployed_by"))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func UpdateDatadogMetrics(ctx context.Context, state *State, repo Repository, ch
for _, app := range entries {
GaugeEnvAppLockMetric(filesystem, env, app.Name())

_, deployedAtTimeUtc, err := state.GetDeploymentMetaData(env, app.Name())
_, deployedAtTimeUtc, err := state.GetDeploymentMetaData(ctx, env, app.Name())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion services/cd-service/pkg/service/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func (o *OverviewServiceServer) getOverview(
}
}
}
deployAuthor, deployTime, err := s.GetDeploymentMetaData(envName, appName)
deployAuthor, deployTime, err := s.GetDeploymentMetaData(ctx, envName, appName)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion services/cd-service/pkg/service/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (o *VersionServiceServer) GetVersion(
}
if version != nil {
res.Version = *version
_, deployedAt, err := state.GetDeploymentMetaData(in.Environment, in.Application)
_, deployedAt, err := state.GetDeploymentMetaData(ctx, in.Environment, in.Application)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit bfa62d5

Please sign in to comment.