|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import typing as t |
| 4 | +from pathlib import Path |
4 | 5 | import pytest |
5 | 6 | import time_machine |
6 | 7 | from pytest_mock.plugin import MockerFixture |
7 | 8 |
|
8 | 9 | from sqlmesh.core import constants as c |
9 | 10 | from sqlmesh.core import dialect as d |
10 | 11 | from sqlmesh.core.config.categorizer import CategorizerConfig |
| 12 | +from sqlmesh.core.config import ( |
| 13 | + Config, |
| 14 | + DuckDBConnectionConfig, |
| 15 | + GatewayConfig, |
| 16 | + ModelDefaultsConfig, |
| 17 | + RunConfig, |
| 18 | +) |
| 19 | +from sqlmesh.core.context import Context |
11 | 20 | from sqlmesh.core.model import ( |
12 | 21 | SqlModel, |
13 | 22 | PythonModel, |
14 | 23 | load_sql_based_model, |
15 | 24 | ) |
16 | 25 | from sqlmesh.utils.date import to_timestamp |
| 26 | +from sqlmesh.utils.concurrency import NodeExecutionFailedError |
| 27 | +from sqlmesh.utils.errors import SQLMeshError |
17 | 28 |
|
18 | 29 | if t.TYPE_CHECKING: |
19 | 30 | pass |
20 | 31 |
|
21 | 32 | pytestmark = pytest.mark.slow |
22 | 33 |
|
23 | 34 |
|
| 35 | +def test_run_waits_for_finalization_after_failed_plan(tmp_path: Path, mocker: MockerFixture): |
| 36 | + model_path = tmp_path / "models" / "example.sql" |
| 37 | + model_path.parent.mkdir() |
| 38 | + original_model = "MODEL (name repro.example, kind FULL); SELECT 1::INT AS id;" |
| 39 | + updated_model = original_model.replace("SELECT 1", "SELECT 2") |
| 40 | + model_path.write_text(original_model) |
| 41 | + context = Context( |
| 42 | + paths=tmp_path, |
| 43 | + config=Config( |
| 44 | + gateways={ |
| 45 | + "local": GatewayConfig( |
| 46 | + connection=DuckDBConnectionConfig(database=str(tmp_path / "repro.duckdb")) |
| 47 | + ) |
| 48 | + }, |
| 49 | + default_gateway="local", |
| 50 | + model_defaults=ModelDefaultsConfig(dialect="duckdb"), |
| 51 | + disable_anonymized_analytics=True, |
| 52 | + run=RunConfig(environment_check_interval=1, environment_check_max_wait=2), |
| 53 | + ), |
| 54 | + ) |
| 55 | + try: |
| 56 | + context.plan("prod", auto_apply=True, no_prompts=True) |
| 57 | + initial_environment = context.state_sync.get_environment("prod") |
| 58 | + assert initial_environment is not None and initial_environment.finalized_ts |
| 59 | + |
| 60 | + model_path.write_text( |
| 61 | + updated_model |
| 62 | + + "\nON_VIRTUAL_UPDATE_BEGIN;" |
| 63 | + + "\nSELECT CAST('intentional_promotion_failure' AS INTEGER);" |
| 64 | + + "\nON_VIRTUAL_UPDATE_END;" |
| 65 | + ) |
| 66 | + context.load() |
| 67 | + with pytest.raises(NodeExecutionFailedError): |
| 68 | + context.plan("prod", auto_apply=True, no_prompts=True) |
| 69 | + |
| 70 | + failed_environment = context.state_sync.get_environment("prod") |
| 71 | + assert failed_environment is not None |
| 72 | + assert failed_environment.finalized_ts is None |
| 73 | + run = mocker.spy(context, "_run") |
| 74 | + warnings = mocker.spy(context.console, "log_warning") |
| 75 | + with pytest.raises(SQLMeshError, match="Exceeded the maximum wait time") as exc_info: |
| 76 | + context.run("prod", skip_janitor=True) |
| 77 | + |
| 78 | + run.assert_not_called() |
| 79 | + assert context.state_sync.get_environment("prod") == failed_environment |
| 80 | + assert warnings.call_count == 2 |
| 81 | + |
| 82 | + model_path.write_text(updated_model) |
| 83 | + context.load() |
| 84 | + context.plan("prod", auto_apply=True, no_prompts=True) |
| 85 | + recovered_environment = context.state_sync.get_environment("prod") |
| 86 | + assert recovered_environment is not None and recovered_environment.finalized_ts |
| 87 | + assert not context.run("prod", skip_janitor=True).is_failure |
| 88 | + run.assert_called_once() |
| 89 | + |
| 90 | + warning = warnings.call_args_list[0].args[0] |
| 91 | + assert failed_environment.plan_id in warning |
| 92 | + assert "finaliz" in warning |
| 93 | + assert "1 seconds" in warning |
| 94 | + assert "2 seconds" in warning |
| 95 | + assert "failed" in warning |
| 96 | + assert "is being updated" not in warning |
| 97 | + assert "finaliz" in str(exc_info.value) |
| 98 | + assert "sqlmesh plan prod" in str(exc_info.value) |
| 99 | + assert "https://sqlmesh.readthedocs.io/en/stable/reference/configuration/#run" in str( |
| 100 | + exc_info.value |
| 101 | + ) |
| 102 | + finally: |
| 103 | + context.close() |
| 104 | + |
| 105 | + |
24 | 106 | @time_machine.travel("2023-01-08 15:00:00 UTC") |
25 | 107 | def test_run_with_select_models( |
26 | 108 | init_and_plan_context: t.Callable, |
|
0 commit comments