From 09661c1d46cc108c53376ac5e4e44469163632fb Mon Sep 17 00:00:00 2001 From: long2ice Date: Thu, 4 Feb 2021 14:39:07 +0800 Subject: [PATCH] Fix unique_together --- aerich/ddl/postgres/__init__.py | 12 +----------- aerich/migrate.py | 12 ++++++------ tests/test_ddl.py | 2 +- tests/test_migrate.py | 2 +- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/aerich/ddl/postgres/__init__.py b/aerich/ddl/postgres/__init__.py index 23123c5..415489d 100644 --- a/aerich/ddl/postgres/__init__.py +++ b/aerich/ddl/postgres/__init__.py @@ -1,4 +1,4 @@ -from typing import List, Type +from typing import Type from tortoise import Model from tortoise.backends.asyncpg.schema_generator import AsyncpgSchemaGenerator @@ -11,7 +11,6 @@ class PostgresDDL(BaseDDL): DIALECT = AsyncpgSchemaGenerator.DIALECT _ADD_INDEX_TEMPLATE = 'CREATE {unique}INDEX "{index_name}" ON "{table_name}" ({column_names})' _DROP_INDEX_TEMPLATE = 'DROP INDEX "{index_name}"' - _DROP_UNIQUE_TEMPLATE = 'ALTER TABLE "{table_name}" DROP CONSTRAINT "{index_name}"' _ALTER_NULL_TEMPLATE = 'ALTER TABLE "{table_name}" ALTER COLUMN "{column}" {set_drop} NOT NULL' _MODIFY_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" ALTER COLUMN "{column}" TYPE {datatype}' _SET_COMMENT_TEMPLATE = 'COMMENT ON COLUMN "{table_name}"."{column}" IS {comment}' @@ -34,15 +33,6 @@ class PostgresDDL(BaseDDL): datatype=db_field_types.get(self.DIALECT) or db_field_types.get(""), ) - def drop_index(self, model: "Type[Model]", field_names: List[str], unique=False): - template = self._DROP_UNIQUE_TEMPLATE if unique else self._DROP_INDEX_TEMPLATE - return template.format( - index_name=self.schema_generator._generate_index_name( - "uid" if unique else "idx", model, field_names - ), - table_name=model._meta.db_table, - ) - def set_comment(self, model: "Type[Model]", field_describe: dict): db_table = model._meta.db_table return self._SET_COMMENT_TEMPLATE.format( diff --git a/aerich/migrate.py b/aerich/migrate.py index 2532105..5bb1c2b 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -173,11 +173,11 @@ class Migrate: else: old_model_describe = old_models.get(new_model_str) - old_unique_together = map( - lambda x: tuple(x), old_model_describe.get("unique_together") + old_unique_together = set( + map(lambda x: tuple(x), old_model_describe.get("unique_together")) ) - new_unique_together = map( - lambda x: tuple(x), new_model_describe.get("unique_together") + new_unique_together = set( + map(lambda x: tuple(x), new_model_describe.get("unique_together")) ) old_pk_field = old_model_describe.get("pk_field") @@ -222,13 +222,13 @@ class Migrate: if add: cls._add_operator(cls.drop_m2m(table), upgrade, fk_m2m=True) # add unique_together - for index in set(new_unique_together).difference(set(old_unique_together)): + for index in new_unique_together.difference(old_unique_together): cls._add_operator( cls._add_index(model, index, True), upgrade, ) # remove unique_together - for index in set(old_unique_together).difference(set(new_unique_together)): + for index in old_unique_together.difference(new_unique_together): cls._add_operator( cls._drop_index(model, index, True), upgrade, diff --git a/tests/test_ddl.py b/tests/test_ddl.py index 9a51289..cd3a3f4 100644 --- a/tests/test_ddl.py +++ b/tests/test_ddl.py @@ -178,7 +178,7 @@ def test_drop_index(): assert ret_u == "ALTER TABLE `category` DROP INDEX `uid_category_name_8b0cb9`" elif isinstance(Migrate.ddl, PostgresDDL): assert ret == 'DROP INDEX "idx_category_name_8b0cb9"' - assert ret_u == 'ALTER TABLE "category" DROP CONSTRAINT "uid_category_name_8b0cb9"' + assert ret_u == 'DROP INDEX "uid_category_name_8b0cb9"' else: assert ret == 'ALTER TABLE "category" DROP INDEX "idx_category_name_8b0cb9"' assert ret_u == 'ALTER TABLE "category" DROP INDEX "uid_category_name_8b0cb9"' diff --git a/tests/test_migrate.py b/tests/test_migrate.py index 2ada0aa..9aeed6e 100644 --- a/tests/test_migrate.py +++ b/tests/test_migrate.py @@ -852,10 +852,10 @@ def test_migrate(mocker: MockerFixture): 'ALTER TABLE "email" RENAME COLUMN "email_id" TO "id"', 'ALTER TABLE "email" ADD CONSTRAINT "fk_email_user_5b58673d" FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE', 'DROP INDEX "idx_email_email_4a1a33"', - 'ALTER TABLE "product" DROP CONSTRAINT "uid_product_name_f14935"', 'ALTER TABLE "product" ALTER COLUMN "view_num" DROP DEFAULT', 'ALTER TABLE "user" ADD "avatar" VARCHAR(200) NOT NULL DEFAULT \'\'', 'DROP INDEX "idx_user_usernam_9987ab"', + 'DROP INDEX "uid_product_name_f14935"', 'ALTER TABLE "user" CHANGE password password VARCHAR(200)', 'DROP TABLE IF EXISTS "email_user"', ]