From 091af7dd08d0526414f5ed3bca7232f25afe7b42 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Thu, 25 Apr 2024 13:26:17 -0400 Subject: [PATCH] use a list instead of a set for index changes to perserve order --- dbt/adapters/postgres/relation.py | 20 +++++++++++-------- .../relation_configs/materialized_view.py | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/dbt/adapters/postgres/relation.py b/dbt/adapters/postgres/relation.py index e546eab4..e8128c46 100644 --- a/dbt/adapters/postgres/relation.py +++ b/dbt/adapters/postgres/relation.py @@ -1,5 +1,5 @@ from dataclasses import dataclass, field -from typing import FrozenSet, Optional, Set +from typing import FrozenSet, List, Optional from dbt.adapters.base.relation import BaseRelation from dbt.adapters.contracts.relation import RelationConfig, RelationType @@ -79,7 +79,7 @@ def _get_index_config_changes( self, existing_indexes: FrozenSet[PostgresIndexConfig], new_indexes: FrozenSet[PostgresIndexConfig], - ) -> Set[PostgresIndexConfigChange]: + ) -> List[PostgresIndexConfigChange]: """ Get the index updates that will occur as a result of a new run @@ -90,18 +90,22 @@ def _get_index_config_changes( 3. Index is old -> drop these 4. Indexes are not equal -> drop old, create new -> two actions - Returns: a set of index updates in the form {"action": "drop/create", "context": } + *Note:* + The order of the operations matters here because if the same index is dropped and recreated + (e.g. via --full-refresh) then we need to drop it first, then create it. + + Returns: an ordered list of index updates in the form {"action": "drop/create", "context": } """ - drop_changes = set( + drop_changes = [ PostgresIndexConfigChange.from_dict( {"action": RelationConfigChangeAction.drop, "context": index} ) for index in existing_indexes.difference(new_indexes) - ) - create_changes = set( + ] + create_changes = [ PostgresIndexConfigChange.from_dict( {"action": RelationConfigChangeAction.create, "context": index} ) for index in new_indexes.difference(existing_indexes) - ) - return set().union(drop_changes, create_changes) # type: ignore + ] + return drop_changes + create_changes diff --git a/dbt/adapters/postgres/relation_configs/materialized_view.py b/dbt/adapters/postgres/relation_configs/materialized_view.py index 3563833e..8eaccbbf 100644 --- a/dbt/adapters/postgres/relation_configs/materialized_view.py +++ b/dbt/adapters/postgres/relation_configs/materialized_view.py @@ -101,7 +101,7 @@ def parse_relation_results(cls, relation_results: RelationResults) -> dict: @dataclass class PostgresMaterializedViewConfigChangeCollection: - indexes: Set[PostgresIndexConfigChange] = field(default_factory=set) + indexes: List[PostgresIndexConfigChange] = field(default_factory=list) @property def requires_full_refresh(self) -> bool: @@ -109,4 +109,4 @@ def requires_full_refresh(self) -> bool: @property def has_changes(self) -> bool: - return self.indexes != set() + return self.indexes != []