Skip to content

Commit 0150154

Browse files
authored
Fix: Always recreate virtual views for dev environments (#4413)
1 parent 5c4cbb6 commit 0150154

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

sqlmesh/core/state_sync/db/facade.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from sqlglot import exp
2626

27+
from sqlmesh.core import constants as c
2728
from sqlmesh.core.console import Console, get_console
2829
from sqlmesh.core.engine_adapter import EngineAdapter
2930
from sqlmesh.core.environment import Environment, EnvironmentStatements, EnvironmentSummary
@@ -229,6 +230,7 @@ def promote(
229230
and existing_environment.finalized_ts
230231
and not existing_environment.expired
231232
and existing_environment.gateway_managed == environment.gateway_managed
233+
and existing_environment.name == c.PROD
232234
):
233235
# Only promote new snapshots.
234236
added_table_infos -= set(existing_environment.promoted_snapshots)

tests/core/state_sync/test_state_sync.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,10 @@ def test_promote_snapshots_parent_plan_id_mismatch(
865865
state_sync.promote(stale_new_environment)
866866

867867

868-
def test_promote_environment_expired(state_sync: EngineAdapterStateSync, make_snapshot: t.Callable):
868+
@pytest.mark.parametrize("environment_name", ["dev", "prod"])
869+
def test_promote_environment_expired(
870+
state_sync: EngineAdapterStateSync, make_snapshot: t.Callable, environment_name: str
871+
):
869872
snapshot = make_snapshot(
870873
SqlModel(
871874
name="a",
@@ -880,7 +883,7 @@ def test_promote_environment_expired(state_sync: EngineAdapterStateSync, make_sn
880883
state_sync.invalidate_environment("dev")
881884

882885
new_environment = Environment(
883-
name="dev",
886+
name=environment_name,
884887
snapshots=[snapshot.table_info],
885888
start_at="2022-01-01",
886889
end_at="2022-01-01",
@@ -901,10 +904,15 @@ def test_promote_environment_expired(state_sync: EngineAdapterStateSync, make_sn
901904
new_environment.previous_plan_id = new_environment.plan_id
902905
new_environment.plan_id = "another_plan_id"
903906
promotion_result = state_sync.promote(new_environment)
907+
904908
# Should be empty since the environment is no longer expired and nothing has changed
905-
assert promotion_result.added == []
906909
assert promotion_result.removed == []
907910
assert promotion_result.removed_environment_naming_info is None
911+
if environment_name == "prod":
912+
assert promotion_result.added == []
913+
else:
914+
# We should always recreate views in dev environments
915+
assert promotion_result.added == [snapshot.table_info]
908916

909917

910918
def test_promote_snapshots_no_gaps(state_sync: EngineAdapterStateSync, make_snapshot: t.Callable):

tests/core/test_integration.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,6 +4257,28 @@ def test_table_name(init_and_plan_context: t.Callable):
42574257
)
42584258

42594259

4260+
@time_machine.travel("2023-01-08 15:00:00 UTC")
4261+
def test_full_model_change_with_plan_start_not_matching_model_start(
4262+
init_and_plan_context: t.Callable,
4263+
):
4264+
context, plan = init_and_plan_context("examples/sushi")
4265+
context.apply(plan)
4266+
4267+
model = context.get_model("sushi.top_waiters")
4268+
context.upsert_model(model, kind=model_kind_type_from_name("FULL")()) # type: ignore
4269+
4270+
# Apply the change with --skip-backfill first and no plan start
4271+
context.plan("dev", skip_tests=True, skip_backfill=True, no_prompts=True, auto_apply=True)
4272+
4273+
# Apply the plan again but this time don't skip backfill and set start
4274+
# to be later than the model start
4275+
context.plan("dev", skip_tests=True, no_prompts=True, auto_apply=True, start="1 day ago")
4276+
4277+
# Check that the number of rows is not 0
4278+
row_num = context.engine_adapter.fetchone(f"SELECT COUNT(*) FROM sushi__dev.top_waiters")[0]
4279+
assert row_num > 0
4280+
4281+
42604282
@time_machine.travel("2023-01-08 15:00:00 UTC")
42614283
def test_dbt_requirements(sushi_dbt_context: Context):
42624284
assert set(sushi_dbt_context.requirements) == {"dbt-core", "dbt-duckdb"}

0 commit comments

Comments
 (0)