Skip to content

Commit

Permalink
new method to fetch only env names
Browse files Browse the repository at this point in the history
  • Loading branch information
lafirm committed Feb 9, 2025
1 parent ec9de47 commit ec4e758
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
6 changes: 3 additions & 3 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ Options:
```
Usage: sqlmesh environments [OPTIONS]
Prints the list of SQLMesh environments with its expiration datetime.
Prints the list of SQLMesh environments with its expiry datetime.
Options:
-e, --expiry-ds Prints the expiration datetime of the environments.
--help Show this message and exit.
-e, --show-expiry Prints the expiry datetime of the environments.
--help Show this message and exit.
```

## evaluate
Expand Down
6 changes: 3 additions & 3 deletions docs/reference/notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ options:

#### environments
```
%environments [--expiry-ds]
Prints the list of SQLMesh environments with its expiration datetime.
%environments [--show-expiry]
Prints the list of SQLMesh environments with its expiry datetime.
options:
--expiry-ds, -e If set will print the expiration datetime of the environments
--show-expiry, -e If set will print the expiry datetime of the environments
```

#### fetchdf
Expand Down
26 changes: 9 additions & 17 deletions sqlmesh/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,25 +971,17 @@ def dlt_refresh(
@cli.command("environments")
@click.option(
"-e",
"--expiry-ds",
"--show-expiry",
is_flag=True,
help="Prints the expiration datetime of the environments.",
help="Prints the expiry datetime of the environments.",
default=False,
)
@click.pass_obj
@click.pass_context
@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(
"\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))
def environments(ctx: click.Context, show_expiry: bool) -> None:
"""Prints the list of SQLMesh environments with its expiry datetime."""
context = ctx.obj
environment_names = context.state_sync.get_environment_names(get_expiry_ts=show_expiry)
output = [f"{name} - {time_like_to_str(ts)}" for name, ts in environment_names] if show_expiry else [name[0] for name in environment_names]
context.console.log_status_update(f"Number of SQLMesh environments are: {len(output)}\n{"\n".join(output)}")
8 changes: 8 additions & 0 deletions sqlmesh/core/state_sync/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ def get_environments(self) -> t.List[Environment]:
A list of all environments.
"""

@abc.abstractmethod
def get_environment_names(self, get_expiry_ts: bool = True) -> t.List[t.Tuple[str]] | t.List[t.Tuple[str, int]]:
"""Fetches all environment names along with expiry datetime if get_expiry_ts is True.
Returns:
A list of all environment names along with expiry datetime if get_expiry_ts is True.
"""

@abc.abstractmethod
def max_interval_end_per_model(
self,
Expand Down
13 changes: 12 additions & 1 deletion sqlmesh/core/state_sync/engine_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,27 @@ def get_environments(self) -> t.List[Environment]:
self._environment_from_row(row) for row in self._fetchall(self._environments_query())
]

def get_environment_names(self, get_expiry_ts: bool = True) -> t.List[t.Tuple[str]] | t.List[t.Tuple[str, int]]:
"""Fetches all environment names along with expiry datetime if get_expiry_ts is True.
Returns:
A list of all environment names along with expiry datetime if get_expiry_ts is True.
"""
name_field = ["name"]
return self._fetchall(self._environments_query(required_fields=name_field if not get_expiry_ts else name_field + ["expiration_ts"]))

def _environment_from_row(self, row: t.Tuple[str, ...]) -> Environment:
return Environment(**{field: row[i] for i, field in enumerate(Environment.all_fields())})

def _environments_query(
self,
where: t.Optional[str | exp.Expression] = None,
lock_for_update: bool = False,
required_fields: t.Optional[t.List[str]] = None,
) -> exp.Select:
required_fields = required_fields if required_fields else Environment.all_fields()
query = (
exp.select(*(exp.to_identifier(field) for field in Environment.all_fields()))
exp.select(*(exp.to_identifier(field) for field in required_fields))
.from_(self.environments_table)
.where(where)
)
Expand Down
19 changes: 4 additions & 15 deletions sqlmesh/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,22 +1017,11 @@ def clean(self, context: Context, line: str) -> None:
@line_magic
@pass_sqlmesh_context
def environments(self, context: Context, line: str) -> None:
"""Prints the list of SQLMesh environments with its expiration datetime."""
"""Prints the list of SQLMesh environments with its expiry 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))
environment_names = context.state_sync.get_environment_names(get_expiry_ts=args.show_expiry)
output = [f"{name} - {date.time_like_to_str(ts)}" for name, ts in environment_names] if args.show_expiry else [name[0] for name in environment_names]
context.console.log_status_update(f"Number of SQLMesh environments are: {len(output)}\n{"\n".join(output)}")


def register_magics() -> None:
Expand Down
6 changes: 3 additions & 3 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ def test_environments(runner, tmp_path):
],
)
assert result.exit_code == 0
assert result.output == "dev\ndev2\n"
assert result.output == "Number of SQLMesh environments are: 2\ndev\ndev2\n"

result = runner.invoke(
cli,
Expand All @@ -1030,9 +1030,9 @@ def test_environments(runner, tmp_path):
"--paths",
tmp_path,
"environments",
"--expiry-ds",
"--show-expiry",
],
)
assert result.exit_code == 0
ttl = time_like_to_str(to_datetime(now_ds()) + timedelta(days=7))
assert result.output == f"dev {ttl}\ndev2 {ttl}\n"
assert result.output == f"Number of SQLMesh environments are: 2\ndev - {ttl}\ndev2 - {ttl}\n"

0 comments on commit ec4e758

Please sign in to comment.