Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Drop the dev table clone if the schema migration fails #2830

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sqlmesh/core/snapshot/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ def _create_snapshot(
snapshot, alter_expressions, allow_destructive_snapshots
)
self.adapter.alter_table(alter_expressions)
except Exception:
self.adapter.drop_table(target_table_name)
raise
finally:
self.adapter.drop_table(tmp_table_name)
else:
Expand Down
51 changes: 51 additions & 0 deletions tests/core/test_snapshot_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,57 @@ def test_create_clone_in_dev(mocker: MockerFixture, adapter_mock, make_snapshot)
)


def test_drop_clone_in_dev_when_migration_fails(mocker: MockerFixture, adapter_mock, make_snapshot):
adapter_mock.SUPPORTS_CLONING = True
adapter_mock.get_alter_expressions.return_value = []
evaluator = SnapshotEvaluator(adapter_mock)

adapter_mock.alter_table.side_effect = Exception("Migration failed")

model = load_sql_based_model(
parse( # type: ignore
"""
MODEL (
name test_schema.test_model,
kind INCREMENTAL_BY_TIME_RANGE (
time_column ds
)
);
SELECT 1::INT as a, ds::DATE FROM a;
"""
),
)

snapshot = make_snapshot(model)
snapshot.categorize_as(SnapshotChangeCategory.FORWARD_ONLY)
snapshot.previous_versions = snapshot.all_versions

evaluator.create([snapshot], {})

adapter_mock.clone_table.assert_called_once_with(
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__temp",
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}",
replace=True,
)

adapter_mock.get_alter_expressions.assert_called_once_with(
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__temp",
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__temp__schema_migration_source",
)

adapter_mock.alter_table.assert_called_once_with([])

adapter_mock.drop_table.assert_has_calls(
[
call(f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__temp"),
call(
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__temp__schema_migration_source"
),
]
)


def test_create_clone_in_dev_self_referencing(mocker: MockerFixture, adapter_mock, make_snapshot):
adapter_mock.SUPPORTS_CLONING = True
adapter_mock.get_alter_expressions.return_value = []
Expand Down
Loading