Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: Sql query fixes #6097

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions internal/sql/repository/AppListingRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ func (impl AppListingRepositoryImpl) FetchJobs(appIds []int, statuses []string,
impl.Logger.Error(appsErr)
return jobContainers, appsErr
}
jobContainers = impl.extractEnvironmentNameFromId(jobContainers)
jobContainers, err := impl.extractEnvironmentNameFromId(jobContainers)
if err != nil {
impl.Logger.Errorw("Error in extractEnvironmentNameFromId", "jobContainers", jobContainers, "err", err)
return nil, err
}
return jobContainers, nil
}

Expand All @@ -137,7 +141,11 @@ func (impl AppListingRepositoryImpl) FetchOverviewCiPipelines(jobId int) ([]*bea
impl.Logger.Error(appsErr)
return jobContainers, appsErr
}
jobContainers = impl.extractEnvironmentNameFromId(jobContainers)
jobContainers, err := impl.extractEnvironmentNameFromId(jobContainers)
if err != nil {
impl.Logger.Errorw("Error in extractEnvironmentNameFromId", "jobContainers", jobContainers, "err", err)
return nil, err
}
return jobContainers, nil
}

Expand Down Expand Up @@ -732,7 +740,7 @@ func (impl AppListingRepositoryImpl) FindAppCount(isProd bool) (int, error) {
return count, nil
}

func (impl AppListingRepositoryImpl) extractEnvironmentNameFromId(jobContainers []*bean.JobListingContainer) []*bean.JobListingContainer {
func (impl AppListingRepositoryImpl) extractEnvironmentNameFromId(jobContainers []*bean.JobListingContainer) ([]*bean.JobListingContainer, error) {
var envIds []*int
for _, job := range jobContainers {
if job.EnvironmentId != 0 {
Expand All @@ -742,7 +750,11 @@ func (impl AppListingRepositoryImpl) extractEnvironmentNameFromId(jobContainers
envIds = append(envIds, &job.LastTriggeredEnvironmentId)
}
}
envs, _ := impl.environmentRepository.FindByIds(envIds)
envs, err := impl.environmentRepository.FindByIds(envIds)
if err != nil {
impl.Logger.Errorw("Error in getting environment", "envIds", envIds, "err", err)
return nil, err
}

envIdNameMap := make(map[int]string)

Expand All @@ -759,5 +771,5 @@ func (impl AppListingRepositoryImpl) extractEnvironmentNameFromId(jobContainers
}
}

return jobContainers
return jobContainers, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func (impl *InstalledAppVersionHistoryRepositoryImpl) UpdateDeploymentHistoryMes
_, err := impl.dbConnection.Model((*InstalledAppVersionHistory)(nil)).
Set("message = ?", message).
Where("id = ?", installedAppVersionHistoryId).
Where("active = ?", true).
Update()
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cluster/EnvironmentService.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func (impl EnvironmentServiceImpl) FindByIds(ids []*int) ([]*bean2.EnvironmentBe
models, err := impl.environmentRepository.FindByIds(ids)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
return nil, err
}
var beans []*bean2.EnvironmentBean
for _, model := range models {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cluster/repository/EnvironmentRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ func (repositoryImpl EnvironmentRepositoryImpl) FindByClusterId(clusterId int) (

func (repositoryImpl EnvironmentRepositoryImpl) FindByIds(ids []*int) ([]*Environment, error) {
var apps []*Environment
if len(ids) == 0 {
return []*Environment{}, nil
}
err := repositoryImpl.dbConnection.Model(&apps).Where("active = ?", true).Where("id in (?)", pg.In(ids)).Select()
return apps, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (impl ExternalLinkIdentifierMappingRepositoryImpl) FindAllActiveByLinkIdent
elim.id as mapping_id,elim.active,elim.type,elim.identifier,elim.env_id,elim.app_id,elim.cluster_id
FROM external_link el
LEFT JOIN external_link_identifier_mapping elim ON el.id = elim.external_link_id
WHERE el.active = true and elim.active = true and ( (elim.type = %d and elim.identifier = '%s' and elim.cluster_id = 0) or (elim.type = 0 and elim.app_id = 0 and elim.cluster_id = %d)
WHERE el.active = true and elim.active = true and ( (elim.type = ? and elim.identifier = ? and elim.cluster_id = 0) or (elim.type = 0 and elim.app_id = 0 and elim.cluster_id = ?)
or (elim.type = -1) );`
queryParams = append(queryParams, TypeMappings[linkIdentifier.Type], linkIdentifier.Identifier, clusterId)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/pipeline/DeploymentPipelineConfigService.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func (impl *CdPipelineConfigServiceImpl) GetCdPipelineById(pipelineId int) (cdPi
impl.logger.Errorw("error in fetching pipeline", "err", err)
return cdPipeline, err
}
if environment == nil || environment.Id == 0 {
impl.logger.Errorw("environment doesn't exists", "environmentId", dbPipeline.EnvironmentId)
return cdPipeline, err
}
strategies, err := impl.pipelineConfigRepository.GetAllStrategyByPipelineId(dbPipeline.Id)
if err != nil && errors.IsNotFound(err) {
impl.logger.Errorw("error in fetching strategies", "err", err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/security/policyService.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,10 @@ func (impl *PolicyServiceImpl) GetPolicies(policyLevel securityBean.PolicyLevel,
envId = append(envId, &pipeline.EnvironmentId)
}
envs, err := impl.environmentService.FindByIds(envId)
if err != nil {
impl.logger.Errorw("Error in fetching env", "envId", envId, "err", err)
return nil, err
}
for _, env := range envs {
cvePolicy, severityPolicy, err := impl.getPolicies(policyLevel, env.ClusterId, env.Id, appId)
if err != nil {
Expand Down
Loading