@@ -54,6 +54,7 @@ type AppRepository interface {
5454 UpdateWithTxn (app * App , tx * pg.Tx ) error
5555 SetDescription (id int , description string , userId int32 ) error
5656 FindActiveByName (appName string ) (pipelineGroup * App , err error )
57+ FindAllActiveByName (appName string ) ([]* App , error )
5758 FindAppIdByName (appName string ) (int , error )
5859
5960 FindJobByDisplayName (appName string ) (pipelineGroup * App , err error )
@@ -133,35 +134,24 @@ func (repo AppRepositoryImpl) SetDescription(id int, description string, userId
133134}
134135
135136func (repo AppRepositoryImpl ) FindActiveByName (appName string ) (* App , error ) {
137+ pipelineGroup := & App {}
138+ err := repo .dbConnection .
139+ Model (pipelineGroup ).
140+ Where ("app_name = ?" , appName ).
141+ Where ("active = ?" , true ).
142+ Order ("id DESC" ).Limit (1 ).
143+ Select ()
144+ return pipelineGroup , err
145+ }
146+
147+ func (repo AppRepositoryImpl ) FindAllActiveByName (appName string ) ([]* App , error ) {
136148 var apps []* App
137149 err := repo .dbConnection .
138150 Model (& apps ).
139151 Where ("app_name = ?" , appName ).
140152 Where ("active = ?" , true ).
141- Order ("id DESC" ).
142153 Select ()
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
154+ return apps , err
165155}
166156
167157func (repo AppRepositoryImpl ) FindAppIdByName (appName string ) (int , error ) {
@@ -349,52 +339,9 @@ func (repo AppRepositoryImpl) FindAppAndProjectByAppName(appName string) (*App,
349339 Where ("app.app_name = ?" , appName ).
350340 Where ("app.active=?" , true ).
351341 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- }
371342 return app , err
372343}
373344
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-
398345func (repo AppRepositoryImpl ) FindAllMatchesByAppName (appName string , appType helper.AppType ) ([]* App , error ) {
399346 var apps []* App
400347 var err error
0 commit comments