Skip to content

Commit a74d550

Browse files
committed
Reverted representativeness logic to before based on testing.
Signed-off-by: davem-bis <68955845+davem-bis@users.noreply.github.com>
1 parent b0443a6 commit a74d550

3 files changed

Lines changed: 72 additions & 63 deletions

File tree

sqlmesh/core/snapshot/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ def create(
17221722
this_deployable = False
17231723

17241724
if not snapshot.is_paused or (
1725-
not snapshot.is_indirect_non_breaking and snapshot.intervals
1725+
snapshot.is_indirect_non_breaking and snapshot.intervals
17261726
):
17271727
# This snapshot represents what's currently deployed in prod.
17281728
representative_shared_version_ids.add(node)

tests/core/test_snapshot.py

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -637,40 +637,46 @@ def test_missing_intervals_start_override_per_model(make_snapshot: t.Callable[..
637637
(to_timestamp("2023-02-07"), to_timestamp("2023-02-08")),
638638
]
639639

640+
640641
def test__missing_intervals__deployable_snapshot_prod_intervals_returned(snapshot: Snapshot):
641642
# Arrange
642643
snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=False)
643644
snapshot.add_interval(start="2020-01-02", end="2020-01-02", is_dev=False)
644645
snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=True)
645-
646+
646647
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
647648
deployability_index = DeployabilityIndex.create([snapshot])
648649

649650
# Act
650-
missing_intervals = snapshot.missing_intervals(start="2020-01-01", end="2020-01-03", deployability_index=deployability_index)
651+
missing_intervals = snapshot.missing_intervals(
652+
start="2020-01-01", end="2020-01-03", deployability_index=deployability_index
653+
)
651654

652655
# Assert
653656
assert deployability_index.is_deployable(snapshot)
654657
assert missing_intervals == [
655658
(to_timestamp("2020-01-03"), to_timestamp("2020-01-04")),
656659
]
657-
660+
661+
658662
def test__missing_intervals__non_deployable_snapshot_dev_intervals_returned(snapshot: Snapshot):
659663
# Arrange
660664
snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=False)
661665
snapshot.add_interval(start="2020-01-02", end="2020-01-02", is_dev=False)
662666
snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=True)
663-
667+
664668
snapshot.categorize_as(SnapshotChangeCategory.BREAKING, forward_only=True)
665669
deployability_index = DeployabilityIndex.create([snapshot])
666670

667671
# Act
668-
missing_intervals = snapshot.missing_intervals(start="2020-01-01", end="2020-01-03", deployability_index=deployability_index)
672+
missing_intervals = snapshot.missing_intervals(
673+
start="2020-01-01", end="2020-01-03", deployability_index=deployability_index
674+
)
669675

670676
# Assert
671677
assert not deployability_index.is_deployable(snapshot)
672678
assert missing_intervals == [
673-
(to_timestamp("2020-01-02"), to_timestamp("2020-01-03")), # Not missing from prod intervals
679+
(to_timestamp("2020-01-02"), to_timestamp("2020-01-03")), # Not missing from prod intervals
674680
(to_timestamp("2020-01-03"), to_timestamp("2020-01-04")),
675681
]
676682

@@ -2444,99 +2450,102 @@ def test_earliest_start_date(sushi_context: Context):
24442450

24452451

24462452
def test_deployability_index(make_snapshot):
2447-
# Breaking change - should be both representantive / deployable
2448-
snapshot_breaking = make_snapshot(SqlModel(name="a", query=parse_one("SELECT 1")))
2453+
# Breaking change - should be both deployable / representative
2454+
snapshot_breaking = make_snapshot(SqlModel(name="breaking", query=parse_one("SELECT 1")))
24492455
snapshot_breaking.categorize_as(SnapshotChangeCategory.BREAKING)
24502456

2451-
# Forward only breaking change - cannot be representative / deployable due to forward only
2452-
snapshot_breaking_forward_only = make_snapshot(SqlModel(name="b", query=parse_one("SELECT 1")))
2457+
# Forward only breaking change - cannot be deployable / representative due to forward only
2458+
snapshot_breaking_forward_only = make_snapshot(
2459+
SqlModel(name="forward_only", query=parse_one("SELECT 1"))
2460+
)
24532461
snapshot_breaking_forward_only.categorize_as(SnapshotChangeCategory.BREAKING, forward_only=True)
24542462
snapshot_breaking_forward_only.parents = (snapshot_breaking.snapshot_id,)
24552463

