add support to m2m

change cli options
add init-db cmd
This commit is contained in:
long2ice
2020-05-16 00:36:39 +08:00
parent 23cbd12570
commit b2115345c0
9 changed files with 241 additions and 105 deletions

View File

@@ -1,6 +1,6 @@
from typing import List, Type
from tortoise import BaseDBAsyncClient, ForeignKeyFieldInstance, Model
from tortoise import BaseDBAsyncClient, ForeignKeyFieldInstance, ManyToManyFieldInstance, Model
from tortoise.backends.base.schema_generator import BaseSchemaGenerator
from tortoise.fields import Field
@@ -8,7 +8,7 @@ from tortoise.fields import Field
class BaseDDL:
schema_generator_cls: Type[BaseSchemaGenerator] = BaseSchemaGenerator
DIALECT = "sql"
_DROP_TABLE_TEMPLATE = "DROP TABLE {table_name} IF EXISTS"
_DROP_TABLE_TEMPLATE = "DROP TABLE IF EXISTS {table_name}"
_ADD_COLUMN_TEMPLATE = "ALTER TABLE {table_name} ADD {column}"
_DROP_COLUMN_TEMPLATE = "ALTER TABLE {table_name} DROP COLUMN {column_name}"
_ADD_INDEX_TEMPLATE = (
@@ -17,6 +17,7 @@ class BaseDDL:
_DROP_INDEX_TEMPLATE = "ALTER TABLE {table_name} DROP INDEX {index_name}"
_ADD_FK_TEMPLATE = "ALTER TABLE {table_name} ADD CONSTRAINT `{fk_name}` FOREIGN KEY (`{db_column}`) REFERENCES `{table}` (`{field}`) ON DELETE {on_delete}"
_DROP_FK_TEMPLATE = "ALTER TABLE {table_name} DROP FOREIGN KEY {fk_name}"
_M2M_TABLE_TEMPLATE = "CREATE TABLE {table_name} ({backward_key} {backward_type} NOT NULL REFERENCES {backward_table} ({backward_field}) ON DELETE CASCADE,{forward_key} {forward_type} NOT NULL REFERENCES {forward_table} ({forward_field}) ON DELETE CASCADE){extra}{comment};"
def __init__(self, client: "BaseDBAsyncClient"):
self.client = client
@@ -25,6 +26,12 @@ class BaseDDL:
def create_table(self, model: "Type[Model]"):
raise NotImplementedError
def create_m2m_table(self, model: "Type[Model]", field: ManyToManyFieldInstance):
raise NotImplementedError
def drop_m2m(self, field: ManyToManyFieldInstance):
raise NotImplementedError
def drop_table(self, model: "Type[Model]"):
raise NotImplementedError

View File

@@ -1,6 +1,6 @@
from typing import List, Type
from tortoise import ForeignKeyFieldInstance, Model
from tortoise import ForeignKeyFieldInstance, ManyToManyFieldInstance, Model
from tortoise.backends.mysql.schema_generator import MySQLSchemaGenerator
from tortoise.fields import Field, JSONField, TextField, UUIDField
@@ -17,6 +17,28 @@ class MysqlDDL(BaseDDL):
def drop_table(self, model: "Type[Model]"):
return self._DROP_TABLE_TEMPLATE.format(table_name=model._meta.db_table)
def create_m2m_table(self, model: "Type[Model]", field: ManyToManyFieldInstance):
return self._M2M_TABLE_TEMPLATE.format(
table_name=field.through,
backward_table=model._meta.db_table,
forward_table=field.related_model._meta.db_table,
backward_field=model._meta.db_pk_column,
forward_field=field.related_model._meta.db_pk_column,
backward_key=field.backward_key,
backward_type=model._meta.pk.get_for_dialect(self.DIALECT, "SQL_TYPE"),
forward_key=field.forward_key,
forward_type=field.related_model._meta.pk.get_for_dialect(self.DIALECT, "SQL_TYPE"),
extra=self.schema_generator._table_generate_extra(table=field.through),
comment=self.schema_generator._table_comment_generator(
table=field.through, comment=field.description
)
if field.description
else "",
)
def drop_m2m(self, field: ManyToManyFieldInstance):
return self._DROP_TABLE_TEMPLATE.format(table_name=field.through)
def add_column(self, model: "Type[Model]", field_object: Field):
db_table = model._meta.db_table
default = field_object.default