Fix unique_together
This commit is contained in:
parent
abfa60133f
commit
09661c1d46
@ -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(
|
||||
|
@ -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,
|
||||
|
@ -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"'
|
||||
|
@ -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"',
|
||||
]
|
||||
|
Loading…
x
Reference in New Issue
Block a user