2456-
# Indirect breaking - cannot be representative / deployable due to forward only non-deployable parent
2457-
snapshot_indirect_breaking_1 = make_snapshot(SqlModel(name="c", query=parse_one("SELECT 1")))
2458-
snapshot_indirect_breaking_1.categorize_as(SnapshotChangeCategory.INDIRECT_BREAKING)
2459-
snapshot_indirect_breaking_1.parents = (snapshot_breaking_forward_only.snapshot_id,)
2460-
2461-
# Indirect breaking - cannot be representative / deployable due to forward only non-deployable parent
2462-
snapshot_indirect_breaking_2 = make_snapshot(SqlModel(name="d", query=parse_one("SELECT 1")))
2463-
snapshot_indirect_breaking_2.categorize_as(SnapshotChangeCategory.INDIRECT_BREAKING)
2464-
snapshot_indirect_breaking_2.parents = (
2464+
# Indirect breaking - cannot be deployable / representative due to forward only non-representative parent
2465+
snapshot_indirect_breaking_non_representative_parent = make_snapshot(
2466+
SqlModel(name="indirect_breaking_non_representative_parent", query=parse_one("SELECT 1"))
2467+
)
2468+
snapshot_indirect_breaking_non_representative_parent.categorize_as(
2469+
SnapshotChangeCategory.INDIRECT_BREAKING
2470+
)
2471+
snapshot_indirect_breaking_non_representative_parent.parents = (
24652472
snapshot_breaking_forward_only.snapshot_id,
2466-
snapshot_breaking.snapshot_id,
24672473
)
24682474

2469-
# Non breaking - representative / deployable due to no breaking changes
2470-
snapshot_non_breaking = make_snapshot(SqlModel(name="e", query=parse_one("SELECT 1")))
2471-
snapshot_non_breaking.categorize_as(SnapshotChangeCategory.NON_BREAKING)
2472-
2473-
# Indirect breaking - can be representative / deployable due to deployable parents
2474-
snapshot_indirect_breaking_deployable_parents = make_snapshot(
2475-
SqlModel(name="f", query=parse_one("SELECT 1"))
2475+
# Indirect breaking - can be deployable / representative due to forward only representative parent
2476+
snapshot_indirect_breaking_representative_parent = make_snapshot(
2477+
SqlModel(name="indirect_breaking_representative_parent", query=parse_one("SELECT 1"))
24762478
)
2477-
snapshot_indirect_breaking_deployable_parents.categorize_as(
2479+
snapshot_indirect_breaking_representative_parent.categorize_as(
24782480
SnapshotChangeCategory.INDIRECT_BREAKING
24792481
)
2480-
snapshot_indirect_breaking_deployable_parents.parents = (
2481-
snapshot_non_breaking.snapshot_id,
2482-
snapshot_breaking.snapshot_id,
2482+
snapshot_indirect_breaking_representative_parent.parents = (snapshot_breaking.snapshot_id,)
2483+
2484+
# Non breaking - deployable / representative due to no breaking changes
2485+
snapshot_non_breaking = make_snapshot(
2486+
SqlModel(name="non_breaking", query=parse_one("SELECT 1"))
24832487
)
2488+
snapshot_non_breaking.categorize_as(SnapshotChangeCategory.NON_BREAKING)
24842489

2485-
# Indirect non breaking - cannot be representative / deployable due to possible data drift from prod
2486-
snapshot_indirect_non_breaking = make_snapshot(SqlModel(name="g", query=parse_one("SELECT 1")))
2490+
# Indirect non breaking - can be representative but not deployable
2491+
snapshot_indirect_non_breaking = make_snapshot(
2492+
SqlModel(name="indirect_non_breaking", query=parse_one("SELECT 1"))
2493+
)
24872494
snapshot_indirect_non_breaking.intervals = [
24882495
(to_timestamp("2023-01-01"), to_timestamp("2023-01-02"))
24892496
]
24902497
snapshot_indirect_non_breaking.categorize_as(SnapshotChangeCategory.INDIRECT_NON_BREAKING)
24912498
snapshot_indirect_non_breaking.parents = (snapshot_non_breaking.snapshot_id,)
24922499

2493-
# Indirect breaking - cannot be representative / deployable due to indirect non breaking non-deployable parent
2494-
snapshot_indirect_breaking_non_deployable_parents = make_snapshot(
2495-
SqlModel(name="h", query=parse_one("SELECT 1"))
2500+
# Breaking with non-representative parent - cannot be deployable due to non-representative parent
2501+
snapshot_breaking_non_representative_parent = make_snapshot(
2502+
SqlModel(name="breaking_non_deployable_parents", query=parse_one("SELECT 1"))
24962503
)
2497-
snapshot_indirect_breaking_non_deployable_parents.categorize_as(
2498-
SnapshotChangeCategory.INDIRECT_BREAKING
2499-
)
2500-
snapshot_indirect_breaking_non_deployable_parents.parents = (
2501-
snapshot_indirect_non_breaking.snapshot_id,
2504+
snapshot_breaking_non_representative_parent.categorize_as(SnapshotChangeCategory.BREAKING)
2505+
snapshot_breaking_non_representative_parent.parents = (
2506+
snapshot_breaking_forward_only.snapshot_id,
25022507
)
25032508

25042509
snapshots = {
25052510
s.snapshot_id: s
25062511
for s in [
25072512
snapshot_breaking,
25082513
snapshot_breaking_forward_only,
2509-
snapshot_indirect_breaking_1,
2510-
snapshot_indirect_breaking_2,
2514+
snapshot_indirect_breaking_non_representative_parent,
2515+
snapshot_indirect_breaking_representative_parent,
25112516
snapshot_non_breaking,
2512-
snapshot_indirect_breaking_deployable_parents,
25132517
snapshot_indirect_non_breaking,
2514-
snapshot_indirect_breaking_non_deployable_parents,
2518+
snapshot_breaking_non_representative_parent,
25152519
]
25162520
}
25172521

25182522
deployability_index = DeployabilityIndex.create(snapshots)
25192523

25202524
assert deployability_index.is_deployable(snapshot_breaking)
2521-
assert deployability_index.is_deployable(snapshot_non_breaking)
2522-
assert deployability_index.is_deployable(snapshot_indirect_breaking_deployable_parents)
2525+
assert deployability_index.is_representative(snapshot_breaking)
2526+
25232527
assert not deployability_index.is_deployable(snapshot_breaking_forward_only)
2524-
assert not deployability_index.is_deployable(snapshot_indirect_breaking_1)
2525-
assert not deployability_index.is_deployable(snapshot_indirect_breaking_2)
2526-
assert not deployability_index.is_deployable(snapshot_indirect_non_breaking)
2527-
assert not deployability_index.is_deployable(snapshot_indirect_breaking_non_deployable_parents)
2528+
assert not deployability_index.is_representative(snapshot_breaking_forward_only)
25282529

2529-
assert deployability_index.is_representative(snapshot_breaking)
2530+
assert deployability_index.is_deployable(snapshot_non_breaking)
25302531
assert deployability_index.is_representative(snapshot_non_breaking)
2531-
assert deployability_index.is_representative(snapshot_indirect_breaking_deployable_parents)
2532-
assert not deployability_index.is_representative(snapshot_breaking_forward_only)
2533-
assert not deployability_index.is_representative(snapshot_indirect_breaking_1)
2534-
assert not deployability_index.is_representative(snapshot_indirect_breaking_2)
2535-
assert not deployability_index.is_representative(snapshot_indirect_non_breaking)
2532+
2533+
assert not deployability_index.is_deployable(
2534+
snapshot_indirect_breaking_non_representative_parent
2535+
)
25362536
assert not deployability_index.is_representative(
2537-
snapshot_indirect_breaking_non_deployable_parents
2537+
snapshot_indirect_breaking_non_representative_parent
25382538
)
25392539

2540+
assert deployability_index.is_deployable(snapshot_indirect_breaking_representative_parent)
2541+
assert deployability_index.is_representative(snapshot_indirect_breaking_representative_parent)
2542+
2543+
assert not deployability_index.is_deployable(snapshot_indirect_non_breaking)
2544+
assert deployability_index.is_representative(snapshot_indirect_non_breaking)
2545+
2546+
assert not deployability_index.is_deployable(snapshot_breaking_non_representative_parent)
2547+
assert not deployability_index.is_representative(snapshot_breaking_non_representative_parent)
2548+
25402549
all_deployable_index = deployability_index.all_deployable()
25412550
assert all(all_deployable_index.is_deployable(s) for s in snapshots.values())
25422551
assert all(all_deployable_index.is_representative(s) for s in snapshots.values())
@@ -3999,4 +4008,3 @@ def test_snapshot_id_and_version_optional_kind_name():
39994008
assert snapshot.model_kind_name
40004009
assert snapshot.is_incremental_unmanaged
40014010
assert snapshot.full_history_restatement_only
4002-

tests/core/test_snapshot_evaluator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def increment_stage_counter(evaluator) -> None:
307307
)
308308

309309

310-
def test_promote__deployable__non_dev_physical(mocker: MockerFixture, adapter_mock, make_snapshot):
310+
def test_promote__deployable__prod_physical(mocker: MockerFixture, adapter_mock, make_snapshot):
311311
evaluator = SnapshotEvaluator(adapter_mock)
312312

313313
model = SqlModel(
@@ -324,7 +324,7 @@ def test_promote__deployable__non_dev_physical(mocker: MockerFixture, adapter_mo
324324
evaluator.promote(
325325
target_snapshots=[snapshot],
326326
environment_naming_info=EnvironmentNamingInfo(name="test_env"),
327-
deployability_index=deployability_index
327+
deployability_index=deployability_index,
328328
)
329329

330330
adapter_mock.transaction.assert_called()
@@ -358,7 +358,7 @@ def test_promote__non_deployable__dev_physical(mocker: MockerFixture, adapter_mo
358358
evaluator.promote(
359359
target_snapshots=[snapshot],
360360
environment_naming_info=EnvironmentNamingInfo(name="test_env"),
361-
deployability_index=deployability_index
361+
deployability_index=deployability_index,
362362
)
363363

364364
adapter_mock.transaction.assert_called()
@@ -374,6 +374,7 @@ def test_promote__non_deployable__dev_physical(mocker: MockerFixture, adapter_mo
374374
view_properties={},
375375
)
376376

377+
377378
def test_demote(mocker: MockerFixture, adapter_mock, make_snapshot):
378379
evaluator = SnapshotEvaluator(adapter_mock)
379380

0 commit comments

Comments
 (0)