@@ -40,6 +40,10 @@ type App struct {
40
40
sql.AuditLog
41
41
}
42
42
43
+ const (
44
+ SYSTEM_USER_ID = 1
45
+ )
46
+
43
47
func (r * App ) IsAppJobOrExternalType () bool {
44
48
return len (r .DisplayName ) > 0
45
49
}
@@ -129,16 +133,37 @@ func (repo AppRepositoryImpl) SetDescription(id int, description string, userId
129
133
}
130
134
131
135
func (repo AppRepositoryImpl ) FindActiveByName (appName string ) (* App , error ) {
132
- pipelineGroup := & App {}
136
+ var apps [] * App
133
137
err := repo .dbConnection .
134
- Model (pipelineGroup ).
138
+ Model (& apps ).
135
139
Where ("app_name = ?" , appName ).
136
140
Where ("active = ?" , true ).
137
- Order ("id DESC" ).Limit ( 1 ).
141
+ Order ("id DESC" ).
138
142
Select ()
139
- // there is only single active app will be present in db with a same name.
140
- return pipelineGroup , err
143
+ if len (apps ) == 1 {
144
+ return apps [0 ], nil
145
+ } else if len (apps ) > 1 {
146
+ isHelmApp := true
147
+ for _ , app := range apps {
148
+ if app .AppType != helper .ChartStoreApp && app .AppType != helper .ExternalChartStoreApp {
149
+ isHelmApp = false
150
+ break
151
+ }
152
+ }
153
+ if isHelmApp {
154
+ err := repo .fixMultipleHelmAppsWithSameName (appName )
155
+ if err != nil {
156
+ repo .logger .Errorw ("error in fixing duplicate helm apps with same name" )
157
+ return nil , err
158
+ }
159
+ }
160
+ return apps [0 ], nil
161
+ } else {
162
+ err = pg .ErrNoRows
163
+ }
164
+ return nil , err
141
165
}
166
+
142
167
func (repo AppRepositoryImpl ) FindAppIdByName (appName string ) (int , error ) {
143
168
app := & App {}
144
169
err := repo .dbConnection .
@@ -324,9 +349,52 @@ func (repo AppRepositoryImpl) FindAppAndProjectByAppName(appName string) (*App,
324
349
Where ("app.app_name = ?" , appName ).
325
350
Where ("app.active=?" , true ).
326
351
Select ()
352
+
353
+ if err == pg .ErrMultiRows && (app .AppType == helper .ChartStoreApp || app .AppType == helper .ExternalChartStoreApp ) {
354
+ // this case can arise in helms apps only
355
+
356
+ err := repo .fixMultipleHelmAppsWithSameName (appName )
357
+ if err != nil {
358
+ repo .logger .Errorw ("error in fixing duplicate helm apps with same name" )
359
+ return nil , err
360
+ }
361
+
362
+ err = repo .dbConnection .Model (app ).Column ("Team" ).
363
+ Where ("app.app_name = ?" , appName ).
364
+ Where ("app.active=?" , true ).
365
+ Select ()
366
+ if err != nil {
367
+ repo .logger .Errorw ("error in fetching apps by name" , "appName" , appName , "err" , err )
368
+ return nil , err
369
+ }
370
+ }
327
371
return app , err
328
372
}
329
373
374
+ func (repo AppRepositoryImpl ) fixMultipleHelmAppsWithSameName (appName string ) error {
375
+ // updating installed apps setting app_id = max app_id
376
+ installAppUpdateQuery := `update installed_apps set
377
+ app_id=(select max(id) as id from app where app_name = ?)
378
+ where app_id in (select id from app where app_name= ? )`
379
+
380
+ _ , err := repo .dbConnection .Exec (installAppUpdateQuery , appName , appName )
381
+ if err != nil {
382
+ repo .logger .Errorw ("error in updating maxAppId in installedApps" , "appName" , appName , "err" , err )
383
+ return err
384
+ }
385
+
386
+ maxAppIdQuery := repo .dbConnection .Model ((* App )(nil )).ColumnExpr ("max(id)" ).
387
+ Where ("app_name = ? " , appName ).
388
+ Where ("active = ? " , true )
389
+
390
+ // deleting all apps other than app with max id
391
+ _ , err = repo .dbConnection .Model ((* App )(nil )).
392
+ Set ("active = ?" , false ).Set ("updated_by = ?" , SYSTEM_USER_ID ).Set ("updated_on = ?" , time .Now ()).
393
+ Where ("id not in (?) " , maxAppIdQuery ).Update ()
394
+
395
+ return nil
396
+ }
397
+
330
398
func (repo AppRepositoryImpl ) FindAllMatchesByAppName (appName string , appType helper.AppType ) ([]* App , error ) {
331
399
var apps []* App
332
400
var err error
0 commit comments