Skip to content

Commit

Permalink
fix(db): close rows (#1648)
Browse files Browse the repository at this point in the history
And handle nil in GetDeploymentMetaData
  • Loading branch information
sven-urbanski-freiheit-com authored May 30, 2024
1 parent bfa62d5 commit e6ecb77
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 30 additions & 1 deletion pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ func (h *DBHandler) DBReadEslEventInternal(ctx context.Context, tx *sql.Tx, firs
if err != nil {
return nil, fmt.Errorf("could not query esl table from DB. Error: %w\n", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
logger.FromContext(ctx).Sugar().Warnf("row closing error: %v", err)
}
}(rows)
if rows.Next() {
var row = EslEventRow{
EslId: 0,
Expand Down Expand Up @@ -345,6 +351,12 @@ func (h *DBHandler) DBReadEslEventLaterThan(ctx context.Context, tx *sql.Tx, esl
if err != nil {
return nil, fmt.Errorf("could not query esl table from DB. Error: %w\n", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
logger.FromContext(ctx).Sugar().Warnf("row closing error: %v", err)
}
}(rows)
if rows.Next() {
var row = EslEventRow{
EslId: 0,
Expand Down Expand Up @@ -437,10 +449,15 @@ func (h *DBHandler) DBSelectAllEventsForCommit(ctx context.Context, commitHash s
span.SetTag("query", query)

rows, err := h.DB.QueryContext(ctx, query, commitHash)

if err != nil {
return nil, fmt.Errorf("Error querying DB. Error: %w\n", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
logger.FromContext(ctx).Sugar().Warnf("row closing error: %v", err)
}
}(rows)

var result []EventRow

Expand Down Expand Up @@ -556,6 +573,12 @@ func (h *DBHandler) DBSelectDeployment(ctx context.Context, tx *sql.Tx, appSelec
if err != nil {
return nil, fmt.Errorf("could not query esl table from DB. Error: %w\n", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
logger.FromContext(ctx).Sugar().Warnf("row closing error: %v", err)
}
}(rows)
if rows.Next() {
var row = DBDeployment{
EslVersion: 0,
Expand Down Expand Up @@ -609,6 +632,12 @@ func (h *DBHandler) DBSelectAnyDeployment(ctx context.Context, tx *sql.Tx) (*DBD
if err != nil {
return nil, fmt.Errorf("could not query esl table from DB. Error: %w\n", err)
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
logger.FromContext(ctx).Sugar().Warnf("row closing error: %v", err)
}
}(rows)
if rows.Next() {
var releaseVersion sql.NullInt64
//exhaustruct:ignore
Expand Down
5 changes: 4 additions & 1 deletion services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,10 @@ func (s *State) GetDeploymentMetaData(ctx context.Context, environment, applicat
if err != nil {
return "", time.Time{}, err
}
return result.Metadata.DeployedByEmail, result.Created, nil
if result != nil {
return result.Metadata.DeployedByEmail, result.Created, nil
}
return "", time.Time{}, err
}
return s.GetDeploymentMetaDataFromRepo(environment, application)
}
Expand Down

0 comments on commit e6ecb77

Please sign in to comment.