@@ -40,6 +40,10 @@ type App struct {
4040 sql.AuditLog
4141}
4242
43+ const (
44+ SYSTEM_USER_ID = 1
45+ )
46+
4347func (r * App ) IsAppJobOrExternalType () bool {
4448 return len (r .DisplayName ) > 0
4549}
@@ -129,16 +133,37 @@ func (repo AppRepositoryImpl) SetDescription(id int, description string, userId
129133}
130134
131135func (repo AppRepositoryImpl ) FindActiveByName (appName string ) (* App , error ) {
132- pipelineGroup := & App {}
136+ var apps [] * App
133137 err := repo .dbConnection .
134- Model (pipelineGroup ).
138+ Model (& apps ).
135139 Where ("app_name = ?" , appName ).
136140 Where ("active = ?" , true ).
137- Order ("id DESC" ).Limit ( 1 ).
141+ Order ("id DESC" ).
138142 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
141165}
166+
142167func (repo AppRepositoryImpl ) FindAppIdByName (appName string ) (int , error ) {
143168 app := & App {}
144169 err := repo .dbConnection .
@@ -324,9 +349,52 @@ func (repo AppRepositoryImpl) FindAppAndProjectByAppName(appName string) (*App,
324349 Where ("app.app_name = ?" , appName ).
325350 Where ("app.active=?" , true ).
326351 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+ }
327371 return app , err
328372}
329373
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+
330398func (repo AppRepositoryImpl ) FindAllMatchesByAppName (appName string , appType helper.AppType ) ([]* App , error ) {
331399 var apps []* App
332400 var err error
0 commit comments