66
77from sqlalchemy import (
88 ColumnElement ,
9- CompoundSelect ,
10- Select ,
11- SQLColumnExpression ,
12- and_ ,
139 any_ ,
1410 bindparam ,
11+ column ,
1512 desc ,
1613 func ,
1714 select ,
@@ -150,20 +147,35 @@ async def paginate( # noqa: PLR0912, C901, PLR0915
150147 if ended_until :
151148 where .append (Run .ended_at <= ended_until )
152149
153- query : Select | CompoundSelect
154- order_by : list [ColumnElement | SQLColumnExpression ]
150+ run = select (Run ).where (* where )
151+
152+ if job_types or job_location_ids :
153+ job = aliased (Job , name = "job_type" )
154+ run = run .join (job , Run .job_id == job .id )
155+ if job_types :
156+ where .append (job .type == any_ (list (job_types ))) # type: ignore[arg-type]
157+ if job_location_ids :
158+ where .append (job .location_id == any_ (list (job_location_ids ))) # type: ignore[arg-type]
159+
160+ if started_by_users :
161+ usernames_lower = [name .lower () for name in started_by_users ]
162+ run = run .join (User , Run .started_by_user_id == User .id )
163+ where .append (func .lower (User .name ) == any_ (usernames_lower )) # type: ignore[arg-type]
164+
165+ query = run .where (* where )
166+ order_by : list [ColumnElement ] = [desc ("created_at" ), desc ("id" )]
155167 if search_query :
156168 tsquery = make_tsquery (search_query )
157169
158- run_stmt = select ( Run , ts_rank ( Run . search_vector , tsquery ). label ( "search_rank" )). where (
159- ts_match ( Run . search_vector , tsquery ),
160- * where ,
170+ run_cte = query . cte ()
171+ run_stmt = select ( run_cte , ts_rank ( column ( " search_vector" ) , tsquery ). label ( "search_rank" )). where (
172+ ts_match ( column ( "search_vector" ), tsquery ) ,
161173 )
162174 job_search = aliased (Job , name = "job_search" )
163175 job_stmt = (
164- select (Run , ts_rank (job_search .search_vector , tsquery ).label ("search_rank" ))
165- .join (job_search , job_search .id == Run . job_id )
166- .where (ts_match (job_search .search_vector , tsquery ), * where )
176+ select (run_cte , ts_rank (job_search .search_vector , tsquery ).label ("search_rank" ))
177+ .join (job_search , job_search .id == column ( " job_id" ) )
178+ .where (ts_match (job_search .search_vector , tsquery ))
167179 )
168180
169181 union_cte = union (run_stmt , job_stmt ).cte ()
@@ -176,27 +188,6 @@ async def paginate( # noqa: PLR0912, C901, PLR0915
176188 ).group_by (* run_columns )
177189 # place the most recent runs on top
178190 order_by = [desc ("search_rank" ), desc ("created_at" ), desc ("id" )]
179- else :
180- query = select (Run ).where (* where )
181- order_by = [Run .created_at .desc (), Run .id .desc ()]
182-
183- if job_types or job_location_ids :
184- job = aliased (Job , name = "job_type" )
185- query = query .join (job , and_ (Run .job_id == job .id ))
186- if job_types :
187- query = query .where (job .type == any_ (list (job_types ))) # type: ignore[arg-type]
188- if job_location_ids :
189- query = query .where (job .location_id == any_ (list (job_location_ids ))) # type: ignore[arg-type]
190-
191- if started_by_users :
192- usernames_lower = [name .lower () for name in started_by_users ]
193- query = query .join (
194- User ,
195- and_ (
196- Run .started_by_user_id == User .id ,
197- func .lower (User .name ) == any_ (usernames_lower ), # type: ignore[arg-type]
198- ),
199- )
200191
201192 options = [selectinload (Run .started_by_user )]
202193 return await self ._paginate_by_query (
@@ -308,9 +299,9 @@ async def update(
308299 "persistent_log_url" : new .persistent_log_url ,
309300 "running_log_url" : new .running_log_url ,
310301 }
311- for column , value in optional_fields .items ():
302+ for col_name , value in optional_fields .items ():
312303 if value is not None :
313- setattr (existing , column , value )
304+ setattr (existing , col_name , value )
314305
315306 await self ._session .flush ([existing ])
316307 return existing
0 commit comments