Fix unique_together
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
				
			|||||||
from typing import List, Type
 | 
					from typing import Type
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from tortoise import Model
 | 
					from tortoise import Model
 | 
				
			||||||
from tortoise.backends.asyncpg.schema_generator import AsyncpgSchemaGenerator
 | 
					from tortoise.backends.asyncpg.schema_generator import AsyncpgSchemaGenerator
 | 
				
			||||||
@@ -11,7 +11,6 @@ class PostgresDDL(BaseDDL):
 | 
				
			|||||||
    DIALECT = AsyncpgSchemaGenerator.DIALECT
 | 
					    DIALECT = AsyncpgSchemaGenerator.DIALECT
 | 
				
			||||||
    _ADD_INDEX_TEMPLATE = 'CREATE {unique}INDEX "{index_name}" ON "{table_name}" ({column_names})'
 | 
					    _ADD_INDEX_TEMPLATE = 'CREATE {unique}INDEX "{index_name}" ON "{table_name}" ({column_names})'
 | 
				
			||||||
    _DROP_INDEX_TEMPLATE = 'DROP INDEX "{index_name}"'
 | 
					    _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'
 | 
					    _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}'
 | 
					    _MODIFY_COLUMN_TEMPLATE = 'ALTER TABLE "{table_name}" ALTER COLUMN "{column}" TYPE {datatype}'
 | 
				
			||||||
    _SET_COMMENT_TEMPLATE = 'COMMENT ON COLUMN "{table_name}"."{column}" IS {comment}'
 | 
					    _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(""),
 | 
					            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):
 | 
					    def set_comment(self, model: "Type[Model]", field_describe: dict):
 | 
				
			||||||
        db_table = model._meta.db_table
 | 
					        db_table = model._meta.db_table
 | 
				
			||||||
        return self._SET_COMMENT_TEMPLATE.format(
 | 
					        return self._SET_COMMENT_TEMPLATE.format(
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -173,11 +173,11 @@ class Migrate:
 | 
				
			|||||||
            else:
 | 
					            else:
 | 
				
			||||||
                old_model_describe = old_models.get(new_model_str)
 | 
					                old_model_describe = old_models.get(new_model_str)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                old_unique_together = map(
 | 
					                old_unique_together = set(
 | 
				
			||||||
                    lambda x: tuple(x), old_model_describe.get("unique_together")
 | 
					                    map(lambda x: tuple(x), old_model_describe.get("unique_together"))
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
                new_unique_together = map(
 | 
					                new_unique_together = set(
 | 
				
			||||||
                    lambda x: tuple(x), new_model_describe.get("unique_together")
 | 
					                    map(lambda x: tuple(x), new_model_describe.get("unique_together"))
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                old_pk_field = old_model_describe.get("pk_field")
 | 
					                old_pk_field = old_model_describe.get("pk_field")
 | 
				
			||||||
@@ -222,13 +222,13 @@ class Migrate:
 | 
				
			|||||||
                        if add:
 | 
					                        if add:
 | 
				
			||||||
                            cls._add_operator(cls.drop_m2m(table), upgrade, fk_m2m=True)
 | 
					                            cls._add_operator(cls.drop_m2m(table), upgrade, fk_m2m=True)
 | 
				
			||||||
                # add unique_together
 | 
					                # 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_operator(
 | 
				
			||||||
                        cls._add_index(model, index, True),
 | 
					                        cls._add_index(model, index, True),
 | 
				
			||||||
                        upgrade,
 | 
					                        upgrade,
 | 
				
			||||||
                    )
 | 
					                    )
 | 
				
			||||||
                # remove unique_together
 | 
					                # 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._add_operator(
 | 
				
			||||||
                        cls._drop_index(model, index, True),
 | 
					                        cls._drop_index(model, index, True),
 | 
				
			||||||
                        upgrade,
 | 
					                        upgrade,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -178,7 +178,7 @@ def test_drop_index():
 | 
				
			|||||||
        assert ret_u == "ALTER TABLE `category` DROP INDEX `uid_category_name_8b0cb9`"
 | 
					        assert ret_u == "ALTER TABLE `category` DROP INDEX `uid_category_name_8b0cb9`"
 | 
				
			||||||
    elif isinstance(Migrate.ddl, PostgresDDL):
 | 
					    elif isinstance(Migrate.ddl, PostgresDDL):
 | 
				
			||||||
        assert ret == 'DROP INDEX "idx_category_name_8b0cb9"'
 | 
					        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:
 | 
					    else:
 | 
				
			||||||
        assert ret == 'ALTER TABLE "category" DROP INDEX "idx_category_name_8b0cb9"'
 | 
					        assert ret == 'ALTER TABLE "category" DROP INDEX "idx_category_name_8b0cb9"'
 | 
				
			||||||
        assert ret_u == 'ALTER TABLE "category" DROP INDEX "uid_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" 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',
 | 
					                '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"',
 | 
					                '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 "product" ALTER COLUMN "view_num" DROP DEFAULT',
 | 
				
			||||||
                'ALTER TABLE "user" ADD "avatar" VARCHAR(200) NOT NULL  DEFAULT \'\'',
 | 
					                'ALTER TABLE "user" ADD "avatar" VARCHAR(200) NOT NULL  DEFAULT \'\'',
 | 
				
			||||||
                'DROP INDEX "idx_user_usernam_9987ab"',
 | 
					                'DROP INDEX "idx_user_usernam_9987ab"',
 | 
				
			||||||
 | 
					                'DROP INDEX "uid_product_name_f14935"',
 | 
				
			||||||
                'ALTER TABLE "user" CHANGE password password VARCHAR(200)',
 | 
					                'ALTER TABLE "user" CHANGE password password VARCHAR(200)',
 | 
				
			||||||
                'DROP TABLE IF EXISTS "email_user"',
 | 
					                'DROP TABLE IF EXISTS "email_user"',
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user