Skip to content

Commit 8cc5abb

Browse files
Fix external model directory duplicate generation
Signed-off-by: srinivas chennupati <reachsrinivaschennupati@gmail.com>
1 parent 2dca7e9 commit 8cc5abb

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

sqlmesh/core/schema_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def create_external_models_file(
4545
external_model_fqns = set()
4646

4747
for fqn, model in models.items():
48-
if model.kind.is_external:
48+
if model.kind.is_external and getattr(model, "_path", None) == path:
4949
external_model_fqns.add(fqn)
5050
for dep in model.depends_on:
5151
if dep not in known_models:

tests/core/test_schema_loader.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,53 @@ def _load_external_models():
305305
assert len(_load_external_models()) == 1
306306

307307

308+
def test_create_external_models_skips_models_from_external_models_directory(tmp_path: Path):
309+
config = Config(gateways={"": GatewayConfig(connection=DuckDBConnectionConfig())})
310+
311+
model_dir = tmp_path / c.MODELS
312+
model_dir.mkdir()
313+
314+
with open(model_dir / "table.sql", "w", encoding="utf8") as fd:
315+
fd.write(
316+
"""
317+
MODEL (
318+
name lake.table,
319+
kind FULL,
320+
);
321+
322+
SELECT * FROM landing.source_table
323+
""",
324+
)
325+
326+
external_models_dir = tmp_path / c.EXTERNAL_MODELS
327+
external_models_dir.mkdir()
328+
329+
with open(external_models_dir / "source_table.yaml", "w", encoding="utf8") as fd:
330+
yaml.dump(
331+
[
332+
{
333+
"name": "landing.source_table",
334+
"columns": {"a": "int"},
335+
}
336+
],
337+
fd,
338+
)
339+
340+
ctx = Context(paths=[tmp_path], config=config)
341+
ctx.engine_adapter.execute("create schema landing")
342+
ctx.engine_adapter.execute("create table landing.source_table as select 1 as a")
343+
ctx.engine_adapter.execute("create schema lake")
344+
345+
ctx.create_external_models()
346+
347+
assert yaml.load(tmp_path / c.EXTERNAL_MODELS_YAML) == []
348+
349+
ctx.load()
350+
external_models = [model for model in ctx.models.values() if isinstance(model, ExternalModel)]
351+
assert len(external_models) == 1
352+
assert external_models[0].fqn == '"memory"."landing"."source_table"'
353+
354+
308355
def test_no_internal_model_conversion(tmp_path: Path, mocker: MockerFixture):
309356
engine_adapter_mock = mocker.Mock()
310357
engine_adapter_mock.columns.return_value = {

0 commit comments

Comments
 (0)