Skip to content

Commit 69390cc

Browse files
authored
Merge pull request #6297 from devtron-labs/app-gitopsconfig-migrate
chore: deployment config methods add
2 parents 962dbfe + 1fe5b1e commit 69390cc

File tree

2 files changed

+107
-7
lines changed

2 files changed

+107
-7
lines changed

internal/sql/repository/deploymentConfig/repository.go

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2020-2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package deploymentConfig
218

319
import (
@@ -29,7 +45,7 @@ type DeploymentConfig struct {
2945
ConfigType string `sql:"config_type"`
3046
RepoUrl string `sql:"repo_url"`
3147
RepoName string `sql:"repo_name"`
32-
ReleaseMode string `json:"release_mode"`
48+
ReleaseMode string `sql:"release_mode"`
3349
Active bool `sql:"active,notnull"`
3450
sql.AuditLog
3551
}
@@ -46,6 +62,7 @@ type Repository interface {
4662
GetAppAndEnvLevelConfigsInBulk(appIdToEnvIdsMap map[int][]int) ([]*DeploymentConfig, error)
4763
GetByAppIdAndEnvIdEvenIfInactive(appId, envId int) (*DeploymentConfig, error)
4864
UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId, envId int) error
65+
GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error)
4966
}
5067

5168
type RepositoryImpl struct {
@@ -86,14 +103,19 @@ func (impl *RepositoryImpl) Update(tx *pg.Tx, config *DeploymentConfig) (*Deploy
86103
return config, err
87104
}
88105

89-
func (impl *RepositoryImpl) UpdateAll(tx *pg.Tx, config []*DeploymentConfig) ([]*DeploymentConfig, error) {
106+
func (impl *RepositoryImpl) UpdateAll(tx *pg.Tx, configs []*DeploymentConfig) ([]*DeploymentConfig, error) {
90107
var err error
91-
if tx != nil {
92-
err = tx.Update(config)
93-
} else {
94-
err = impl.dbConnection.Update(config)
108+
for _, config := range configs {
109+
if tx != nil {
110+
_, err = tx.Model(config).WherePK().UpdateNotNull()
111+
} else {
112+
_, err = impl.dbConnection.Model(&config).UpdateNotNull()
113+
}
114+
if err != nil {
115+
return nil, err
116+
}
95117
}
96-
return config, err
118+
return configs, err
97119
}
98120

99121
func (impl *RepositoryImpl) GetById(id int) (*DeploymentConfig, error) {
@@ -172,3 +194,12 @@ func (impl *RepositoryImpl) UpdateRepoUrlByAppIdAndEnvId(repoUrl string, appId,
172194
Update()
173195
return err
174196
}
197+
198+
func (impl *RepositoryImpl) GetConfigByAppIds(appIds []int) ([]*DeploymentConfig, error) {
199+
var results []*DeploymentConfig
200+
err := impl.dbConnection.Model(&results).
201+
Where("app_id in (?) ", pg.In(appIds)).
202+
Where("active = ?", true).
203+
Select()
204+
return results, err
205+
}

pkg/deployment/common/deploymentConfigService.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2020-2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package common
218

319
import (
@@ -18,6 +34,7 @@ import (
1834

1935
type DeploymentConfigService interface {
2036
CreateOrUpdateConfig(tx *pg.Tx, config *bean.DeploymentConfig, userId int32) (*bean.DeploymentConfig, error)
37+
CreateOrUpdateConfigInBulk(tx *pg.Tx, configToBeCreated, configToBeUpdated []*bean.DeploymentConfig, userId int32) error
2138
IsDeploymentConfigUsed() bool
2239
GetConfigForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error)
2340
GetAndMigrateConfigIfAbsentForDevtronApps(appId, envId int) (*bean.DeploymentConfig, error)
@@ -27,6 +44,7 @@ type DeploymentConfigService interface {
2744
GetAppLevelConfigForDevtronApp(appId int) (*bean.DeploymentConfig, error)
2845
UpdateRepoUrlForAppAndEnvId(repoURL string, appId, envId int) error
2946
GetDeploymentAppTypeForCDInBulk(pipelines []*pipelineConfig.Pipeline) (map[int]string, error)
47+
GetConfigsByAppIds(appIds []int) ([]*bean.DeploymentConfig, error)
3048
}
3149

3250
type DeploymentConfigServiceImpl struct {
@@ -90,6 +108,41 @@ func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfig(tx *pg.Tx, config
90108
return ConvertDeploymentConfigDbObjToDTO(newDBObj), nil
91109
}
92110

111+
func (impl *DeploymentConfigServiceImpl) CreateOrUpdateConfigInBulk(tx *pg.Tx, configToBeCreated, configToBeUpdated []*bean.DeploymentConfig, userId int32) error {
112+
113+
dbObjCreate := make([]*deploymentConfig.DeploymentConfig, 0, len(configToBeCreated))
114+
for i := range configToBeCreated {
115+
dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeCreated[i])
116+
dbObj.AuditLog.CreateAuditLog(userId)
117+
dbObjCreate = append(dbObjCreate, dbObj)
118+
}
119+
120+
dbObjUpdate := make([]*deploymentConfig.DeploymentConfig, 0, len(configToBeUpdated))
121+
for i := range configToBeUpdated {
122+
dbObj := ConvertDeploymentConfigDTOToDbObj(configToBeUpdated[i])
123+
dbObj.AuditLog.UpdateAuditLog(userId)
124+
dbObjUpdate = append(dbObjUpdate, dbObj)
125+
}
126+
127+
if len(dbObjCreate) > 0 {
128+
_, err := impl.deploymentConfigRepository.SaveAll(tx, dbObjCreate)
129+
if err != nil {
130+
impl.logger.Errorw("error in saving deploymentConfig", "dbObjCreate", dbObjCreate, "err", err)
131+
return err
132+
}
133+
}
134+
135+
if len(dbObjUpdate) > 0 {
136+
_, err := impl.deploymentConfigRepository.UpdateAll(tx, dbObjUpdate)
137+
if err != nil {
138+
impl.logger.Errorw("error in updating deploymentConfig", "dbObjUpdate", dbObjUpdate, "err", err)
139+
return err
140+
}
141+
}
142+
143+
return nil
144+
}
145+
93146
func (impl *DeploymentConfigServiceImpl) IsDeploymentConfigUsed() bool {
94147
return impl.deploymentServiceTypeConfig.UseDeploymentConfigData
95148
}
@@ -478,3 +531,19 @@ func (impl *DeploymentConfigServiceImpl) GetDeploymentAppTypeForCDInBulk(pipelin
478531
}
479532
return resp, nil
480533
}
534+
535+
func (impl *DeploymentConfigServiceImpl) GetConfigsByAppIds(appIds []int) ([]*bean.DeploymentConfig, error) {
536+
if len(appIds) == 0 {
537+
return nil, nil
538+
}
539+
configs, err := impl.deploymentConfigRepository.GetConfigByAppIds(appIds)
540+
if err != nil {
541+
impl.logger.Errorw("error in getting deployment config db object by appIds", "appIds", appIds, "err", err)
542+
return nil, err
543+
}
544+
resp := make([]*bean.DeploymentConfig, 0, len(configs))
545+
for _, config := range configs {
546+
resp = append(resp, ConvertDeploymentConfigDbObjToDTO(config))
547+
}
548+
return resp, nil
549+
}

0 commit comments

Comments
 (0)