-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Add environments command to list environments #3770
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
from sqlmesh.core.analytics import cli_analytics | ||
from sqlmesh.core.config import load_configs | ||
from sqlmesh.core.context import Context | ||
from sqlmesh.utils.date import TimeLike | ||
from sqlmesh.utils.date import TimeLike, time_like_to_str | ||
from sqlmesh.utils.errors import MissingDependencyError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -966,3 +966,30 @@ def dlt_refresh( | |
ctx.obj.console.log_success(f"Updated SQLMesh project with models:\n{model_names}") | ||
else: | ||
ctx.obj.console.log_success("All SQLMesh models are up to date.") | ||
|
||
|
||
@cli.command("environments") | ||
@click.option( | ||
"-e", | ||
"--expiry-ds", | ||
is_flag=True, | ||
help="Prints the expiration datetime of the environments.", | ||
default=False, | ||
) | ||
@click.pass_obj | ||
@error_handler | ||
@cli_analytics | ||
def environments(obj: Context, expiry_ds: bool) -> None: | ||
"""Prints the list of SQLMesh environments with its expiration datetime.""" | ||
environments = {e.name: e.expiration_ts for e in obj.state_sync.get_environments()} | ||
environment_names = list(environments.keys()) | ||
if expiry_ds: | ||
max_width = len(max(environment_names, key=len)) | ||
print( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should be using the |
||
"\n".join( | ||
f"{k:<{max_width}} {time_like_to_str(v)}" if v else f"{k:<{max_width}} no-expiry" | ||
for k, v in environments.items() | ||
), | ||
) | ||
return | ||
print("\n".join(environment_names)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ | |
from sqlmesh.core.dialect import format_model_expressions, parse | ||
from sqlmesh.core.model import load_sql_based_model | ||
from sqlmesh.core.test import ModelTestMetadata, get_all_model_tests | ||
from sqlmesh.utils import sqlglot_dialects, yaml | ||
from sqlmesh.utils import date, sqlglot_dialects, yaml | ||
from sqlmesh.utils.errors import MagicError, MissingContextException, SQLMeshError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -1007,6 +1007,33 @@ def clean(self, context: Context, line: str) -> None: | |
context.clear_caches() | ||
context.console.log_success("SQLMesh cache and build artifacts cleared") | ||
|
||
@magic_arguments() | ||
@argument( | ||
"--expiry-ds", | ||
"-e", | ||
action="store_true", | ||
help="Prints the expiration datetime of the environments.", | ||
) | ||
@line_magic | ||
@pass_sqlmesh_context | ||
def environments(self, context: Context, line: str) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like the exact copy-paste of the code above. It might be worse considering having a method as part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed the pattern used by If you not mind could you please elaborate on whats expected here? |
||
"""Prints the list of SQLMesh environments with its expiration datetime.""" | ||
args = parse_argstring(self.environments, line) | ||
environments = {e.name: e.expiration_ts for e in context.state_sync.get_environments()} | ||
environment_names = list(environments.keys()) | ||
if args.expiry_ds: | ||
max_width = len(max(environment_names, key=len)) | ||
context.console.log_status_update( | ||
"\n".join( | ||
f"{k:<{max_width}} {date.time_like_to_str(v)}" | ||
if v | ||
else f"{k:<{max_width}} no-expiry" | ||
for k, v in environments.items() | ||
), | ||
) | ||
return | ||
context.console.log_status_update("\n".join(environment_names)) | ||
|
||
|
||
def register_magics() -> None: | ||
try: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a heads up that environment objects are pretty large and loading and parsing them all at once can be a pretty long and memory-intensive operation for big projects with a lot of environments.
A better approach could be having a specialized method in the the
state_sync.py
that only fetches limited fields like names.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, I'll do this. Thanks :)