Skip to content

Commit 9f48b92

Browse files
Fix: Gracefully handle execution errors in before after all
1 parent 983aff7 commit 9f48b92

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

sqlmesh/core/environment.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from sqlmesh.core.snapshot import SnapshotId, SnapshotTableInfo, Snapshot
1515
from sqlmesh.utils import word_characters_only
1616
from sqlmesh.utils.date import TimeLike, now_timestamp
17+
from sqlmesh.utils.errors import SQLMeshError
1718
from sqlmesh.utils.jinja import JinjaMacroRegistry
1819
from sqlmesh.utils.metaprogramming import Executable
1920
from sqlmesh.utils.pydantic import PydanticModel, field_validator, ValidationInfo
@@ -282,4 +283,7 @@ def execute_environment_statements(
282283
if rendered_expressions:
283284
with adapter.transaction():
284285
for expr in rendered_expressions:
285-
adapter.execute(expr)
286+
try:
287+
adapter.execute(expr)
288+
except Exception as e:
289+
raise SQLMeshError(f"An error occurred during execution of:\n\n{expr}\n\n{e}")

tests/core/test_integration.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pytest_mock.plugin import MockerFixture
1919
from sqlglot import exp
2020
from sqlglot.expressions import DataType
21+
import re
2122

2223
from sqlmesh import CustomMaterialization
2324
from sqlmesh.cli.example_project import init_example_project
@@ -5501,6 +5502,46 @@ def test_plan_production_environment_statements(tmp_path: Path):
55015502
ctx.fetchdf("select * from not_create")
55025503

55035504

5505+
def test_environment_statements_error_handling(tmp_path: Path):
5506+
model_a = """
5507+
MODEL (
5508+
name test_schema.a,
5509+
kind FULL,
5510+
);
5511+
5512+
SELECT 1 AS account_id
5513+
"""
5514+
5515+
models_dir = tmp_path / "models"
5516+
models_dir.mkdir()
5517+
5518+
for path, defn in {"a.sql": model_a}.items():
5519+
with open(models_dir / path, "w") as f:
5520+
f.write(defn)
5521+
5522+
before_all = [
5523+
"CREATE TABLE identical_table (physical_schema_name VARCHAR)",
5524+
"CREATE TABLE identical_table (physical_schema_name VARCHAR)",
5525+
]
5526+
5527+
config = Config(
5528+
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
5529+
before_all=before_all,
5530+
)
5531+
ctx = Context(paths=[tmp_path], config=config)
5532+
5533+
expected_error_message = re.escape(
5534+
"""An error occurred during execution of:
5535+
5536+
CREATE TABLE identical_table (physical_schema_name TEXT)
5537+
5538+
Catalog Error: Table with name "identical_table" already exists!"""
5539+
)
5540+
5541+
with pytest.raises(SQLMeshError, match=expected_error_message):
5542+
ctx.plan(auto_apply=True, no_prompts=True)
5543+
5544+
55045545
@time_machine.travel("2025-03-08 00:00:00 UTC")
55055546
def test_tz(init_and_plan_context):
55065547
context, _ = init_and_plan_context("examples/sushi")

0 commit comments

Comments
 (0)