Skip to content

Commit 6574d62

Browse files
authored
Merge branch 'main' into fix/local-only-format
2 parents c3a7bff + 7c31c5c commit 6574d62

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

sqlmesh/utils/cache.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,16 @@ def __init__(self, path: Path, prefix: t.Optional[str] = None):
6363
# the file.stat() call below will fail on windows if the :file name is longer than 260 chars
6464
file = fix_windows_path(file)
6565

66-
if not file.stem.startswith(self._cache_version) or file.stat().st_atime < threshold:
67-
file.unlink(missing_ok=True)
66+
try:
67+
stat_result = file.stat()
68+
if (
69+
not file.stem.startswith(self._cache_version)
70+
or stat_result.st_atime < threshold
71+
):
72+
file.unlink(missing_ok=True)
73+
except FileNotFoundError:
74+
# File was deleted between glob() and stat() — skip stale cache entries gracefully
75+
continue
6876

6977
def get_or_load(self, name: str, entry_id: str = "", *, loader: t.Callable[[], T]) -> T:
7078
"""Returns an existing cached entry or loads and caches a new one.

tests/utils/test_cache.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,21 @@ def test_optimized_query_cache_macro_def_change(tmp_path: Path, mocker: MockerFi
131131
new_model.render_query_or_raise().sql()
132132
== 'SELECT "_0"."a" AS "a" FROM (SELECT 1 AS "a") AS "_0" WHERE "_0"."a" = 2'
133133
)
134+
135+
136+
def test_file_cache_init_handles_stale_file(tmp_path: Path, mocker: MockerFixture) -> None:
137+
cache: FileCache[_TestEntry] = FileCache(tmp_path)
138+
139+
stale_file = tmp_path / f"{cache._cache_version}__fake_deleted_model_9999999999"
140+
stale_file.touch()
141+
142+
original_stat = Path.stat
143+
144+
def flaky_stat(self, **kwargs):
145+
if self.name == stale_file.name:
146+
raise FileNotFoundError(f"Simulated stale file: {self}")
147+
return original_stat(self, **kwargs)
148+
149+
mocker.patch.object(Path, "stat", flaky_stat)
150+
151+
FileCache(tmp_path)

0 commit comments

Comments
 (0)