@@ -77,40 +77,25 @@ func SearchLatestReport(ctx context.Context, scmID, sourceID, conditionID, targe
7777 queryString := ""
7878 var args []any
7979
80- filteredReportsQuery := psql .Select (
80+ query := psql .Select (
8181 sm .From ("pipelineReports" ),
82- sm .Columns ("id" , "data" , "config_source_ids" , "config_condition_ids" , "config_target_ids" , "target_db_scm_ids" , "created_at" , "updated_at" ),
82+ sm .Columns ("id" , "data" , "created_at" , "updated_at" ),
83+ sm .OrderBy (psql .Quote ("updated_at" )).Desc (),
8384 sm .Where (
8485 psql .Raw (fmt .Sprintf ("updated_at > current_date - interval '%d day'" , options .Days )),
8586 ),
8687 )
8788
88- query := psql .Select (
89- sm .Distinct (
90- "data -> 'ID'" ,
91- "target_db_scm_ids" ,
92- ),
93- sm .With ("filtered_reports" ).As (filteredReportsQuery ),
94- sm .Columns ("id" , "data" , "created_at" , "updated_at" ),
95- sm .From ("filtered_reports" ),
96- sm .OrderBy ("data -> 'ID'" ),
97- sm .OrderBy ("target_db_scm_ids" ),
98- sm .OrderBy (psql .Quote ("updated_at" )).Desc (),
99- )
100-
10189 if sourceID != "" {
10290 // Ensure sourceID is a valid UUID
10391 if _ , err := uuid .Parse (sourceID ); err != nil {
10492 return nil , 0 , fmt .Errorf ("parsing sourceID: %w" , err )
10593 }
10694
107- filteredReportsQuery .Apply (
95+ query .Apply (
10896 sm .Where (
10997 psql .Raw (`config_source_ids \? ?` , sourceID ),
11098 ),
111- )
112-
113- query .Apply (
11499 sm .Columns ("config_source_ids" ),
115100 )
116101 }
@@ -121,15 +106,12 @@ func SearchLatestReport(ctx context.Context, scmID, sourceID, conditionID, targe
121106 return nil , 0 , fmt .Errorf ("parsing conditionID: %w" , err )
122107 }
123108
124- filteredReportsQuery .Apply (
109+ query .Apply (
110+ sm .Columns ("config_condition_ids" ),
125111 sm .Where (
126112 psql .Raw (`config_condition_ids \? ?` , conditionID ),
127113 ),
128114 )
129-
130- query .Apply (
131- sm .Columns ("config_condition_ids" ),
132- )
133115 }
134116
135117 if targetID != "" {
@@ -138,14 +120,12 @@ func SearchLatestReport(ctx context.Context, scmID, sourceID, conditionID, targe
138120 return nil , 0 , fmt .Errorf ("parsing targetID: %w" , err )
139121 }
140122
141- filteredReportsQuery .Apply (
123+ query .Apply (
124+ sm .Columns ("config_target_ids" ),
142125 sm .Where (
143126 psql .Raw (`config_target_ids \? ?` , targetID ),
144127 ),
145128 )
146- query .Apply (
147- sm .Columns ("config_target_ids" ),
148- )
149129 }
150130
151131 switch scmID {
@@ -176,7 +156,7 @@ func SearchLatestReport(ctx context.Context, scmID, sourceID, conditionID, targe
176156 // FROM filtered_reports
177157 // ORDER BY (data ->> 'Name'), updated_at DESC;`
178158
179- filteredReportsQuery .Apply (
159+ query .Apply (
180160 sm .Where (
181161 psql .Or (
182162 psql .Quote ("cardinality(target_db_scm_ids) = 0" ),
@@ -213,7 +193,7 @@ func SearchLatestReport(ctx context.Context, scmID, sourceID, conditionID, targe
213193 // FROM filtered_reports
214194 // ORDER BY (data ->> 'Name'), updated_at DESC;
215195
216- filteredReportsQuery .Apply (
196+ query .Apply (
217197 sm .Where (
218198 psql .Raw (`target_db_scm_ids && ?` , fmt .Sprintf ("{%s}" , scm [0 ].ID .String ())),
219199 ),
0 commit comments