feat: add --environment flag to sqlmesh janitor for scoped cleanup
Description
Add an --environment / -e flag to sqlmesh janitor that scopes cleanup to a single named environment, and fix sqlmesh invalidate <env> --sync to only clean up the named environment rather than all currently-expired environments.
New flag: sqlmesh janitor --environment <name> removes views, schemas, and state records for the named environment only. If the environment is not expired or does not exist, a warning is emitted. Global snapshot expiration and interval compaction are skipped when this flag is set — those operations are cross-environment and cannot be safely scoped to a single one.
Bug fix: sqlmesh invalidate <env> --sync previously called _cleanup_environments() with no filter after marking the target environment expired, which caused all other currently-expired environments to be deleted as a side effect. It now scopes the cleanup to the named environment only.
Test Plan
- Tested manually against the sushi example project using
--gateway duckdb_persistentwith two environments (dev1,dev2). Invalidatingdev1and runningsqlmesh janitor --environment dev1cleaned updev1while leavingdev2untouched. - Verified the warning message fires when targeting an environment that is not expired or does not exist.
Checklist
- [x] I have run
make styleand fixed any issues - [x] I have added tests for my changes (if applicable)
- [x] All existing tests pass (
make fast-test) - [x] My commits are signed off (
git commit -s) per the DCO
@mday-io Not a blocker for merge, but a few things I noticed:
- Notebook magic doesn’t expose
--environment. I don' think it's too big of a deal, but it would be nice that notebook magic is aligned as well get_expired_environmentsanddelete_expired_environmentshave dup logic, where it might be able to be put in to_create_expiration_filter_exprinstead. The dup part:
where: exp.Expr = self._create_expiration_filter_expr(current_ts)
if name is not None:
where = exp.and_(t.cast(exp.Condition, where), exp.column("name").eq(name))
- Should we add a Context test that
invalidate_environment(..., sync=True)calls_cleanup_environments(name=...). The state sync test covers targeted deletion, but not the Context wiring where the--syncbug lived.
Let me know if I'm missing anything!
@mday-io Not a blocker for merge, but a few things I noticed:
- Notebook magic doesn’t expose
--environment. I don' think it's too big of a deal, but it would be nice that notebook magic is aligned as wellget_expired_environmentsanddelete_expired_environmentshave dup logic, where it might be able to be put in to_create_expiration_filter_exprinstead. The dup part:where: exp.Expr = self._create_expiration_filter_expr(current_ts) if name is not None: where = exp.and_(t.cast(exp.Condition, where), exp.column("name").eq(name))
- Should we add a Context test that
invalidate_environment(..., sync=True)calls_cleanup_environments(name=...). The state sync test covers targeted deletion, but not the Context wiring where the--syncbug lived.Let me know if I'm missing anything!
Not tackling the notebooks wrapper for now, but created a new commit for the other comments. Thanks for the review!