|
18 | 18 | from pytest_mock.plugin import MockerFixture |
19 | 19 | from sqlglot import exp |
20 | 20 | from sqlglot.expressions import DataType |
| 21 | +import re |
21 | 22 |
|
22 | 23 | from sqlmesh import CustomMaterialization |
23 | 24 | from sqlmesh.cli.example_project import init_example_project |
@@ -5501,6 +5502,46 @@ def test_plan_production_environment_statements(tmp_path: Path): |
5501 | 5502 | ctx.fetchdf("select * from not_create") |
5502 | 5503 |
|
5503 | 5504 |
|
| 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 | + |
5504 | 5545 | @time_machine.travel("2025-03-08 00:00:00 UTC") |
5505 | 5546 | def test_tz(init_and_plan_context): |
5506 | 5547 | context, _ = init_and_plan_context("examples/sushi") |
|
0 commit comments