@@ -172,46 +172,40 @@ def get_expired_environments(self, current_ts: int) -> t.List[Environment]:
172172 rows = fetchall (
173173 self .engine_adapter ,
174174 self ._environments_query (
175- where = self ._create_filter_expr (current_ts ),
175+ where = self ._create_expiration_filter_expr (current_ts ),
176176 lock_for_update = True ,
177177 ),
178178 )
179179 expired_environments = [self ._environment_from_row (r ) for r in rows ]
180180
181181 return expired_environments
182182
183- def delete_environments (self , environments : t .List [Environment ], current_ts : int ) -> None :
183+ def delete_expired_environments (
184+ self , current_ts : t .Optional [int ] = None
185+ ) -> t .List [Environment ]:
184186 """Deletes expired environments.
185187
186188 Returns:
187189 A list of deleted environments.
188190 """
191+ current_ts = current_ts or now_timestamp ()
192+ expired_environments = self .get_expired_environments (current_ts = current_ts )
193+
189194 self .engine_adapter .delete_from (
190195 self .environments_table ,
191- where = self ._create_filter_expr (current_ts ),
196+ where = self ._create_expiration_filter_expr (current_ts ),
192197 )
193198
194199 # Delete the expired environments' corresponding environment statements
195- if expired_environments := [
200+ if expired_environments_exprs := [
196201 exp .EQ (this = exp .column ("environment_name" ), expression = exp .Literal .string (env .name ))
197- for env in environments
202+ for env in expired_environments
198203 ]:
199204 self .engine_adapter .delete_from (
200205 self .environment_statements_table ,
201- where = exp .or_ (* expired_environments ),
206+ where = exp .or_ (* expired_environments_exprs ),
202207 )
203208
204- def delete_expired_environments (
205- self , current_ts : t .Optional [int ] = None
206- ) -> t .List [Environment ]:
207- """Deletes expired environments.
208-
209- Returns:
210- A list of deleted environments.
211- """
212- current_ts = current_ts or now_timestamp ()
213- expired_environments = self .get_expired_environments (current_ts = current_ts )
214- self .delete_environments (expired_environments , current_ts = current_ts )
215209 return expired_environments
216210
217211 def get_environments (self ) -> t .List [Environment ]:
@@ -316,7 +310,12 @@ def _environments_query(
316310 return query .lock (copy = False )
317311 return query
318312
319- def _create_filter_expr (self , current_ts : int ) -> exp .Expression :
313+ def _create_expiration_filter_expr (self , current_ts : int ) -> exp .Expression :
314+ """Creates a SQLGlot filter expression to find expired environments.
315+
316+ Args:
317+ current_ts: The current timestamp.
318+ """
320319 return exp .LTE (
321320 this = exp .column ("expiration_ts" ),
322321 expression = exp .Literal .number (current_ts ),
0 commit comments