Skip to content

Commit

Permalink
use a list instead of a set for index changes to perserve order
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare committed Apr 25, 2024
1 parent 6a1897e commit 091af7d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions dbt/adapters/postgres/relation.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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": <IndexConfig>}
*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": <IndexConfig>}
"""
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
4 changes: 2 additions & 2 deletions dbt/adapters/postgres/relation_configs/materialized_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ 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:
return any(index.requires_full_refresh for index in self.indexes)

@property
def has_changes(self) -> bool:
return self.indexes != set()
return self.indexes != []

0 comments on commit 091af7d

Please sign in to comment.