From 2babf81a86bc8bbcaffd0266e915dbf7e1c394ef Mon Sep 17 00:00:00 2001 From: kartik-579 Date: Thu, 16 Jan 2025 16:59:15 +0530 Subject: [PATCH 1/5] added common code --- .../repository/deploymentConfig/repository.go | 10 ++++ .../common/deploymentConfigService.go | 53 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/internal/sql/repository/deploymentConfig/repository.go b/internal/sql/repository/deploymentConfig/repository.go index 0dac42b98b..38b4d68f3f 100644 --- a/internal/sql/repository/deploymentConfig/repository.go +++ b/internal/sql/repository/deploymentConfig/repository.go @@ -46,6 +46,7 @@ type Repository interface { GetAppAndEnvLevelConfigsInBulk(appIdToEnvIdsMap map[int][]int) ([]*DeploymentConfig, error) GetByAppIdAndEnvIdEvenIfInactive(appId, envId int) (*DeploymentConfig, error) UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId, envId int) error + GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error) } type RepositoryImpl struct { @@ -172,3 +173,12 @@ func (impl *RepositoryImpl) UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId, Update() return err } + +func (impl *RepositoryImpl) GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error) { + var results []*DeploymentConfig + err := impl.dbConnection.Model(&results). + Where("app_id in (?) ", pg.In(appIds)). + Where("active = ?", true). + Select() + return results, err +} diff --git a/pkg/deployment/common/deploymentConfigService.go b/pkg/deployment/common/deploymentConfigService.go index 86b8ddcc10..e8b1330f1e 100644 --- a/pkg/deployment/common/deploymentConfigService.go +++ b/pkg/deployment/common/deploymentConfigService.go @@ -18,6 +18,7 @@ import ( type DeploymentConfigService interface { CreateOrUpdateConfig(tx *pg.Tx, config *bean.DeploymentConfig, userId int32) (*bean.DeploymentConfig, error) + CreateOrUpdateConfigInBulk(tx *pg.Tx, configToBeCreated, configToBeUpdated []*bean.DeploymentConfig, userId int32) error IsDeploymentConfigUsed() bool GetConfigForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error) GetAndMigrateConfigIfAbsentForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error) @@ -27,6 +28,7 @@ type DeploymentConfigService interface { GetAppLevelConfigForDevtronApp(appId int) (*bean.DeploymentConfig, error) UpdateRepoUrlForAppAndEnvId(repoURL string, appId, envId int) error GetDeploymentAppTypeForCDInBulk(pipelines []*pipelineConfig.Pipeline) (map[int]string, error) + GetConfigsByAppIds(appIds []int) ([]*bean.DeploymentConfig, error) } type DeploymentConfigServiceImpl struct { @@ -90,6 +92,41 @@ func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfig(tx *pg.Tx, config return ConvertDeploymentConfigDbObjToDTO(newDBObj), nil } +func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfigInBulk(tx *pg.Tx, configToBeCreated, configToBeUpdated []*bean.DeploymentConfig, userId int32) error { + + dbObjCreate := make([]*deploymentConfig.DeploymentConfig, len(configToBeCreated)) + for i := range configToBeCreated { + dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeCreated[i]) + dbObj.AuditLog.CreateAuditLog(userId) + dbObjCreate = append(dbObjCreate, dbObj) + } + + dbObjUpdate := make([]*deploymentConfig.DeploymentConfig, len(configToBeUpdated)) + for i := range configToBeUpdated { + dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeUpdated[i]) + dbObj.AuditLog.CreateAuditLog(userId) + dbObjUpdate = append(dbObjUpdate, dbObj) + } + + if len(dbObjCreate) > 0 { + _, err := impl.deploymentConfigRepository.SaveAll(tx, dbObjCreate) + if err != nil { + impl.logger.Errorw("error in saving deploymentConfig", "dbObjCreate", dbObjCreate, "err", err) + return err + } + } + + if len(dbObjUpdate) > 0 { + _, err := impl.deploymentConfigRepository.UpdateAll(tx, dbObjUpdate) + if err != nil { + impl.logger.Errorw("error in updating deploymentConfig", "dbObjUpdate", dbObjUpdate, "err", err) + return err + } + } + + return nil +} + func (impl *DeploymentConfigServiceImpl) IsDeploymentConfigUsed() bool { return impl.deploymentServiceTypeConfig.UseDeploymentConfigData } @@ -478,3 +515,19 @@ func (impl *DeploymentConfigServiceImpl) GetDeploymentAppTypeForCDInBulk(pipelin } return resp, nil } + +func (impl *DeploymentConfigServiceImpl) GetConfigsByAppIds(appIds []int) ([]*bean.DeploymentConfig, error) { + if len(appIds) == 0 { + return nil, nil + } + configs, err := impl.deploymentConfigRepository.GetConfigByAppIds(appIds) + if err != nil { + impl.logger.Errorw("error in getting deployment config db object by appIds", "appIds", appIds, "err", err) + return nil, err + } + resp := make([]*bean.DeploymentConfig, 0, len(configs)) + for _, config := range configs { + resp = append(resp, ConvertDeploymentConfigDbObjToDTO(config)) + } + return resp, nil +} From dbfa22f23f9feec8cbee9ca577fc6bcbebdfab76 Mon Sep 17 00:00:00 2001 From: kartik-579 Date: Thu, 16 Jan 2025 17:00:03 +0530 Subject: [PATCH 2/5] added license --- .../repository/deploymentConfig/repository.go | 16 ++++++++++++++++ pkg/deployment/common/deploymentConfigService.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/internal/sql/repository/deploymentConfig/repository.go b/internal/sql/repository/deploymentConfig/repository.go index 38b4d68f3f..9734f189ba 100644 --- a/internal/sql/repository/deploymentConfig/repository.go +++ b/internal/sql/repository/deploymentConfig/repository.go @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2020-2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package deploymentConfig import ( diff --git a/pkg/deployment/common/deploymentConfigService.go b/pkg/deployment/common/deploymentConfigService.go index e8b1330f1e..ccba7228c9 100644 --- a/pkg/deployment/common/deploymentConfigService.go +++ b/pkg/deployment/common/deploymentConfigService.go @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2020-2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package common import ( From 084df2b26cca297c28b71928c40ad2ab9ee2ae4e Mon Sep 17 00:00:00 2001 From: kartik-579 Date: Wed, 22 Jan 2025 11:43:46 +0530 Subject: [PATCH 3/5] oss/ent change --- .../repository/deploymentConfig/repository.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/sql/repository/deploymentConfig/repository.go b/internal/sql/repository/deploymentConfig/repository.go index 9734f189ba..45dad95627 100644 --- a/internal/sql/repository/deploymentConfig/repository.go +++ b/internal/sql/repository/deploymentConfig/repository.go @@ -45,7 +45,7 @@ type DeploymentConfig struct { ConfigType string `sql:"config_type"` RepoUrl string `sql:"repo_url"` RepoName string `sql:"repo_name"` - ReleaseMode string `json:"release_mode"` + ReleaseMode string `sql:"release_mode"` Active bool `sql:"active,notnull"` sql.AuditLog } @@ -103,14 +103,19 @@ func (impl *RepositoryImpl) Update(tx *pg.Tx, config *DeploymentConfig) (*Deploy return config, err } -func (impl *RepositoryImpl) UpdateAll(tx *pg.Tx, config []*DeploymentConfig) ([]*DeploymentConfig, error) { +func (impl *RepositoryImpl) UpdateAll(tx *pg.Tx, configs []*DeploymentConfig) ([]*DeploymentConfig, error) { var err error - if tx != nil { - err = tx.Update(config) - } else { - err = impl.dbConnection.Update(config) + for _, config := range configs { + if tx != nil { + _, err = tx.Model(config).WherePK().UpdateNotNull() + } else { + _, err = impl.dbConnection.Model(&config).UpdateNotNull() + } + if err != nil { + return nil, err + } } - return config, err + return configs, err } func (impl *RepositoryImpl) GetById(id int) (*DeploymentConfig, error) { From 627beeb92cd6ec8739652c7eeabb22009fd0c466 Mon Sep 17 00:00:00 2001 From: kartik-579 Date: Wed, 22 Jan 2025 11:46:22 +0530 Subject: [PATCH 4/5] oss/ent change --- pkg/deployment/common/deploymentConfigService.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/deployment/common/deploymentConfigService.go b/pkg/deployment/common/deploymentConfigService.go index ccba7228c9..60f9e846c6 100644 --- a/pkg/deployment/common/deploymentConfigService.go +++ b/pkg/deployment/common/deploymentConfigService.go @@ -120,7 +120,7 @@ func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfigInBulk(tx *pg.Tx, c dbObjUpdate := make([]*deploymentConfig.DeploymentConfig, len(configToBeUpdated)) for i := range configToBeUpdated { dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeUpdated[i]) - dbObj.AuditLog.CreateAuditLog(userId) + dbObj.AuditLog.UpdateAuditLog(userId) dbObjUpdate = append(dbObjUpdate, dbObj) } From cc36d746776517fbc349efca437ac06b5265597d Mon Sep 17 00:00:00 2001 From: kartik-579 Date: Wed, 22 Jan 2025 11:47:00 +0530 Subject: [PATCH 5/5] oss/ent change --- pkg/deployment/common/deploymentConfigService.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/deployment/common/deploymentConfigService.go b/pkg/deployment/common/deploymentConfigService.go index 60f9e846c6..2d4d5bd4fb 100644 --- a/pkg/deployment/common/deploymentConfigService.go +++ b/pkg/deployment/common/deploymentConfigService.go @@ -110,14 +110,14 @@ func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfig(tx *pg.Tx, config func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfigInBulk(tx *pg.Tx, configToBeCreated, configToBeUpdated []*bean.DeploymentConfig, userId int32) error { - dbObjCreate := make([]*deploymentConfig.DeploymentConfig, len(configToBeCreated)) + dbObjCreate := make([]*deploymentConfig.DeploymentConfig, 0, len(configToBeCreated)) for i := range configToBeCreated { dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeCreated[i]) dbObj.AuditLog.CreateAuditLog(userId) dbObjCreate = append(dbObjCreate, dbObj) } - dbObjUpdate := make([]*deploymentConfig.DeploymentConfig, len(configToBeUpdated)) + dbObjUpdate := make([]*deploymentConfig.DeploymentConfig, 0, len(configToBeUpdated)) for i := range configToBeUpdated { dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeUpdated[i]) dbObj.AuditLog.UpdateAuditLog(userId)