Add scoped snapshot cleanup to `invalidate` command
維護者通常 4 天內回覆
評估
這個 Issue 還沒有評估資料。
描述
Problem
Currently, when invalidating an environment with sqlmesh invalidate <env> -s, the command only removes the environment record and virtual layer (views/schemas), but does not clean up the physical snapshot tables.
To clean up snapshots, users must run sqlmesh janitor --ignore-ttl separately. However, this operates globally and will delete all unreferenced snapshots across the project, including those from other previously invalidated environments that haven't been cleaned up yet.
This creates a problem when you want to clean up snapshots for a specific environment without affecting other environments' snapshots.
Proposed Solution
Add a --cleanup-snapshots flag to the invalidate command that triggers scoped snapshot cleanup for only the target environment. This would:
- Capture the snapshot IDs from the environment being invalidated (before deletion)
- Identify which of those snapshots are not referenced by any other environment
- Delete only those exclusively-owned snapshots
Implementation Approach
The feature requires changes across multiple layers:
1. CLI Layer (sqlmesh/cli/main.py)
Add a new flag to the invalidate command:
@click.option(
"--cleanup-snapshots",
is_flag=True,
help="Clean up snapshots exclusively used by this environment after invalidation",
)
2. Context Layer (sqlmesh/core/context.py)
Modify invalidate_environment to capture snapshot IDs before deletion and pass them to a scoped cleanup function when the flag is set. 1
3. State Sync Layer (sqlmesh/core/state_sync/db/facade.py)
Add a method to get expired snapshots scoped to a specific set of snapshot IDs, while still respecting the protection of snapshots referenced by other environments.
4. Snapshot State Layer (sqlmesh/core/state_sync/db/snapshot.py)
Modify get_expired_snapshots to accept an optional target_snapshot_ids parameter that restricts the query to only those IDs, while still excluding any that appear in other environments' promoted_snapshot_ids.
5. Janitor Layer (sqlmesh/core/janitor.py)
Add support for scoped snapshot deletion in delete_expired_snapshots and iter_expired_snapshot_batches.
Example Usage
# Invalidate environment and clean up its exclusively-owned snapshots
sqlmesh invalidate dev_mday_picnic_ibuk_test -s --cleanup-snapshots
This would be equivalent to running:
sqlmesh invalidate dev_mday_picnic_ibuk_test -s
sqlmesh janitor --ignore-ttl # but scoped to only this environment's snapshots
Notes
- The implementation must preserve the existing protection mechanism: snapshots shared with other environments (e.g., prod) must never be deleted
- The
--cleanup-snapshotsflag should work with or without the-sflag - This feature would be particularly useful for CI/CD workflows where PR environments are frequently created and invalidated
Wiki pages you might want to explore:
Citations
File: docs/integrations/github.md (L289-292)
| Option | Description | Type | Required |
|---------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------:|:--------:|
| `invalidate_environment_after_deploy` | Indicates if the PR environment created should be automatically invalidated after changes are deployed. Invalidated environments are cleaned up automatically by the Janitor. Default: `True` | bool | N |
| `merge_method` | The merge method to use when automatically merging a PR after deploying to prod. Defaults to `None` meaning automatic merge is not done. Options: `merge`, `squash`, `rebase` | string | N |
- 主要語言
- Python
- 星號
- 3.3k
- 分支
- 471
- 平均合併
- 6 天 7 小時
- 30 天內合併 PR
- 35
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
SQLMesh/sqlmesh 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 86/100
維護者通常 4 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 4 天內回覆
-
難度 2/5 1-3 小時 新手友好度 67/100
SQLMesh/sqlmesh#6008 · 1 則留言 ·
維護者通常 4 天內回覆
-
VSCode Extension: SQLMesh LSP crashes on startup: ImportError: cannot import name 'WorkspaceDiagnosticRefreshRequest' with lsprotocol 2025.x可能已有人在做 @boomesh11 於 15 天前認領。 未關閉Bug
難度 2/5 1-3 小時 新手友好度 66/100
維護者通常 4 天內回覆
-
Improvement
難度 1/5 1 小時以內 新手友好度 85/100
SQLMesh/sqlmesh#5696 · 6 則留言 ·
維護者通常 4 天內回覆
相似的 Issue
-
[BUG][Low–Medium] founder_numbers.py silently reads a SAFE discount or pool target given as a fraction (0.2) as 0.2%可能已有人在做 @sumitkhond111-del 今天認領。 未關閉bug
難度 2/5 1-3 小時 新手友好度 72/100
bmad-code-org/BMAD-METHOD#3358 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 86/100
open-mmlab/mmcv#3352 ·
-
難度 1/5 1 小時以內 新手友好度 68/100
OpenGithubs/weekly#263 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
comp/cli P3 type/feature
難度 2/5 1-3 小時 新手友好度 76/100
NousResearch/hermes-agent#162784 ·
維護者通常 1 天內回覆