@@ -473,13 +473,11 @@ def test_promote_snapshots(state_sync: EngineAdapterStateSync, make_snapshot: t.
473473 [snapshot_a , snapshot_b_old , snapshot_c ],
474474 "prod" ,
475475 )
476- assert set (promotion_result .added ) == set (
477- [
478- snapshot_a .table_info ,
479- snapshot_b_old .table_info ,
480- snapshot_c .table_info ,
481- ]
482- )
476+ assert set (promotion_result .added ) == {
477+ snapshot_a .table_info ,
478+ snapshot_b_old .table_info ,
479+ snapshot_c .table_info ,
480+ }
483481 assert not promotion_result .removed
484482 assert not promotion_result .removed_environment_naming_info
485483
@@ -550,7 +548,7 @@ def test_promote_snapshots_suffix_change(
550548 environment_suffix_target = EnvironmentSuffixTarget .TABLE ,
551549 )
552550
553- assert set (promotion_result .added ) == set ([ snapshot_a .table_info , snapshot_b .table_info ])
551+ assert set (promotion_result .added ) == { snapshot_a .table_info , snapshot_b .table_info }
554552 assert not promotion_result .removed
555553 assert not promotion_result .removed_environment_naming_info
556554
@@ -572,76 +570,156 @@ def test_promote_snapshots_suffix_change(
572570 )
573571
574572 # We still only add the snapshots that are included in the promotion
575- assert set (promotion_result .added ) == set ([ snapshot_b .table_info , snapshot_c .table_info ])
576- # We also remove b because of the suffix target change. The new one will be created in the new suffix target
577- assert set (promotion_result .removed ) == set ([ snapshot_a .table_info , snapshot_b . table_info ])
573+ assert set (promotion_result .added ) == { snapshot_b .table_info , snapshot_c .table_info }
574+ # B does not get removed because the suffix target change doesn't affect it due to running in prod.
575+ assert set (promotion_result .removed ) == { snapshot_a .table_info }
578576 # Make sure the removed suffix target is correctly seen as table
579- assert promotion_result .removed_environment_naming_info
577+ assert promotion_result .removed_environment_naming_info is not None
580578 assert promotion_result .removed_environment_naming_info .suffix_target .is_table
581579
580+ promotion_result = promote_snapshots (
581+ state_sync ,
582+ [snapshot_b , snapshot_c ],
583+ "dev" ,
584+ environment_suffix_target = EnvironmentSuffixTarget .SCHEMA ,
585+ )
586+
587+ # We still only add the snapshots that are included in the promotion
588+ assert set (promotion_result .added ) == {snapshot_b .table_info , snapshot_c .table_info }
589+ assert len (promotion_result .removed ) == 0
590+ assert promotion_result .removed_environment_naming_info is None
591+
592+ promotion_result = promote_snapshots (
593+ state_sync ,
594+ [snapshot_b , snapshot_c ],
595+ "dev" ,
596+ environment_suffix_target = EnvironmentSuffixTarget .TABLE ,
597+ )
598+
599+ # All snapshots are promoted due to suffix target change
600+ assert set (promotion_result .added ) == {
601+ snapshot_b .table_info ,
602+ snapshot_c .table_info ,
603+ }
604+ # All snapshots are removed due to suffix target change
605+ assert set (promotion_result .removed ) == {
606+ snapshot_b .table_info ,
607+ snapshot_c .table_info ,
608+ }
609+ assert promotion_result .removed_environment_naming_info is not None
610+ assert promotion_result .removed_environment_naming_info .suffix_target .is_schema
611+
582612
583613def test_promote_snapshots_catalog_name_override_change (
584614 state_sync : EngineAdapterStateSync , make_snapshot : t .Callable
585615):
586616 snapshot_a = make_snapshot (
587617 SqlModel (
588- name = "a" ,
618+ name = "catalog1.schema. a" ,
589619 query = parse_one ("select 1, ds" ),
590620 ),
591621 )
592622 snapshot_a .categorize_as (SnapshotChangeCategory .BREAKING )
593623
594624 snapshot_b = make_snapshot (
595625 SqlModel (
596- name = "b" ,
626+ name = "catalog1.schema. b" ,
597627 kind = FullKind (),
598628 query = parse_one ("select * from a" ),
599629 ),
600630 nodes = {"a" : snapshot_a .model },
601631 )
602632 snapshot_b .categorize_as (SnapshotChangeCategory .BREAKING )
603633
604- state_sync .push_snapshots ([snapshot_a , snapshot_b ])
634+ snapshot_c = make_snapshot (
635+ SqlModel (
636+ name = "catalog2.schema.c" ,
637+ kind = FullKind (),
638+ query = parse_one ("select * from a" ),
639+ ),
640+ nodes = {"a" : snapshot_a .model },
641+ )
642+ snapshot_c .categorize_as (SnapshotChangeCategory .BREAKING )
643+
644+ state_sync .push_snapshots ([snapshot_a , snapshot_b , snapshot_c ])
605645
606646 promotion_result = promote_snapshots (
607647 state_sync ,
608- [snapshot_a , snapshot_b ],
648+ [snapshot_a , snapshot_b , snapshot_c ],
609649 "prod" ,
610- environment_suffix_target = EnvironmentSuffixTarget .TABLE ,
611650 environment_catalog_mapping = {},
612651 )
613652
614- assert set (promotion_result .added ) == set ([snapshot_a .table_info , snapshot_b .table_info ])
653+ assert set (promotion_result .added ) == {
654+ snapshot_a .table_info ,
655+ snapshot_b .table_info ,
656+ snapshot_c .table_info ,
657+ }
615658 assert not promotion_result .removed
616659 assert not promotion_result .removed_environment_naming_info
617660
618- snapshot_c = make_snapshot (
661+ snapshot_d = make_snapshot (
619662 SqlModel (
620- name = "c " ,
663+ name = "catalog1.schema.d " ,
621664 query = parse_one ("select 3, ds" ),
622665 ),
623666 )
624- snapshot_c .categorize_as (SnapshotChangeCategory .BREAKING )
667+ snapshot_d .categorize_as (SnapshotChangeCategory .BREAKING )
625668
626- state_sync .push_snapshots ([snapshot_c ])
669+ state_sync .push_snapshots ([snapshot_d ])
627670
628671 promotion_result = promote_snapshots (
629672 state_sync ,
630- [snapshot_b , snapshot_c ],
673+ [snapshot_b , snapshot_c , snapshot_d ],
631674 "prod" ,
632675 environment_catalog_mapping = {
633- re .compile ("^prod$" ): "prod_catalog " ,
676+ re .compile ("^prod$" ): "catalog1 " ,
634677 },
635678 )
636679
637- # We still only add the snapshots that are included in the promotion
638- assert set (promotion_result .added ) == set ([snapshot_b .table_info , snapshot_c .table_info ])
639- # We also remove b because of the catalog change. The new one will be created in the new catalog
640- assert set (promotion_result .removed ) == set ([snapshot_a .table_info , snapshot_b .table_info ])
680+ # We still only add the snapshots that are included in the promotion which means removing A
681+ assert set (promotion_result .added ) == {
682+ snapshot_b .table_info ,
683+ snapshot_c .table_info ,
684+ snapshot_d .table_info ,
685+ }
686+ # C is removed because of the catalog change. The new one will be created in the new catalog.
687+ # B is not removed because it's catalog did not change and therefore removing would actually result
688+ # in dropping what we just added.
689+ # A is removed because it was explicitly removed from the promotion.
690+ assert set (promotion_result .removed ) == {snapshot_a .table_info , snapshot_c .table_info }
641691 # Make sure the removed suffix target correctly has the old catalog name set
642692 assert promotion_result .removed_environment_naming_info
643693 assert promotion_result .removed_environment_naming_info .catalog_name_override is None
644694
695+ promotion_result = promote_snapshots (
696+ state_sync ,
697+ [snapshot_b , snapshot_c , snapshot_d ],
698+ "prod" ,
699+ environment_catalog_mapping = {
700+ re .compile ("^prod$" ): "catalog2" ,
701+ },
702+ )
703+
704+ # All are added since their catalog was changed
705+ assert set (promotion_result .added ) == {
706+ snapshot_b .table_info ,
707+ snapshot_c .table_info ,
708+ snapshot_d .table_info ,
709+ }
710+ # All are removed since there were moved from their old catalog location
711+ # Note that C has a catalog set in the model definition of `catalog2` which is what we moved to so you might think
712+ # it shouldn't be removed, but its actual catalog was `catalog1` because of the previous override so therefore
713+ # it should be removed from `catalog1`.
714+ assert set (promotion_result .removed ) == {
715+ snapshot_b .table_info ,
716+ snapshot_c .table_info ,
717+ snapshot_d .table_info ,
718+ }
719+ # Make sure the removed suffix target correctly has the old catalog name set
720+ assert promotion_result .removed_environment_naming_info
721+ assert promotion_result .removed_environment_naming_info .catalog_name_override == "catalog1"
722+
645723
646724def test_promote_snapshots_parent_plan_id_mismatch (
647725 state_sync : EngineAdapterStateSync , make_snapshot : t .Callable
0 commit comments