Skip to content

Commit a273ffb

Browse files
committed
Feat: Push filter down to query for get_expired_environments
1 parent b256ba5 commit a273ffb

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

sqlmesh/core/state_sync/db/environment.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,30 @@ def finalize(self, environment: Environment) -> None:
165165
where=environment_filter,
166166
)
167167

168+
def _fetch_environment_summaries(
169+
self, where: t.Optional[str | exp.Expression] = None
170+
) -> t.List[EnvironmentSummary]:
171+
return [
172+
self._environment_summmary_from_row(row)
173+
for row in fetchall(
174+
self.engine_adapter,
175+
self._environments_query(
176+
where=where,
177+
required_fields=list(EnvironmentSummary.all_fields()),
178+
),
179+
)
180+
]
181+
168182
def get_expired_environments(self, current_ts: int) -> t.List[EnvironmentSummary]:
169183
"""Returns the expired environments.
170184
171185
Expired environments are environments that have exceeded their time-to-live value.
172186
Returns:
173187
The list of environment summaries to remove.
174188
"""
175-
176-
environment_summaries = self.get_environments_summary()
177-
return [
178-
env_summary
179-
for env_summary in environment_summaries
180-
if env_summary.expiration_ts is not None and env_summary.expiration_ts <= current_ts
181-
]
189+
return self._fetch_environment_summaries(
190+
where=self._create_expiration_filter_expr(current_ts)
191+
)
182192

183193
def delete_expired_environments(
184194
self, current_ts: t.Optional[int] = None
@@ -225,13 +235,7 @@ def get_environments_summary(self) -> t.List[EnvironmentSummary]:
225235
Returns:
226236
A list of all environment summaries.
227237
"""
228-
return [
229-
self._environment_summmary_from_row(row)
230-
for row in fetchall(
231-
self.engine_adapter,
232-
self._environments_query(required_fields=list(EnvironmentSummary.all_fields())),
233-
)
234-
]
238+
return self._fetch_environment_summaries()
235239

236240
def get_environment(
237241
self, environment: str, lock_for_update: bool = False

sqlmesh/core/state_sync/db/facade.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@
6969
T = t.TypeVar("T")
7070

7171

72-
if t.TYPE_CHECKING:
73-
pass
74-
75-
7672
class EngineAdapterStateSync(StateSync):
7773
"""Manages state of nodes and snapshot with an existing engine adapter.
7874

0 commit comments

Comments
 (0)