Skip to content

Commit 58b07a0

Browse files
authored
Different approach: decide whether we should recreate based on forward only and virtual env mode flags
1 parent ce5273e commit 58b07a0

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

sqlmesh/core/plan/evaluator.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"""
1616

1717
import abc
18-
import itertools
1918
import logging
2019
import typing as t
2120
from sqlmesh.core import analytics
@@ -380,12 +379,6 @@ def visit_migrate_schemas_stage(
380379
allow_destructive_snapshots=plan.allow_destructive_models,
381380
allow_additive_snapshots=plan.allow_additive_models,
382381
deployability_index=stage.deployability_index,
383-
directly_or_indirectly_modified_snapshots_ids=set(
384-
itertools.chain(
385-
*plan.indirectly_modified_snapshots.values(),
386-
plan.directly_modified_snapshots,
387-
)
388-
),
389382
)
390383
except NodeExecutionFailedError as ex:
391384
raise PlanError(str(ex.__cause__) if ex.__cause__ else str(ex))

sqlmesh/core/snapshot/evaluator.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ def migrate(
477477
allow_destructive_snapshots: t.Optional[t.Set[str]] = None,
478478
allow_additive_snapshots: t.Optional[t.Set[str]] = None,
479479
deployability_index: t.Optional[DeployabilityIndex] = None,
480-
directly_or_indirectly_modified_snapshots_ids: t.Optional[t.Set[SnapshotId]] = None,
481480
) -> None:
482481
"""Alters a physical snapshot table to match its snapshot's schema for the given collection of snapshots.
483482
@@ -487,7 +486,6 @@ def migrate(
487486
allow_destructive_snapshots: Set of snapshots that are allowed to have destructive schema changes.
488487
allow_additive_snapshots: Set of snapshots that are allowed to have additive schema changes.
489488
deployability_index: Determines snapshots that are deployable in the context of this evaluation.
490-
directly_or_indirectly_modified_snapshots_ids: Set of SnapshotIds with direct or indirect changes.
491489
"""
492490
deployability_index = deployability_index or DeployabilityIndex.all_deployable()
493491
target_data_objects = self._get_physical_data_objects(target_snapshots, deployability_index)
@@ -512,10 +510,6 @@ def migrate(
512510
allow_additive_snapshots,
513511
self.get_adapter(s.model_gateway),
514512
deployability_index,
515-
only_metadata_changes=s.snapshot_id
516-
not in directly_or_indirectly_modified_snapshots_ids
517-
if directly_or_indirectly_modified_snapshots_ids is not None
518-
else False,
519513
),
520514
self.ddl_concurrent_tasks,
521515
)
@@ -1117,7 +1111,6 @@ def _migrate_snapshot(
11171111
allow_additive_snapshots: t.Set[str],
11181112
adapter: EngineAdapter,
11191113
deployability_index: DeployabilityIndex,
1120-
only_metadata_changes: bool,
11211114
) -> None:
11221115
if not snapshot.is_model or snapshot.is_symbolic:
11231116
return
@@ -1161,7 +1154,6 @@ def _migrate_snapshot(
11611154
allow_destructive_snapshots=allow_destructive_snapshots,
11621155
allow_additive_snapshots=allow_additive_snapshots,
11631156
run_pre_post_statements=True,
1164-
only_metadata_changes=only_metadata_changes,
11651157
)
11661158
else:
11671159
self._execute_create(
@@ -1198,7 +1190,6 @@ def _migrate_target_table(
11981190
allow_destructive_snapshots: t.Set[str],
11991191
allow_additive_snapshots: t.Set[str],
12001192
run_pre_post_statements: bool = False,
1201-
only_metadata_changes: bool = False,
12021193
) -> None:
12031194
adapter = self.get_adapter(snapshot.model.gateway)
12041195

@@ -1235,7 +1226,6 @@ def _migrate_target_table(
12351226
ignore_destructive=snapshot.model.on_destructive_change.is_ignore,
12361227
ignore_additive=snapshot.model.on_additive_change.is_ignore,
12371228
deployability_index=deployability_index,
1238-
only_metadata_changes=only_metadata_changes,
12391229
)
12401230
finally:
12411231
if snapshot.is_materialized:
@@ -2770,8 +2760,12 @@ def migrate(
27702760
**kwargs: t.Any,
27712761
) -> None:
27722762
logger.info("Migrating view '%s'", target_table_name)
2773-
if not (
2774-
kwargs["only_metadata_changes"] and self.adapter.COMMENT_CREATION_VIEW.is_unsupported
2763+
# Optimization: avoid unnecessary recreation when possible
2764+
if (
2765+
snapshot.model.forward_only
2766+
or bool(snapshot.model.physical_version)
2767+
or not snapshot.virtual_environment_mode.is_full
2768+
or not self.adapter.COMMENT_CREATION_VIEW.is_unsupported
27752769
):
27762770
model = snapshot.model
27772771
render_kwargs = dict(

tests/core/test_snapshot_evaluator.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,10 +1520,20 @@ def test_migrate_view(
15201520
)
15211521

15221522

1523+
@pytest.mark.parametrize(
1524+
"change_category",
1525+
[
1526+
SnapshotChangeCategory.BREAKING,
1527+
SnapshotChangeCategory.NON_BREAKING,
1528+
SnapshotChangeCategory.METADATA,
1529+
],
1530+
)
1531+
@pytest.mark.tz
15231532
def test_migrate_view_recreation_not_needed(
15241533
mocker: MockerFixture,
15251534
make_snapshot,
15261535
make_mocked_engine_adapter,
1536+
change_category: SnapshotChangeCategory,
15271537
):
15281538
model = SqlModel(
15291539
name="test_schema.test_model",
@@ -1532,7 +1542,7 @@ def test_migrate_view_recreation_not_needed(
15321542
query=parse_one("SELECT c, a FROM tbl"),
15331543
)
15341544
snapshot = make_snapshot(model, version="1")
1535-
snapshot.change_category = SnapshotChangeCategory.METADATA
1545+
snapshot.change_category = change_category
15361546
snapshot.forward_only = False
15371547

15381548
adapter = make_mocked_engine_adapter(EngineAdapter)
@@ -1550,7 +1560,7 @@ def test_migrate_view_recreation_not_needed(
15501560
)
15511561

15521562
evaluator = SnapshotEvaluator(adapter)
1553-
evaluator.migrate([snapshot], {}, directly_or_indirectly_modified_snapshots_ids=set())
1563+
evaluator.migrate([snapshot], {})
15541564

15551565
adapter.cursor.execute.assert_not_called()
15561566

0 commit comments

Comments
 (0)