diff --git a/CHANGELOG.md b/CHANGELOG.md index 094dc16..55d4598 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - Fix configuration file reading error when containing Chinese characters. (#286) - sqlite: failed to create/drop index. (#302) - PostgreSQL: Cannot drop constraint after deleting or rename FK on a model. (#378) +- Fix create/drop indexes in every migration. (#377) - Sort m2m fields before comparing them with diff. (#271) #### Changed diff --git a/aerich/migrate.py b/aerich/migrate.py index 3158b93..9d00f7e 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -1,4 +1,5 @@ -import hashlib +from __future__ import annotations + import importlib import os from datetime import datetime @@ -197,21 +198,19 @@ 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 = [] + 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 index_hash(self) -> str: - h = hashlib.new("MD5", usedforsecurity=False) # type:ignore[call-arg] - h.update( - self.index_name(cls.ddl.schema_generator, model).encode() - + self.__class__.__name__.encode() - ) - return h.hexdigest() + def _hash(self) -> int: + return hash((tuple(sorted(self.fields)), self.name, self.expressions)) - for index in indexes: - if isinstance(index, Index): - index.__hash__ = index_hash # type:ignore[method-assign,assignment] - ret.append(index) - return ret + def _eq(self, other) -> bool: + return type(self) is type(other) and self.__dict__ == other.__dict__ + + 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, ...]]]: diff --git a/tests/indexes.py b/tests/indexes.py new file mode 100644 index 0000000..83959f8 --- /dev/null +++ b/tests/indexes.py @@ -0,0 +1,7 @@ +from tortoise.indexes import Index + + +class CustomIndex(Index): + def __init__(self, *args, **kw) -> None: + super().__init__(*args, **kw) + self._foo = "" diff --git a/tests/models.py b/tests/models.py index 3126fbe..1153934 100644 --- a/tests/models.py +++ b/tests/models.py @@ -3,6 +3,9 @@ from enum import IntEnum from tortoise import Model, fields +from tortoise.indexes import Index + +from tests.indexes import CustomIndex class ProductType(IntEnum): @@ -33,6 +36,10 @@ class User(Model): products: fields.ManyToManyRelation["Product"] + class Meta: + # reverse indexes elements + indexes = [CustomIndex(fields=("is_superuser",)), Index(fields=("username", "is_active"))] + class Email(Model): email_id = fields.IntField(primary_key=True) diff --git a/tests/old_models.py b/tests/old_models.py index 5225597..8fbb47e 100644 --- a/tests/old_models.py +++ b/tests/old_models.py @@ -2,6 +2,9 @@ from enum import IntEnum from tortoise import Model, fields +from tortoise.indexes import Index + +from tests.indexes import CustomIndex class ProductType(IntEnum): @@ -31,6 +34,9 @@ class User(Model): intro = fields.TextField(default="") longitude = fields.DecimalField(max_digits=12, decimal_places=9) + class Meta: + indexes = [Index(fields=("username", "is_active")), CustomIndex(fields=("is_superuser",))] + class Email(Model): email = fields.CharField(max_length=200) diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 6cb1d33..87fb34c 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -3,6 +3,7 @@ import pytest import tortoise from pytest_mock import MockerFixture +from tortoise.indexes import Index from aerich.ddl.mysql import MysqlDDL from aerich.ddl.postgres import PostgresDDL @@ -10,6 +11,7 @@ from aerich.exceptions import NotSupportError from aerich.migrate import MIGRATE_TEMPLATE, Migrate from aerich.utils import get_models_describe +from tests.indexes import CustomIndex # tortoise-orm>=0.21 changes IntField constraints # from {"ge": 1, "le": 2147483647} to {"ge": -2147483648, "le": 2147483647} @@ -567,7 +569,7 @@ "description": None, "docstring": None, "unique_together": [], - "indexes": [], + "indexes": [Index(fields=("username", "is_active")), CustomIndex(fields=("is_superuser",))], "pk_field": { "name": "id", "field_type": "IntField",