Skip to content
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
45 changes: 38 additions & 7 deletions internal/sql/repository/deploymentConfig/repository.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -29,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
}
Expand All @@ -46,6 +62,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 {
Expand Down Expand Up @@ -86,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) {
Expand Down Expand Up @@ -172,3 +194,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
}
69 changes: 69 additions & 0 deletions pkg/deployment/common/deploymentConfigService.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -18,6 +34,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)
Expand All @@ -27,6 +44,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 {
Expand Down Expand Up @@ -90,6 +108,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, 0, len(configToBeCreated))
for i := range configToBeCreated {
dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeCreated[i])
dbObj.AuditLog.CreateAuditLog(userId)
dbObjCreate = append(dbObjCreate, dbObj)
}

dbObjUpdate := make([]*deploymentConfig.DeploymentConfig, 0, len(configToBeUpdated))
for i := range configToBeUpdated {
dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeUpdated[i])
dbObj.AuditLog.UpdateAuditLog(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
}
Expand Down Expand Up @@ -478,3 +531,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
}
Loading