Skip to content

Commit

Permalink
refactor: compare index instances before set hash and eq func to class
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Dec 7, 2024
1 parent 5de31d7 commit c168d24
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions aerich/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Migrate:
migrate_location: Path
dialect: str
_db_version: Optional[str] = None
_patched_index_classes: set[Type[Index]] = set()

@staticmethod
def get_field_by_name(name: str, fields: List[dict]) -> dict:
Expand Down Expand Up @@ -191,25 +190,20 @@ def _add_operator(cls, operator: str, upgrade=True, fk_m2m_index=False) -> None:

@classmethod
def _handle_indexes(cls, model: Type[Model], indexes: List[Union[Tuple[str], Index]]) -> list:
ret: list = []
for index in indexes:
if (
isinstance(index, Index)
and (index_cls := type(index)) not in cls._patched_index_classes
):

def _hash(self) -> int:
ident = self.index_name(cls.ddl.schema_generator, model) + index_cls.__name__
return hash(ident)

def _eq(self, other) -> bool:
return type(other) is index_cls and hash(self) == hash(other)

setattr(index_cls, "__hash__", _hash)
setattr(index_cls, "__eq__", _eq)
cls._patched_index_classes.add(index_cls)
ret.append(index)
return ret
if index_classes := set(index.__class__ for index in indexes if isinstance(index, Index)):
for index_cls in index_classes:
if index_cls(fields=("id",)) != index_cls(fields=("id",)):

def _hash(self) -> int:
ident = tuple(self.fields), model, self.name, self.expressions
return hash(ident)

def _eq(self, other) -> bool:
return type(other) is index_cls and hash(self) == hash(other)

setattr(index_cls, "__hash__", _hash)
setattr(index_cls, "__eq__", _eq)
return indexes

@classmethod
def _get_indexes(cls, model, model_describe: dict) -> Set[Union[Index, Tuple[str, ...]]]:
Expand Down

0 comments on commit c168d24

Please sign in to comment.