Skip to content

Commit 0ae940e

Browse files
committed
Fix error handling
1 parent 9bc0a7a commit 0ae940e

2 files changed

Lines changed: 32 additions & 23 deletions

File tree

sqlmesh/cli/example_project.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pathlib import Path
44
from dataclasses import dataclass
55

6-
from sqlmesh.cli.main import ENGINE_TYPE_DISPLAY_ORDER
76
from sqlmesh.integrations.dlt import generate_dlt_models_and_settings
87
from sqlmesh.utils.date import yesterday_ds
98
from sqlmesh.utils.errors import SQLMeshError
@@ -94,7 +93,7 @@ def _gen_config(
9493
# https://sqlmesh.readthedocs.io/en/stable/reference/model_configuration/#model-defaults
9594
9695
model_defaults:
97-
dialect: {DIALECT_TO_TYPE[engine_type]}
96+
dialect: {DIALECT_TO_TYPE.get(engine_type)}
9897
start: {start or yesterday_ds()} # Start date for backfill history
9998
cron: '@daily' # Run models daily at 12am UTC (can override per model)
10099
@@ -281,6 +280,8 @@ def init_example_project(
281280
schema_name: str = "sqlmesh_example",
282281
cli_mode: InitCliMode = InitCliMode.DEFAULT,
283282
) -> t.Union[str, Path]:
283+
from sqlmesh.cli.main import ENGINE_TYPE_DISPLAY_ORDER
284+
284285
root_path = Path(path)
285286
config_extension = "py" if template == ProjectTemplate.DBT else "yaml"
286287
config_path = root_path / f"config.{config_extension}"
@@ -295,31 +296,32 @@ def init_example_project(
295296
f"Found an existing config file '{config_path}'.\n\nPlease change to another directory or remove the existing file."
296297
)
297298

298-
if not engine_type and template != ProjectTemplate.DBT:
299+
engine_types = "'" + "', '".join(ENGINE_TYPE_DISPLAY_ORDER) + "'"
300+
if engine_type is None and template != ProjectTemplate.DBT:
299301
raise SQLMeshError(
300-
"Missing `engine` argument to `sqlmesh init`. Please specify a SQL engine for your project."
302+
f"Missing `engine` argument to `sqlmesh init` - please specify a SQL engine for your project. Options: '{engine_types}'."
301303
)
302304

303-
if engine_type not in ENGINE_TYPE_DISPLAY_ORDER:
304-
engine_strings = "'" + "', '".join(ENGINE_TYPE_DISPLAY_ORDER) + "'"
305+
if engine_type and engine_type not in ENGINE_TYPE_DISPLAY_ORDER:
305306
raise SQLMeshError(
306-
f"Invalid engine '{engine_type}'. Please specify one of {engine_strings}."
307+
f"Invalid engine '{engine_type}'. Please specify one of '{engine_types}'."
307308
)
308309

309-
dialect = DIALECT_TO_TYPE[engine_type]
310-
311310
models: t.Set[t.Tuple[str, str]] = set()
312311
settings = None
313312
start = None
314-
if template == ProjectTemplate.DLT:
315-
if pipeline and dialect:
316-
models, settings, start = generate_dlt_models_and_settings(
317-
pipeline_name=pipeline, dialect=dialect, dlt_path=dlt_path
318-
)
319-
else:
320-
raise SQLMeshError(
321-
"Please provide a DLT pipeline with the `--dlt-pipeline` flag to generate a SQLMesh project from DLT."
322-
)
313+
if engine_type:
314+
dialect = DIALECT_TO_TYPE[engine_type]
315+
316+
if template == ProjectTemplate.DLT:
317+
if pipeline and dialect:
318+
models, settings, start = generate_dlt_models_and_settings(
319+
pipeline_name=pipeline, dialect=dialect, dlt_path=dlt_path
320+
)
321+
else:
322+
raise SQLMeshError(
323+
"Please provide a DLT pipeline with the `--dlt-pipeline` flag to generate a SQLMesh project from DLT."
324+
)
323325

324326
_create_config(config_path, engine_type, settings, start, template, cli_mode)
325327
if template == ProjectTemplate.DBT:

sqlmesh/cli/main.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,14 @@ def init(
198198
"'" + "', '".join([template.value for template in ProjectTemplate]) + "'"
199199
)
200200
raise click.ClickException(
201-
f"Invalid project template value '{template}'. Please specify one of {template_strings}."
201+
f"Invalid project template '{template}'. Please specify one of {template_strings}."
202202
)
203203

204+
if project_template == ProjectTemplate.DBT and not Path(ctx.obj, "dbt_project.yml").exists():
205+
raise click.ClickException(
206+
"Required dbt project file 'dbt_project.yml' not found in the current directory.\n\n Please add it or change directories before running `sqlmesh init` to set up your dbt project with SQLMesh."
207+
)
208+
204209
if engine or project_template == ProjectTemplate.DBT:
205210
init_example_project(
206211
path=ctx.obj,
@@ -215,7 +220,7 @@ def init(
215220

216221
console = srich.console
217222

218-
project_template, engine_type, cli_mode = _interactive_init(console, project_template)
223+
project_template, engine_type, cli_mode = _interactive_init(ctx.obj, console, project_template)
219224
if project_template != ProjectTemplate.DBT:
220225
_check_engine_installed(console, engine_type)
221226

@@ -1302,17 +1307,19 @@ def state_import(obj: Context, input_file: Path, replace: bool, no_confirm: bool
13021307

13031308

13041309
def _interactive_init(
1305-
console: Console, project_template: t.Optional[ProjectTemplate] = None
1310+
path: Path,
1311+
console: Console,
1312+
project_template: t.Optional[ProjectTemplate] = None,
13061313
) -> t.Tuple[ProjectTemplate, t.Optional[str], t.Optional[InitCliMode]]:
13071314
console.print("──────────────────────────────")
13081315
console.print("Welcome to SQLMesh!")
13091316

13101317
project_template = _init_template_prompt(console) if not project_template else project_template
13111318

13121319
if project_template == ProjectTemplate.DBT:
1313-
if not Path("dbt_project.yml").exists():
1320+
if not Path(path, "dbt_project.yml").exists():
13141321
raise SQLMeshError(
1315-
"Required file 'dbt_project.yml' not found in the current directory.\n\n Please add it or change directories before running `sqlmesh init` to set up your dbt project with SQLMesh."
1322+
"Required dbt project file 'dbt_project.yml' not found in the current directory.\n\n Please add it or change directories before running `sqlmesh init` to set up your dbt project with SQLMesh."
13161323
)
13171324
return (project_template, None, None)
13181325

0 commit comments

Comments
 (0)