Merge remote-tracking branch 'upstream/dev' into type-hint-simple
This commit is contained in:
@@ -66,11 +66,11 @@ class BaseDDL:
|
||||
forward_type=db_field_types.get(self.DIALECT) or db_field_types.get(""),
|
||||
on_delete=field_describe.get("on_delete"),
|
||||
extra=self.schema_generator._table_generate_extra(table=through),
|
||||
comment=self.schema_generator._table_comment_generator(
|
||||
table=through, comment=description
|
||||
)
|
||||
if description
|
||||
else "",
|
||||
comment=(
|
||||
self.schema_generator._table_comment_generator(table=through, comment=description)
|
||||
if description
|
||||
else ""
|
||||
),
|
||||
)
|
||||
|
||||
def drop_m2m(self, table_name: str):
|
||||
@@ -121,13 +121,15 @@ class BaseDDL:
|
||||
field_type=db_field_types.get(self.DIALECT, db_field_types.get("")),
|
||||
nullable="NOT NULL" if not field_describe.get("nullable") else "",
|
||||
unique="UNIQUE" if field_describe.get("unique") else "",
|
||||
comment=self.schema_generator._column_comment_generator(
|
||||
table=db_table,
|
||||
column=db_column,
|
||||
comment=field_describe.get("description"),
|
||||
)
|
||||
if description
|
||||
else "",
|
||||
comment=(
|
||||
self.schema_generator._column_comment_generator(
|
||||
table=db_table,
|
||||
column=db_column,
|
||||
comment=field_describe.get("description"),
|
||||
)
|
||||
if description
|
||||
else ""
|
||||
),
|
||||
is_primary_key=is_pk,
|
||||
default=default,
|
||||
),
|
||||
@@ -151,13 +153,15 @@ class BaseDDL:
|
||||
field_type=db_field_types.get(self.DIALECT) or db_field_types.get(""),
|
||||
nullable="NOT NULL" if not field_describe.get("nullable") else "",
|
||||
unique="",
|
||||
comment=self.schema_generator._column_comment_generator(
|
||||
table=db_table,
|
||||
column=field_describe.get("db_column"),
|
||||
comment=field_describe.get("description"),
|
||||
)
|
||||
if field_describe.get("description")
|
||||
else "",
|
||||
comment=(
|
||||
self.schema_generator._column_comment_generator(
|
||||
table=db_table,
|
||||
column=field_describe.get("db_column"),
|
||||
comment=field_describe.get("description"),
|
||||
)
|
||||
if field_describe.get("description")
|
||||
else ""
|
||||
),
|
||||
is_primary_key=is_pk,
|
||||
default=default,
|
||||
),
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
from typing import TYPE_CHECKING, List, Type
|
||||
|
||||
from tortoise.backends.mysql.schema_generator import MySQLSchemaGenerator
|
||||
|
||||
from aerich.ddl import BaseDDL
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from tortoise import Model # noqa:F401
|
||||
|
||||
|
||||
class MysqlDDL(BaseDDL):
|
||||
schema_generator_cls = MySQLSchemaGenerator
|
||||
@@ -30,3 +35,29 @@ class MysqlDDL(BaseDDL):
|
||||
)
|
||||
_MODIFY_COLUMN_TEMPLATE = "ALTER TABLE `{table_name}` MODIFY COLUMN {column}"
|
||||
_RENAME_TABLE_TEMPLATE = "ALTER TABLE `{old_table_name}` RENAME TO `{new_table_name}`"
|
||||
|
||||
def _index_name(self, unique: bool, model: "Type[Model]", field_names: List[str]) -> str:
|
||||
if unique:
|
||||
if len(field_names) == 1:
|
||||
# Example: `email = CharField(max_length=50, unique=True)`
|
||||
# Generate schema: `"email" VARCHAR(10) NOT NULL UNIQUE`
|
||||
# Unique index key is the same as field name: `email`
|
||||
return field_names[0]
|
||||
index_prefix = "uid"
|
||||
else:
|
||||
index_prefix = "idx"
|
||||
return self.schema_generator._generate_index_name(index_prefix, model, field_names)
|
||||
|
||||
def add_index(self, model: "Type[Model]", field_names: List[str], unique=False) -> str:
|
||||
return self._ADD_INDEX_TEMPLATE.format(
|
||||
unique="UNIQUE " if unique else "",
|
||||
index_name=self._index_name(unique, model, field_names),
|
||||
table_name=model._meta.db_table,
|
||||
column_names=", ".join(self.schema_generator.quote(f) for f in field_names),
|
||||
)
|
||||
|
||||
def drop_index(self, model: "Type[Model]", field_names: List[str], unique=False) -> str:
|
||||
return self._DROP_INDEX_TEMPLATE.format(
|
||||
index_name=self._index_name(unique, model, field_names),
|
||||
table_name=model._meta.db_table,
|
||||
)
|
||||
|
||||
@@ -43,7 +43,9 @@ class PostgresDDL(BaseDDL):
|
||||
return self._SET_COMMENT_TEMPLATE.format(
|
||||
table_name=db_table,
|
||||
column=field_describe.get("db_column") or field_describe.get("raw_field"),
|
||||
comment="'{}'".format(field_describe.get("description"))
|
||||
if field_describe.get("description")
|
||||
else "NULL",
|
||||
comment=(
|
||||
"'{}'".format(field_describe.get("description"))
|
||||
if field_describe.get("description")
|
||||
else "NULL"
|
||||
),
|
||||
)
|
||||
|
||||
@@ -477,12 +477,13 @@ class Migrate:
|
||||
_, option, old_new = change
|
||||
if option == "indexed":
|
||||
# change index
|
||||
unique = new_data_field.get("unique")
|
||||
if old_new[0] is False and old_new[1] is True:
|
||||
unique = new_data_field.get("unique")
|
||||
cls._add_operator(
|
||||
cls._add_index(model, (field_name,), unique), upgrade, True
|
||||
)
|
||||
else:
|
||||
unique = old_data_field.get("unique")
|
||||
cls._add_operator(
|
||||
cls._drop_index(model, (field_name,), unique), upgrade, True
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user