sqlmesh icon indicating copy to clipboard operation
sqlmesh copied to clipboard

feat: add --environment flag to sqlmesh janitor for scoped cleanup

Open mday-io opened this issue 6 months ago • 1 comments

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_persistent with two environments (dev1, dev2). Invalidating dev1 and running sqlmesh janitor --environment dev1 cleaned up dev1 while leaving dev2 untouched.
  • Verified the warning message fires when targeting an environment that is not expired or does not exist.

Checklist

  • [x] I have run make style and 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 avatar May 22 '26 21:05 mday-io

@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_environments and delete_expired_environments have dup logic, where it might be able to be put in to _create_expiration_filter_expr instead. 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 --sync bug lived.

Let me know if I'm missing anything!

yuki-kakegawa avatar May 31 '26 06:05 yuki-kakegawa

@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_environments and delete_expired_environments have dup logic, where it might be able to be put in to _create_expiration_filter_expr instead. 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 --sync bug 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!

mday-io avatar Jun 01 '26 22:06 mday-io