Skip to content

Commit

Permalink
Fix unique_together
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Feb 4, 2021
1 parent abfa601 commit 09661c1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
12 changes: 1 addition & 11 deletions aerich/ddl/postgres/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}'
Expand All @@ -34,15 +33,6 @@ def modify_column(self, model: "Type[Model]", field_describe: dict, is_pk: bool
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(
Expand Down
12 changes: 6 additions & 6 deletions aerich/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
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")
Expand Down Expand Up @@ -222,13 +222,13 @@ def diff_models(cls, old_models: Dict[str, dict], new_models: Dict[str, dict], u
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,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
]
Expand Down

0 comments on commit 09661c1

Please sign in to comment.