Skip to content

Commit 8ccee70

Browse files
fix(plan): migrate views that depend on a migrated view (#6200)
Signed-off-by: ReguiguiMohamed <mohamedreguigui2004@gmail.com>
1 parent 3ef1e0a commit 8ccee70

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎sqlmesh/core/plan/stages.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,23 @@ def build(self, plan: EvaluatablePlan) -> t.List[PlanStage]:
279279
]
280280
# Include all upstream dependencies of snapshots that require schema migration to make sure
281281
# the upstream tables are created before the schema updates are applied
282+
snapshot_ids_to_migrate = set(dag.subdag(*snapshot_ids_with_schema_migration))
283+
# Migrating a view replaces it, and engines like Postgres drop its dependent views with CASCADE,
284+
# so those are migrated too to be recreated
285+
for s_id in dag:
286+
snapshot = snapshots[s_id]
287+
if snapshot.is_view and any(
288+
p_id in snapshot_ids_to_migrate
289+
and snapshots[p_id].is_view
290+
and snapshots[p_id].supports_schema_migration_in_prod
291+
for p_id in snapshot.parents
292+
):
293+
snapshot_ids_to_migrate.add(s_id)
282294
snapshots_with_schema_migration = [
283295
snapshots[s_id]
284-
for s_id in dag.subdag(*snapshot_ids_with_schema_migration)
285-
if snapshots[s_id].supports_schema_migration_in_prod
296+
for s_id in dag
297+
if s_id in snapshot_ids_to_migrate
298+
and snapshots[s_id].supports_schema_migration_in_prod
286299
]
287300

288301
snapshots_to_intervals = self._missing_intervals(

‎tests/core/test_plan_stages.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,85 @@ def test_build_plan_stages_indirect_non_breaking_view_migration(
16881688
assert isinstance(stages[8], FinalizeEnvironmentStage)
16891689

16901690

1691+
@pytest.mark.parametrize("base_view_unpaused", [False, True])
1692+
def test_build_plan_stages_migrates_views_downstream_of_migrated_view(
1693+
snapshot_a: Snapshot, make_snapshot, mocker: MockerFixture, base_view_unpaused: bool
1694+
) -> None:
1695+
base_view = make_snapshot(
1696+
SqlModel(name="base_view", query=parse_one("select * from a"), kind=ModelKindName.VIEW),
1697+
nodes={'"a"': snapshot_a.model},
1698+
)
1699+
base_view.categorize_as(SnapshotChangeCategory.BREAKING)
1700+
if base_view_unpaused:
1701+
base_view.unpaused_ts = 1
1702+
public_view = make_snapshot(
1703+
SqlModel(
1704+
name="public_view", query=parse_one("select * from base_view"), kind=ModelKindName.VIEW
1705+
),
1706+
nodes={'"a"': snapshot_a.model, '"base_view"': base_view.model},
1707+
)
1708+
public_view.categorize_as(SnapshotChangeCategory.BREAKING)
1709+
forward_only = make_snapshot(
1710+
SqlModel(
1711+
name="forward_only",
1712+
query=parse_one("select 1, ds from base_view"),
1713+
kind=dict(
1714+
name=ModelKindName.INCREMENTAL_BY_TIME_RANGE, time_column="ds", forward_only=True
1715+
),
1716+
),
1717+
nodes={'"a"': snapshot_a.model, '"base_view"': base_view.model},
1718+
)
1719+
forward_only.categorize_as(SnapshotChangeCategory.BREAKING)
1720+
snapshots = [snapshot_a, base_view, public_view, forward_only]
1721+
1722+
state_reader = mocker.Mock(spec=StateReader)
1723+
state_reader.get_snapshots.return_value = {}
1724+
state_reader.get_environment.return_value = None
1725+
1726+
environment = Environment(
1727+
snapshots=[s.table_info for s in snapshots],
1728+
start_at="2023-01-01",
1729+
end_at="2023-01-02",
1730+
plan_id="test_plan",
1731+
previous_plan_id=None,
1732+
promoted_snapshot_ids=[s.snapshot_id for s in snapshots],
1733+
)
1734+
plan = EvaluatablePlan(
1735+
start="2023-01-01",
1736+
end="2023-01-02",
1737+
new_snapshots=snapshots,
1738+
environment=environment,
1739+
no_gaps=False,
1740+
skip_backfill=False,
1741+
empty_backfill=False,
1742+
restatements={},
1743+
restate_all_snapshots=False,
1744+
is_dev=False,
1745+
allow_destructive_models=set(),
1746+
allow_additive_models=set(),
1747+
forward_only=False,
1748+
end_bounded=False,
1749+
ensure_finalized_snapshots=False,
1750+
ignore_cron=False,
1751+
directly_modified_snapshots=[s.snapshot_id for s in snapshots],
1752+
indirectly_modified_snapshots={},
1753+
metadata_updated_snapshots=[],
1754+
removed_snapshots=[],
1755+
requires_backfill=True,
1756+
models_to_backfill=None,
1757+
execution_time="2023-01-02",
1758+
disabled_restatement_models=set(),
1759+
environment_statements=None,
1760+
user_provided_flags=None,
1761+
)
1762+
1763+
stages = build_plan_stages(plan, state_reader, None)
1764+
1765+
migrate_stage = next(s for s in stages if isinstance(s, MigrateSchemasStage))
1766+
expected = [snapshot_a, forward_only] if base_view_unpaused else snapshots
1767+
assert {s.snapshot_id for s in migrate_stage.snapshots} == {s.snapshot_id for s in expected}
1768+
1769+
16911770
def test_build_plan_stages_virtual_environment_mode_filtering(
16921771
make_snapshot, mocker: MockerFixture
16931772
) -> None:

0 commit comments

Comments
 (0)