diff --git a/pkg/db/db.go b/pkg/db/db.go index 955b03fcd..d415195e5 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -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 diff --git a/services/cd-service/pkg/repository/repository.go b/services/cd-service/pkg/repository/repository.go index 79335ddab..579561af0 100644 --- a/services/cd-service/pkg/repository/repository.go +++ b/services/cd-service/pkg/repository/repository.go @@ -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 { diff --git a/services/cd-service/pkg/repository/transformer.go b/services/cd-service/pkg/repository/transformer.go index 165ca28a2..4d114f27c 100644 --- a/services/cd-service/pkg/repository/transformer.go +++ b/services/cd-service/pkg/repository/transformer.go @@ -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 } diff --git a/services/cd-service/pkg/service/overview.go b/services/cd-service/pkg/service/overview.go index 4fe6f0e29..4f782f4b2 100644 --- a/services/cd-service/pkg/service/overview.go +++ b/services/cd-service/pkg/service/overview.go @@ -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 } diff --git a/services/cd-service/pkg/service/version.go b/services/cd-service/pkg/service/version.go index eecf0e938..ee3a82044 100644 --- a/services/cd-service/pkg/service/version.go +++ b/services/cd-service/pkg/service/version.go @@ -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 }