Skip to content

Commit 9938f67

Browse files
committed
Remove dialect arg from init_example_project
1 parent e3968e5 commit 9938f67

12 files changed

Lines changed: 91 additions & 92 deletions

File tree

sqlmesh/cli/example_project.py

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

6-
from sqlglot import Dialect
6+
from sqlmesh.cli.main import ENGINE_TYPE_DISPLAY_ORDER
77
from sqlmesh.integrations.dlt import generate_dlt_models_and_settings
88
from sqlmesh.utils.date import yesterday_ds
99
from sqlmesh.utils.errors import SQLMeshError
@@ -39,17 +39,15 @@ def _gen_config(
3939
database: db.db"""
4040
)
4141

42-
engine = "mssql" if engine_type == "tsql" else engine_type
43-
4442
if not settings and template != ProjectTemplate.DBT:
4543
doc_link = "https://sqlmesh.readthedocs.io/en/stable/integrations/engines{engine_link}"
4644
engine_link = ""
4745

48-
if engine in CONNECTION_CONFIG_TO_TYPE:
46+
if engine_type in CONNECTION_CONFIG_TO_TYPE:
4947
required_fields = []
5048
non_required_fields = []
5149

52-
for name, field in CONNECTION_CONFIG_TO_TYPE[engine].model_fields.items():
50+
for name, field in CONNECTION_CONFIG_TO_TYPE[engine_type].model_fields.items():
5351
field_name = field.alias or name
5452
if field_name in ("dialect", "display_name"):
5553
continue
@@ -65,7 +63,7 @@ def _gen_config(
6563
option_str = f" {'# ' if not required else ''}{field_name}: {default_value}\n"
6664

6765
# specify the DuckDB database field so quickstart runs out of the box
68-
if engine == "duckdb" and field_name == "database":
66+
if engine_type == "duckdb" and field_name == "database":
6967
option_str = " database: db.db\n"
7068
required = True
7169

@@ -76,7 +74,7 @@ def _gen_config(
7674

7775
connection_settings = "".join(required_fields + non_required_fields)
7876

79-
engine_link = f"/{engine}/#connection-options"
77+
engine_link = f"/{engine_type}/#connection-options"
8078

8179
connection_settings = (
8280
" # For more information on configuring the connection to your execution engine, visit:\n"
@@ -87,16 +85,16 @@ def _gen_config(
8785
default_configs = {
8886
ProjectTemplate.DEFAULT: f"""# --- Gateway Connection ---
8987
gateways:
90-
{engine}:
88+
{engine_type}:
9189
connection:
9290
{connection_settings}
93-
default_gateway: {engine}
91+
default_gateway: {engine_type}
9492
9593
# --- Model Defaults ---
9694
# https://sqlmesh.readthedocs.io/en/stable/reference/model_configuration/#model-defaults
9795
9896
model_defaults:
99-
dialect: {DIALECT_TO_TYPE[engine]}
97+
dialect: {DIALECT_TO_TYPE[engine_type]}
10098
start: {start or yesterday_ds()} # Start date for backfill history
10199
cron: '@daily' # Run models daily at 12am UTC (can override per model)
102100
@@ -276,7 +274,6 @@ def _gen_example_objects(schema_name: str) -> ExampleObjects:
276274

277275
def init_example_project(
278276
path: t.Union[str, Path],
279-
dialect: t.Optional[str],
280277
engine_type: t.Optional[str],
281278
template: ProjectTemplate = ProjectTemplate.DEFAULT,
282279
pipeline: t.Optional[str] = None,
@@ -299,9 +296,17 @@ def init_example_project(
299296
)
300297

301298
if not engine_type and template != ProjectTemplate.DBT:
302-
if not dialect:
303-
raise SQLMeshError("Please provide a default SQL dialect for your project's models.")
304-
Dialect.get_or_raise(dialect)
299+
raise SQLMeshError(
300+
"Missing `engine` argument to `sqlmesh init`. Please specify a SQL engine for your project."
301+
)
302+
303+
if engine_type not in ENGINE_TYPE_DISPLAY_ORDER:
304+
engine_strings = "'" + "', '".join(ENGINE_TYPE_DISPLAY_ORDER) + "'"
305+
raise SQLMeshError(
306+
f"Invalid engine '{engine_type}'. Please specify one of {engine_strings}."
307+
)
308+
309+
dialect = DIALECT_TO_TYPE[engine_type]
305310

306311
models: t.Set[t.Tuple[str, str]] = set()
307312
settings = None
@@ -316,11 +321,7 @@ def init_example_project(
316321
"Please provide a DLT pipeline with the `--dlt-pipeline` flag to generate a SQLMesh project from DLT."
317322
)
318323

319-
# config generation chooses engine based on ConnectionConfig.type_
320-
# - if user passes a SQL dialect, we always generate the engine whose type_ == dialect
321-
# - example: if users passes `postgres` we will always choose `postgres` engine and never `gcp_postgres`
322-
# - if user interactively chooses an engine, we will pass the correct ConnectionConfig.type_
323-
_create_config(config_path, engine_type or dialect, settings, start, template, cli_mode)
324+
_create_config(config_path, engine_type, settings, start, template, cli_mode)
324325
if template == ProjectTemplate.DBT:
325326
return config_path
326327

sqlmesh/cli/main.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def cli(
161161

162162

163163
@cli.command("init")
164-
@click.argument("sql_dialect", required=False)
164+
@click.argument("engine", required=False)
165165
@click.option(
166166
"-t",
167167
"--template",
@@ -183,7 +183,7 @@ def cli(
183183
@cli_analytics
184184
def init(
185185
ctx: click.Context,
186-
sql_dialect: t.Optional[str] = None,
186+
engine: t.Optional[str] = None,
187187
template: t.Optional[str] = None,
188188
dlt_pipeline: t.Optional[str] = None,
189189
dlt_path: t.Optional[str] = None,
@@ -201,11 +201,10 @@ def init(
201201
f"Invalid project template value '{template}'. Please specify one of {template_strings}."
202202
)
203203

204-
if sql_dialect or project_template == ProjectTemplate.DBT:
204+
if engine or project_template == ProjectTemplate.DBT:
205205
init_example_project(
206206
path=ctx.obj,
207-
dialect=sql_dialect,
208-
engine_type=None,
207+
engine_type=engine,
209208
template=project_template or ProjectTemplate.DEFAULT,
210209
pipeline=dlt_pipeline,
211210
dlt_path=dlt_path,
@@ -222,7 +221,6 @@ def init(
222221

223222
config_path = init_example_project(
224223
path=ctx.obj,
225-
dialect=None,
226224
template=project_template,
227225
engine_type=engine_type,
228226
cli_mode=cli_mode or InitCliMode.DEFAULT,

sqlmesh/magics.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
from IPython.utils.process import arg_split
2626
from rich.jupyter import JupyterRenderable
2727
from sqlmesh.cli.example_project import ProjectTemplate, init_example_project
28+
from sqlmesh.cli.main import ENGINE_TYPE_DISPLAY_ORDER
2829
from sqlmesh.core import analytics
2930
from sqlmesh.core.config import load_configs
3031
from sqlmesh.core.console import create_console, set_console, configure_console
3132
from sqlmesh.core.context import Context
3233
from sqlmesh.core.dialect import format_model_expressions, parse
3334
from sqlmesh.core.model import load_sql_based_model
3435
from sqlmesh.core.test import ModelTestMetadata
35-
from sqlmesh.utils import sqlglot_dialects, yaml, Verbosity, optional_import
36+
from sqlmesh.utils import yaml, Verbosity, optional_import
3637
from sqlmesh.utils.errors import MagicError, MissingContextException, SQLMeshError
3738

3839
logger = logging.getLogger(__name__)
@@ -159,9 +160,9 @@ def context(self, line: str) -> None:
159160
@magic_arguments()
160161
@argument("path", type=str, help="The path where the new SQLMesh project should be created.")
161162
@argument(
162-
"sql_dialect",
163+
"engine",
163164
type=str,
164-
help=f"Default model SQL dialect. Supported values: {sqlglot_dialects()}.",
165+
help=f"Project SQL engine. Supported values: '{', '.join(ENGINE_TYPE_DISPLAY_ORDER)}'.", # type: ignore
165166
)
166167
@argument(
167168
"--template",
@@ -191,8 +192,7 @@ def init(self, line: str) -> None:
191192
raise MagicError(f"Invalid project template '{args.template}'")
192193
init_example_project(
193194
path=args.path,
194-
dialect=args.sql_dialect,
195-
engine_type=None,
195+
engine_type=args.engine,
196196
template=project_template,
197197
pipeline=args.dlt_pipeline,
198198
dlt_path=args.dlt_path,

tests/cli/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def create_example_project(temp_dir) -> None:
4747
- Creating the SQLMesh example project in the temp_dir directory
4848
- Overwriting the config.yaml file so the duckdb database file will be created in the temp_dir directory
4949
"""
50-
init_example_project(temp_dir, "duckdb")
50+
init_example_project(temp_dir, engine_type="duckdb")
5151
with open(temp_dir / "config.yaml", "w", encoding="utf-8") as f:
5252
f.write(
5353
f"""gateways:

tests/cli/test_integration_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _invoke(sqlmesh_args: t.List[str], **kwargs: t.Any) -> subprocess.CompletedP
4747

4848
@pytest.fixture
4949
def duckdb_example_project(tmp_path: Path) -> Path:
50-
init_example_project(tmp_path, dialect="duckdb")
50+
init_example_project(tmp_path, engine_type="duckdb")
5151
config_path = tmp_path / "config.yaml"
5252

5353
# we need state to persist between invocations

tests/core/engine_adapter/integration/test_integration_bigquery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def test_information_schema_view_external_model(ctx: TestContext, tmp_path: Path
210210
model_name = ctx.table("test")
211211
dependency = f"`{'.'.join(part.name for part in information_schema_tables.parts)}`"
212212

213-
init_example_project(tmp_path, dialect="bigquery", template=ProjectTemplate.EMPTY)
213+
init_example_project(tmp_path, engine_type="bigquery", template=ProjectTemplate.EMPTY)
214214
with open(tmp_path / "models" / "test.sql", "w", encoding="utf-8") as f:
215215
f.write(
216216
f"""

tests/core/state_sync/test_export_import.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_export_empty_state(tmp_path: Path, state_sync: StateSync) -> None:
7474
def test_export_entire_project(
7575
tmp_path: Path, example_project_config: Config, state_sync: StateSync
7676
) -> None:
77-
init_example_project(path=tmp_path, dialect="duckdb")
77+
init_example_project(path=tmp_path, engine_type="duckdb")
7878
context = Context(paths=tmp_path, config=example_project_config, state_sync=state_sync)
7979

8080
# prod
@@ -159,7 +159,7 @@ def test_export_specific_environment(
159159
tmp_path: Path, example_project_config: Config, state_sync: StateSync
160160
) -> None:
161161
output_file = tmp_path / "state_dump.json"
162-
init_example_project(path=tmp_path, dialect="duckdb")
162+
init_example_project(path=tmp_path, engine_type="duckdb")
163163
context = Context(paths=tmp_path, config=example_project_config, state_sync=state_sync)
164164

165165
# create prod
@@ -231,7 +231,7 @@ def test_export_local_state(
231231
tmp_path: Path, example_project_config: Config, state_sync: StateSync
232232
) -> None:
233233
output_file = tmp_path / "state_dump.json"
234-
init_example_project(path=tmp_path, dialect="duckdb")
234+
init_example_project(path=tmp_path, engine_type="duckdb")
235235
context = Context(paths=tmp_path, config=example_project_config, state_sync=state_sync)
236236

237237
# create prod
@@ -385,7 +385,7 @@ def test_import_local_state_fails(
385385
tmp_path: Path, example_project_config: Config, state_sync: StateSync
386386
) -> None:
387387
output_file = tmp_path / "state_dump.json"
388-
init_example_project(path=tmp_path, dialect="duckdb")
388+
init_example_project(path=tmp_path, engine_type="duckdb")
389389
context = Context(paths=tmp_path, config=example_project_config, state_sync=state_sync)
390390

391391
export_state(state_sync, output_file, context.snapshots)
@@ -400,7 +400,7 @@ def test_import_partial(
400400
tmp_path: Path, example_project_config: Config, state_sync: StateSync
401401
) -> None:
402402
output_file = tmp_path / "state_dump.json"
403-
init_example_project(path=tmp_path, dialect="duckdb")
403+
init_example_project(path=tmp_path, engine_type="duckdb")
404404
context = Context(paths=tmp_path, config=example_project_config, state_sync=state_sync)
405405

406406
# create prod
@@ -453,7 +453,7 @@ def test_import_partial(
453453
def test_roundtrip(tmp_path: Path, example_project_config: Config, state_sync: StateSync) -> None:
454454
state_file = tmp_path / "state_dump.json"
455455

456-
init_example_project(path=tmp_path, dialect="duckdb")
456+
init_example_project(path=tmp_path, engine_type="duckdb")
457457
context = Context(paths=tmp_path, config=example_project_config, state_sync=state_sync)
458458

459459
# populate initial state
@@ -525,7 +525,7 @@ def test_roundtrip(tmp_path: Path, example_project_config: Config, state_sync: S
525525
def test_roundtrip_includes_auto_restatements(
526526
tmp_path: Path, example_project_config: Config, state_sync: StateSync
527527
) -> None:
528-
init_example_project(path=tmp_path, dialect="duckdb")
528+
init_example_project(path=tmp_path, engine_type="duckdb")
529529

530530
# add a model with auto restatements
531531
(tmp_path / c.MODELS / "new_model.sql").write_text("""

tests/core/test_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,7 @@ def test_audit():
20922092

20932093

20942094
def test_prompt_if_uncategorized_snapshot(mocker: MockerFixture, tmp_path: Path) -> None:
2095-
init_example_project(tmp_path, dialect="duckdb")
2095+
init_example_project(tmp_path, engine_type="duckdb")
20962096

20972097
config = Config(
20982098
model_defaults=ModelDefaultsConfig(dialect="duckdb"),

tests/core/test_integration.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4003,7 +4003,7 @@ def fail_auto_restatement(evaluator, start: exp.Expression, **kwargs: t.Any) ->
40034003

40044004

40054005
def test_plan_twice_with_star_macro_yields_no_diff(tmp_path: Path):
4006-
init_example_project(tmp_path, dialect="duckdb")
4006+
init_example_project(tmp_path, engine_type="duckdb")
40074007

40084008
star_model_definition = """
40094009
MODEL (
@@ -6334,13 +6334,13 @@ def plan_with_output(ctx: Context, environment: str):
63346334
assert "Differences from the `prod` environment" in output.stdout
63356335

63366336
assert (
6337-
"""MODEL (
6338-
name test.a,
6339-
+ owner test,
6340-
kind FULL
6341-
)
6342-
SELECT
6343-
- 5 AS col
6337+
"""MODEL (
6338+
name test.a,
6339+
+ owner test,
6340+
kind FULL
6341+
)
6342+
SELECT
6343+
- 5 AS col
63446344
+ 10 AS col"""
63456345
in output.stdout
63466346
)

tests/core/test_loader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_duplicate_model_names_different_kind(tmp_path: Path, sample_models):
6868
else:
6969
model_2, model_3 = models[0], None
7070

71-
init_example_project(tmp_path, dialect="duckdb")
71+
init_example_project(tmp_path, engine_type="duckdb")
7272
config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))
7373

7474
path_1: Path = tmp_path / model_1["path"]
@@ -98,7 +98,7 @@ def duplicate_model_path(fpath):
9898
return Path(fpath).parent / ("duplicate" + Path(fpath).suffix)
9999

100100
model = sample_models[0]
101-
init_example_project(tmp_path, dialect="duckdb")
101+
init_example_project(tmp_path, engine_type="duckdb")
102102
config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))
103103

104104
path_1: Path = tmp_path / model["path"]
@@ -118,7 +118,7 @@ def duplicate_model_path(fpath):
118118
@pytest.mark.registry_isolation
119119
def test_duplicate_python_model_names_raise_error(tmp_path: Path) -> None:
120120
"""Test python models with duplicate model names raises ConfigError if the functions are not identical."""
121-
init_example_project(tmp_path, dialect="duckdb")
121+
init_example_project(tmp_path, engine_type="duckdb")
122122
config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))
123123
model_name = "test_schema.test_model"
124124

@@ -167,7 +167,7 @@ def execute(
167167
@pytest.mark.slow
168168
def test_duplicate_python_model_names_no_error(tmp_path: Path) -> None:
169169
"""Test python models with duplicate model names raises no error if the functions are identical."""
170-
init_example_project(tmp_path, dialect="duckdb")
170+
init_example_project(tmp_path, engine_type="duckdb")
171171
config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))
172172
model_name = "test_schema.test_model"
173173

0 commit comments

Comments
 (0)