diff --git a/backend/plugin/code_generator/api/router.py b/backend/plugin/code_generator/api/router.py index 5f755adf3..bc336c1b6 100644 --- a/backend/plugin/code_generator/api/router.py +++ b/backend/plugin/code_generator/api/router.py @@ -3,9 +3,9 @@ from fastapi import APIRouter from backend.core.conf import settings +from backend.plugin.code_generator.api.v1.business import router as gen_business_router +from backend.plugin.code_generator.api.v1.column import router as gen_model_router from backend.plugin.code_generator.api.v1.gen import router as gen_router -from backend.plugin.code_generator.api.v1.gen_business import router as gen_business_router -from backend.plugin.code_generator.api.v1.gen_model import router as gen_model_router v1 = APIRouter(prefix=f'{settings.FASTAPI_API_V1_PATH}/gen', tags=['代码生成']) diff --git a/backend/plugin/code_generator/api/v1/gen_business.py b/backend/plugin/code_generator/api/v1/business.py similarity index 89% rename from backend/plugin/code_generator/api/v1/gen_business.py rename to backend/plugin/code_generator/api/v1/business.py index c364bc8f8..f95dbeec8 100644 --- a/backend/plugin/code_generator/api/v1/gen_business.py +++ b/backend/plugin/code_generator/api/v1/business.py @@ -8,14 +8,14 @@ from backend.common.security.jwt import DependsJwtAuth from backend.common.security.permission import RequestPermission from backend.common.security.rbac import DependsRBAC -from backend.plugin.code_generator.schema.gen_business import ( +from backend.plugin.code_generator.schema.business import ( CreateGenBusinessParam, GetGenBusinessDetail, UpdateGenBusinessParam, ) -from backend.plugin.code_generator.schema.gen_model import GetGenModelDetail -from backend.plugin.code_generator.service.gen_business_service import gen_business_service -from backend.plugin.code_generator.service.gen_model_service import gen_model_service +from backend.plugin.code_generator.schema.column import GetGenModelDetail +from backend.plugin.code_generator.service.business_service import gen_business_service +from backend.plugin.code_generator.service.column_service import gen_model_service router = APIRouter() diff --git a/backend/plugin/code_generator/api/v1/gen_model.py b/backend/plugin/code_generator/api/v1/column.py similarity index 91% rename from backend/plugin/code_generator/api/v1/gen_model.py rename to backend/plugin/code_generator/api/v1/column.py index f0e771f2d..8e75ea716 100644 --- a/backend/plugin/code_generator/api/v1/gen_model.py +++ b/backend/plugin/code_generator/api/v1/column.py @@ -8,8 +8,8 @@ from backend.common.security.jwt import DependsJwtAuth from backend.common.security.permission import RequestPermission from backend.common.security.rbac import DependsRBAC -from backend.plugin.code_generator.schema.gen_model import CreateGenModelParam, GetGenModelDetail, UpdateGenModelParam -from backend.plugin.code_generator.service.gen_model_service import gen_model_service +from backend.plugin.code_generator.schema.column import CreateGenModelParam, GetGenModelDetail, UpdateGenModelParam +from backend.plugin.code_generator.service.column_service import gen_model_service router = APIRouter() diff --git a/backend/plugin/code_generator/crud/crud_gen_business.py b/backend/plugin/code_generator/crud/crud_business.py similarity index 95% rename from backend/plugin/code_generator/crud/crud_gen_business.py rename to backend/plugin/code_generator/crud/crud_business.py index 99aab067d..ec55b2bc0 100644 --- a/backend/plugin/code_generator/crud/crud_gen_business.py +++ b/backend/plugin/code_generator/crud/crud_business.py @@ -6,7 +6,7 @@ from sqlalchemy_crud_plus import CRUDPlus from backend.plugin.code_generator.model import GenBusiness -from backend.plugin.code_generator.schema.gen_business import CreateGenBusinessParam, UpdateGenBusinessParam +from backend.plugin.code_generator.schema.business import CreateGenBusinessParam, UpdateGenBusinessParam class CRUDGenBusiness(CRUDPlus[GenBusiness]): diff --git a/backend/plugin/code_generator/crud/crud_gen_model.py b/backend/plugin/code_generator/crud/crud_column.py similarity index 83% rename from backend/plugin/code_generator/crud/crud_gen_model.py rename to backend/plugin/code_generator/crud/crud_column.py index b7b3bfa43..9c498d9ba 100644 --- a/backend/plugin/code_generator/crud/crud_gen_model.py +++ b/backend/plugin/code_generator/crud/crud_column.py @@ -5,14 +5,14 @@ from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy_crud_plus import CRUDPlus -from backend.plugin.code_generator.model import GenModel -from backend.plugin.code_generator.schema.gen_model import CreateGenModelParam, UpdateGenModelParam +from backend.plugin.code_generator.model import GenColumn +from backend.plugin.code_generator.schema.column import CreateGenModelParam, UpdateGenModelParam -class CRUDGenModel(CRUDPlus[GenModel]): +class CRUDGenModel(CRUDPlus[GenColumn]): """代码生成模型 CRUD 类""" - async def get(self, db: AsyncSession, pk: int) -> GenModel | None: + async def get(self, db: AsyncSession, pk: int) -> GenColumn | None: """ 获取代码生成模型列 @@ -22,7 +22,7 @@ async def get(self, db: AsyncSession, pk: int) -> GenModel | None: """ return await self.select_model(db, pk) - async def get_all_by_business(self, db: AsyncSession, business_id: int) -> Sequence[GenModel]: + async def get_all_by_business(self, db: AsyncSession, business_id: int) -> Sequence[GenColumn]: """ 获取所有代码生成模型列 @@ -66,4 +66,4 @@ async def delete(self, db: AsyncSession, pk: int) -> int: return await self.delete_model(db, pk) -gen_model_dao: CRUDGenModel = CRUDGenModel(GenModel) +gen_model_dao: CRUDGenModel = CRUDGenModel(GenColumn) diff --git a/backend/plugin/code_generator/model/__init__.py b/backend/plugin/code_generator/model/__init__.py index f27da1400..facaf1d78 100644 --- a/backend/plugin/code_generator/model/__init__.py +++ b/backend/plugin/code_generator/model/__init__.py @@ -1,4 +1,4 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from backend.plugin.code_generator.model.gen_business import GenBusiness -from backend.plugin.code_generator.model.gen_model import GenModel +from backend.plugin.code_generator.model.business import GenBusiness +from backend.plugin.code_generator.model.column import GenColumn diff --git a/backend/plugin/code_generator/model/gen_business.py b/backend/plugin/code_generator/model/business.py similarity index 83% rename from backend/plugin/code_generator/model/gen_business.py rename to backend/plugin/code_generator/model/business.py index 8e54a3ca4..88e4c4b93 100644 --- a/backend/plugin/code_generator/model/gen_business.py +++ b/backend/plugin/code_generator/model/business.py @@ -10,13 +10,13 @@ from backend.common.model import Base, id_key if TYPE_CHECKING: - from backend.plugin.code_generator.model import GenModel + from backend.plugin.code_generator.model import GenColumn class GenBusiness(Base): """代码生成业务表""" - __tablename__ = 'sys_gen_business' + __tablename__ = 'gen_business' id: Mapped[id_key] = mapped_column(init=False) app_name: Mapped[str] = mapped_column(String(50), comment='应用名称(英文)') @@ -26,6 +26,7 @@ class GenBusiness(Base): table_comment: Mapped[str | None] = mapped_column(String(255), default=None, comment='表描述') # relate_model_fk: Mapped[int | None] = mapped_column(default=None, comment='关联表外键') schema_name: Mapped[str | None] = mapped_column(String(255), default=None, comment='Schema 名称 (默认为英文表名称)') + filename: Mapped[str | None] = mapped_column(String(20), default=None, comment='基础文件名(默认为英文表名称)') default_datetime_column: Mapped[bool] = mapped_column(default=True, comment='是否存在默认时间列') api_version: Mapped[str] = mapped_column(String(20), default='v1', comment='代码生成 api 版本,默认为 v1') gen_path: Mapped[str | None] = mapped_column(String(255), default=None, comment='代码生成路径(默认为 app 根路径)') @@ -33,4 +34,4 @@ class GenBusiness(Base): LONGTEXT().with_variant(TEXT, 'postgresql'), default=None, comment='备注' ) # 代码生成业务模型一对多 - gen_model: Mapped[list['GenModel']] = relationship(init=False, back_populates='gen_business') + gen_column: Mapped[list['GenColumn']] = relationship(init=False, back_populates='gen_business') diff --git a/backend/plugin/code_generator/model/gen_model.py b/backend/plugin/code_generator/model/column.py similarity index 80% rename from backend/plugin/code_generator/model/gen_model.py rename to backend/plugin/code_generator/model/column.py index 8f17e2b0c..5bb9a424d 100644 --- a/backend/plugin/code_generator/model/gen_model.py +++ b/backend/plugin/code_generator/model/column.py @@ -13,15 +13,15 @@ from backend.plugin.code_generator.model import GenBusiness -class GenModel(DataClassBase): - """代码生成模型表""" +class GenColumn(DataClassBase): + """代码生成模型列表""" - __tablename__ = 'sys_gen_model' + __tablename__ = 'gen_column' id: Mapped[id_key] = mapped_column(init=False) name: Mapped[str] = mapped_column(String(50), comment='列名称') comment: Mapped[str | None] = mapped_column(String(255), default=None, comment='列描述') - type: Mapped[str] = mapped_column(String(20), default='str', comment='SQLA 模型列类型') + type: Mapped[str] = mapped_column(String(20), default='String', comment='SQLA 模型列类型') pd_type: Mapped[str] = mapped_column(String(20), default='str', comment='列类型对应的 pydantic 类型') default: Mapped[str | None] = mapped_column( LONGTEXT().with_variant(TEXT, 'postgresql'), default=None, comment='列默认值' @@ -33,6 +33,6 @@ class GenModel(DataClassBase): # 代码生成业务模型一对多 gen_business_id: Mapped[int] = mapped_column( - ForeignKey('sys_gen_business.id', ondelete='CASCADE'), default=0, comment='代码生成业务ID' + ForeignKey('gen_business.id', ondelete='CASCADE'), default=0, comment='代码生成业务ID' ) - gen_business: Mapped[Union['GenBusiness', None]] = relationship(init=False, back_populates='gen_model') + gen_business: Mapped[Union['GenBusiness', None]] = relationship(init=False, back_populates='gen_column') diff --git a/backend/plugin/code_generator/schema/gen_business.py b/backend/plugin/code_generator/schema/business.py similarity index 82% rename from backend/plugin/code_generator/schema/gen_business.py rename to backend/plugin/code_generator/schema/business.py index 53cc72472..2b3da6fe9 100644 --- a/backend/plugin/code_generator/schema/gen_business.py +++ b/backend/plugin/code_generator/schema/business.py @@ -2,8 +2,7 @@ # -*- coding: utf-8 -*- from datetime import datetime -from pydantic import ConfigDict, Field, model_validator -from typing_extensions import Self +from pydantic import ConfigDict, Field from backend.common.schema import SchemaBase @@ -17,18 +16,12 @@ class GenBusinessSchemaBase(SchemaBase): table_simple_name_zh: str = Field(description='表名称(中文简称)') table_comment: str | None = Field(None, description='表描述') schema_name: str | None = Field(None, description='Schema 名称 (默认为英文表名称)') + filename: str | None = Field(None, description='基础文件名(默认为英文表名称)') default_datetime_column: bool = Field(True, description='是否存在默认时间列') api_version: str = Field('v1', description='代码生成 api 版本') gen_path: str | None = Field(None, description='代码生成路径(默认为 app 根路径)') remark: str | None = Field(None, description='备注') - @model_validator(mode='after') - def check_schema_name(self) -> Self: - """检查并设置 schema 名称""" - if self.schema_name is None: - self.schema_name = self.table_name_en - return self - class CreateGenBusinessParam(GenBusinessSchemaBase): """创建代码生成业务参数""" diff --git a/backend/plugin/code_generator/schema/gen_model.py b/backend/plugin/code_generator/schema/column.py similarity index 100% rename from backend/plugin/code_generator/schema/gen_model.py rename to backend/plugin/code_generator/schema/column.py diff --git a/backend/plugin/code_generator/service/gen_business_service.py b/backend/plugin/code_generator/service/business_service.py similarity index 91% rename from backend/plugin/code_generator/service/gen_business_service.py rename to backend/plugin/code_generator/service/business_service.py index 23d277580..bd3a1f1a3 100644 --- a/backend/plugin/code_generator/service/gen_business_service.py +++ b/backend/plugin/code_generator/service/business_service.py @@ -4,9 +4,9 @@ from backend.common.exception import errors from backend.database.db import async_db_session -from backend.plugin.code_generator.crud.crud_gen_business import gen_business_dao +from backend.plugin.code_generator.crud.crud_business import gen_business_dao from backend.plugin.code_generator.model import GenBusiness -from backend.plugin.code_generator.schema.gen_business import CreateGenBusinessParam, UpdateGenBusinessParam +from backend.plugin.code_generator.schema.business import CreateGenBusinessParam, UpdateGenBusinessParam class GenBusinessService: diff --git a/backend/plugin/code_generator/service/gen_model_service.py b/backend/plugin/code_generator/service/column_service.py similarity index 91% rename from backend/plugin/code_generator/service/gen_model_service.py rename to backend/plugin/code_generator/service/column_service.py index aaa6ddbdb..781f4d782 100644 --- a/backend/plugin/code_generator/service/gen_model_service.py +++ b/backend/plugin/code_generator/service/column_service.py @@ -4,10 +4,10 @@ from backend.common.exception import errors from backend.database.db import async_db_session -from backend.plugin.code_generator.crud.crud_gen_model import gen_model_dao +from backend.plugin.code_generator.crud.crud_column import gen_model_dao from backend.plugin.code_generator.enums import GenModelMySQLColumnType -from backend.plugin.code_generator.model import GenModel -from backend.plugin.code_generator.schema.gen_model import CreateGenModelParam, UpdateGenModelParam +from backend.plugin.code_generator.model import GenColumn +from backend.plugin.code_generator.schema.column import CreateGenModelParam, UpdateGenModelParam from backend.plugin.code_generator.utils.type_conversion import sql_type_to_pydantic @@ -15,7 +15,7 @@ class GenModelService: """代码生成模型服务类""" @staticmethod - async def get(*, pk: int) -> GenModel: + async def get(*, pk: int) -> GenColumn: """ 获取指定 ID 的模型 @@ -36,7 +36,7 @@ async def get_types() -> list[str]: return types @staticmethod - async def get_by_business(*, business_id: int) -> Sequence[GenModel]: + async def get_by_business(*, business_id: int) -> Sequence[GenColumn]: """ 获取指定业务的所有模型 diff --git a/backend/plugin/code_generator/service/gen_service.py b/backend/plugin/code_generator/service/gen_service.py index 39da49b18..47fea0430 100644 --- a/backend/plugin/code_generator/service/gen_service.py +++ b/backend/plugin/code_generator/service/gen_service.py @@ -5,6 +5,7 @@ import zipfile from pathlib import Path +from typing import Sequence import aiofiles @@ -13,14 +14,14 @@ from backend.common.exception import errors from backend.core.path_conf import BASE_PATH from backend.database.db import async_db_session +from backend.plugin.code_generator.crud.crud_business import gen_business_dao +from backend.plugin.code_generator.crud.crud_column import gen_model_dao from backend.plugin.code_generator.crud.crud_gen import gen_dao -from backend.plugin.code_generator.crud.crud_gen_business import gen_business_dao -from backend.plugin.code_generator.crud.crud_gen_model import gen_model_dao from backend.plugin.code_generator.model import GenBusiness +from backend.plugin.code_generator.schema.business import CreateGenBusinessParam +from backend.plugin.code_generator.schema.column import CreateGenModelParam from backend.plugin.code_generator.schema.gen import ImportParam -from backend.plugin.code_generator.schema.gen_business import CreateGenBusinessParam -from backend.plugin.code_generator.schema.gen_model import CreateGenModelParam -from backend.plugin.code_generator.service.gen_model_service import gen_model_service +from backend.plugin.code_generator.service.column_service import gen_model_service from backend.plugin.code_generator.utils.gen_template import gen_template from backend.plugin.code_generator.utils.type_conversion import sql_type_to_pydantic @@ -29,7 +30,7 @@ class GenService: """代码生成服务类""" @staticmethod - async def get_tables(*, table_schema: str) -> list[str]: + async def get_tables(*, table_schema: str) -> Sequence[str]: """ 获取指定 schema 下的所有表名 @@ -57,14 +58,17 @@ async def import_business_and_model(*, obj: ImportParam) -> None: raise errors.ForbiddenError(msg='已存在相同数据库表业务') table_name = table_info[0] - business_data = { - 'app_name': obj.app, - 'table_name_en': table_name, - 'table_name_zh': table_info[1] or ' '.join(table_name.split('_')), - 'table_simple_name_zh': table_info[1] or table_name.split('_')[-1], - 'table_comment': table_info[1], - } - new_business = GenBusiness(**CreateGenBusinessParam(**business_data).model_dump()) + new_business = GenBusiness( + **CreateGenBusinessParam( + app_name=obj.app, + table_name_en=table_name, + table_name_zh=table_info[1] or ' '.join(table_name.split('_')), + table_simple_name_zh=table_info[1] or table_name.split('_')[-1], + table_comment=table_info[1], + schema_name=table_name, + filename=table_name, + ).model_dump() + ) db.add(new_business) await db.flush() @@ -72,17 +76,20 @@ async def import_business_and_model(*, obj: ImportParam) -> None: for column in column_info: column_type = column[-1].split('(')[0].upper() pd_type = sql_type_to_pydantic(column_type) - model_data = { - 'name': column[0], - 'comment': column[-2], - 'type': column_type, - 'sort': column[-3], - 'length': column[-1].split('(')[1][:-1] if pd_type == 'str' and '(' in column[-1] else 0, - 'is_pk': column[1], - 'is_nullable': column[2], - 'gen_business_id': new_business.id, - } - await gen_model_dao.create(db, CreateGenModelParam(**model_data), pd_type=pd_type) + await gen_model_dao.create( + db, + CreateGenModelParam( + name=column[0], + comment=column[-2], + type=column_type, + sort=column[-3], + length=column[-1].split('(')[1][:-1] if pd_type == 'str' and '(' in column[-1] else 0, + is_pk=column[1], + is_nullable=column[2], + gen_business_id=new_business.id, + ), + pd_type=pd_type, + ) @staticmethod async def render_tpl_code(*, business: GenBusiness) -> dict[str, str]: diff --git a/backend/plugin/code_generator/utils/gen_template.py b/backend/plugin/code_generator/utils/gen_template.py index 1d5e3f958..45c28f21f 100644 --- a/backend/plugin/code_generator/utils/gen_template.py +++ b/backend/plugin/code_generator/utils/gen_template.py @@ -6,7 +6,7 @@ from pydantic.alias_generators import to_pascal, to_snake from backend.core.conf import settings -from backend.plugin.code_generator.model import GenBusiness, GenModel +from backend.plugin.code_generator.model import GenBusiness, GenColumn from backend.plugin.code_generator.path_conf import JINJA2_TEMPLATE_DIR @@ -56,13 +56,13 @@ def get_code_gen_paths(business: GenBusiness) -> list[str]: :return: """ app_name = business.app_name - module_name = business.table_name_en + filename = business.filename return [ - f'{app_name}/api/{business.api_version}/{module_name}.py', - f'{app_name}/crud/crud_{module_name}.py', - f'{app_name}/model/{module_name}.py', - f'{app_name}/schema/{module_name}.py', - f'{app_name}/service/{module_name}_service.py', + f'{app_name}/api/{business.api_version}/{filename}.py', + f'{app_name}/crud/crud_{filename}.py', + f'{app_name}/model/{filename}.py', + f'{app_name}/schema/{filename}.py', + f'{app_name}/service/{filename}_service.py', ] def get_code_gen_path(self, tpl_path: str, business: GenBusiness) -> str: @@ -78,7 +78,7 @@ def get_code_gen_path(self, tpl_path: str, business: GenBusiness) -> str: return code_gen_path_mapping[tpl_path] @staticmethod - def get_vars(business: GenBusiness, models: Sequence[GenModel]) -> dict[str, str | Sequence[GenModel]]: + def get_vars(business: GenBusiness, models: Sequence[GenColumn]) -> dict[str, str | Sequence[GenColumn]]: """ 获取模板变量 diff --git a/backend/sql/mysql/create_tables.sql b/backend/sql/mysql/create_tables.sql index 112497b8c..06adb974d 100644 --- a/backend/sql/mysql/create_tables.sql +++ b/backend/sql/mysql/create_tables.sql @@ -1,3 +1,54 @@ +create table gen_business +( + id int auto_increment comment '主键 ID' + primary key, + app_name varchar(50) not null comment '应用名称(英文)', + table_name_en varchar(255) not null comment '表名称(英文)', + table_name_zh varchar(255) not null comment '表名称(中文)', + table_simple_name_zh varchar(255) not null comment '表名称(中文简称)', + table_comment varchar(255) null comment '表描述', + schema_name varchar(255) null comment 'Schema 名称 (默认为英文表名称)', + filename varchar(20) null comment '基础文件名(默认为英文表名称)', + default_datetime_column tinyint(1) not null comment '是否存在默认时间列', + api_version varchar(20) not null comment '代码生成 api 版本,默认为 v1', + gen_path varchar(255) null comment '代码生成路径(默认为 app 根路径)', + remark longtext null comment '备注', + created_time datetime not null comment '创建时间', + updated_time datetime null comment '更新时间', + constraint table_name_en + unique (table_name_en) +) + comment '代码生成业务表'; + +create index ix_gen_business_id + on gen_business (id); + +create table gen_column +( + id int auto_increment comment '主键 ID' + primary key, + name varchar(50) not null comment '列名称', + comment varchar(255) null comment '列描述', + type varchar(20) not null comment 'SQLA 模型列类型', + pd_type varchar(20) not null comment '列类型对应的 pydantic 类型', + `default` longtext null comment '列默认值', + sort int null comment '列排序', + length int not null comment '列长度', + is_pk tinyint(1) not null comment '是否主键', + is_nullable tinyint(1) not null comment '是否可为空', + gen_business_id int not null comment '代码生成业务ID', + constraint gen_column_ibfk_1 + foreign key (gen_business_id) references gen_business (id) + on delete cascade +) + comment '代码生成模型列表'; + +create index gen_business_id + on gen_column (gen_business_id); + +create index ix_gen_column_id + on gen_column (id); + create table sys_api ( id int auto_increment comment '主键 ID' @@ -110,9 +161,7 @@ create table sys_dict_type created_time datetime not null comment '创建时间', updated_time datetime null comment '更新时间', constraint code - unique (code), - constraint name - unique (name) + unique (code) ) comment '字典类型表'; @@ -130,8 +179,6 @@ create table sys_dict_data updated_time datetime null comment '更新时间', constraint label unique (label), - constraint value - unique (value), constraint sys_dict_data_ibfk_1 foreign key (type_id) references sys_dict_type (id) on delete cascade @@ -147,56 +194,6 @@ create index type_id create index ix_sys_dict_type_id on sys_dict_type (id); -create table sys_gen_business -( - id int auto_increment comment '主键 ID' - primary key, - app_name varchar(50) not null comment '应用名称(英文)', - table_name_en varchar(255) not null comment '表名称(英文)', - table_name_zh varchar(255) not null comment '表名称(中文)', - table_simple_name_zh varchar(255) not null comment '表名称(中文简称)', - table_comment varchar(255) null comment '表描述', - schema_name varchar(255) null comment 'Schema 名称 (默认为英文表名称)', - default_datetime_column tinyint(1) not null comment '是否存在默认时间列', - api_version varchar(20) not null comment '代码生成 api 版本,默认为 v1', - gen_path varchar(255) null comment '代码生成路径(默认为 app 根路径)', - remark longtext null comment '备注', - created_time datetime not null comment '创建时间', - updated_time datetime null comment '更新时间', - constraint table_name_en - unique (table_name_en) -) - comment '代码生成业务表'; - -create index ix_sys_gen_business_id - on sys_gen_business (id); - -create table sys_gen_model -( - id int auto_increment comment '主键 ID' - primary key, - name varchar(50) not null comment '列名称', - comment varchar(255) null comment '列描述', - type varchar(20) not null comment 'SQLA 模型列类型', - pd_type varchar(20) not null comment '列类型对应的 pydantic 类型', - `default` longtext null comment '列默认值', - sort int null comment '列排序', - length int not null comment '列长度', - is_pk tinyint(1) not null comment '是否主键', - is_nullable tinyint(1) not null comment '是否可为空', - gen_business_id int not null comment '代码生成业务ID', - constraint sys_gen_model_ibfk_1 - foreign key (gen_business_id) references sys_gen_business (id) - on delete cascade -) - comment '代码生成模型表'; - -create index gen_business_id - on sys_gen_model (gen_business_id); - -create index ix_sys_gen_model_id - on sys_gen_model (id); - create table sys_login_log ( id int auto_increment comment '主键 ID' diff --git a/backend/sql/postgresql/create_tables.sql b/backend/sql/postgresql/create_tables.sql index e469a2f13..b73c593e2 100644 --- a/backend/sql/postgresql/create_tables.sql +++ b/backend/sql/postgresql/create_tables.sql @@ -397,6 +397,62 @@ alter table sys_casbin_rule create index ix_sys_casbin_rule_id on sys_casbin_rule (id); +create table gen_business +( + id serial + primary key, + app_name varchar(50) not null, + table_name_en varchar(255) not null + unique, + table_name_zh varchar(255) not null, + table_simple_name_zh varchar(255) not null, + table_comment varchar(255), + schema_name varchar(255), + filename varchar(20), + default_datetime_column boolean not null, + api_version varchar(20) not null, + gen_path varchar(255), + remark text, + created_time timestamp with time zone not null, + updated_time timestamp with time zone +); + +comment on table gen_business is '代码生成业务表'; + +comment on column gen_business.id is '主键 ID'; + +comment on column gen_business.app_name is '应用名称(英文)'; + +comment on column gen_business.table_name_en is '表名称(英文)'; + +comment on column gen_business.table_name_zh is '表名称(中文)'; + +comment on column gen_business.table_simple_name_zh is '表名称(中文简称)'; + +comment on column gen_business.table_comment is '表描述'; + +comment on column gen_business.schema_name is 'Schema 名称 (默认为英文表名称)'; + +comment on column gen_business.filename is '基础文件名(默认为英文表名称)'; + +comment on column gen_business.default_datetime_column is '是否存在默认时间列'; + +comment on column gen_business.api_version is '代码生成 api 版本,默认为 v1'; + +comment on column gen_business.gen_path is '代码生成路径(默认为 app 根路径)'; + +comment on column gen_business.remark is '备注'; + +comment on column gen_business.created_time is '创建时间'; + +comment on column gen_business.updated_time is '更新时间'; + +alter table gen_business + owner to postgres; + +create index ix_gen_business_id + on gen_business (id); + create table sys_config ( id serial @@ -438,52 +494,11 @@ alter table sys_config create index ix_sys_config_id on sys_config (id); -create table sys_notice -( - id serial - primary key, - title varchar(50) not null, - type integer not null, - author varchar(16) not null, - source varchar(50) not null, - status integer not null, - content text not null, - created_time timestamp with time zone not null, - updated_time timestamp with time zone -); - -comment on table sys_notice is '系统通知公告表'; - -comment on column sys_notice.id is '主键 ID'; - -comment on column sys_notice.title is '标题'; - -comment on column sys_notice.type is '类型(0:通知、1:公告)'; - -comment on column sys_notice.author is '作者'; - -comment on column sys_notice.source is '信息来源'; - -comment on column sys_notice.status is '状态(0:隐藏、1:显示)'; - -comment on column sys_notice.content is '内容'; - -comment on column sys_notice.created_time is '创建时间'; - -comment on column sys_notice.updated_time is '更新时间'; - -alter table sys_notice - owner to postgres; - -create index ix_sys_notice_id - on sys_notice (id); - create table sys_dict_type ( id serial primary key, - name varchar(32) not null - unique, + name varchar(32) not null, code varchar(32) not null unique, status integer not null, @@ -514,58 +529,45 @@ alter table sys_dict_type create index ix_sys_dict_type_id on sys_dict_type (id); -create table sys_gen_business +create table sys_notice ( - id serial + id serial primary key, - app_name varchar(50) not null, - table_name_en varchar(255) not null - unique, - table_name_zh varchar(255) not null, - table_simple_name_zh varchar(255) not null, - table_comment varchar(255), - schema_name varchar(255), - default_datetime_column boolean not null, - api_version varchar(20) not null, - gen_path varchar(255), - remark text, - created_time timestamp with time zone not null, - updated_time timestamp with time zone + title varchar(50) not null, + type integer not null, + author varchar(16) not null, + source varchar(50) not null, + status integer not null, + content text not null, + created_time timestamp with time zone not null, + updated_time timestamp with time zone ); -comment on table sys_gen_business is '代码生成业务表'; - -comment on column sys_gen_business.id is '主键 ID'; - -comment on column sys_gen_business.app_name is '应用名称(英文)'; - -comment on column sys_gen_business.table_name_en is '表名称(英文)'; - -comment on column sys_gen_business.table_name_zh is '表名称(中文)'; +comment on table sys_notice is '系统通知公告表'; -comment on column sys_gen_business.table_simple_name_zh is '表名称(中文简称)'; +comment on column sys_notice.id is '主键 ID'; -comment on column sys_gen_business.table_comment is '表描述'; +comment on column sys_notice.title is '标题'; -comment on column sys_gen_business.schema_name is 'Schema 名称 (默认为英文表名称)'; +comment on column sys_notice.type is '类型(0:通知、1:公告)'; -comment on column sys_gen_business.default_datetime_column is '是否存在默认时间列'; +comment on column sys_notice.author is '作者'; -comment on column sys_gen_business.api_version is '代码生成 api 版本,默认为 v1'; +comment on column sys_notice.source is '信息来源'; -comment on column sys_gen_business.gen_path is '代码生成路径(默认为 app 根路径)'; +comment on column sys_notice.status is '状态(0:隐藏、1:显示)'; -comment on column sys_gen_business.remark is '备注'; +comment on column sys_notice.content is '内容'; -comment on column sys_gen_business.created_time is '创建时间'; +comment on column sys_notice.created_time is '创建时间'; -comment on column sys_gen_business.updated_time is '更新时间'; +comment on column sys_notice.updated_time is '更新时间'; -alter table sys_gen_business +alter table sys_notice owner to postgres; -create index ix_sys_gen_business_id - on sys_gen_business (id); +create index ix_sys_notice_id + on sys_notice (id); create table sys_role_menu ( @@ -681,17 +683,65 @@ comment on column sys_user.updated_time is '更新时间'; alter table sys_user owner to postgres; +create index ix_sys_user_id + on sys_user (id); + +create unique index ix_sys_user_email + on sys_user (email); + create unique index ix_sys_user_username on sys_user (username); create index ix_sys_user_status on sys_user (status); -create index ix_sys_user_id - on sys_user (id); +create table gen_column +( + id serial + primary key, + name varchar(50) not null, + comment varchar(255), + type varchar(20) not null, + pd_type varchar(20) not null, + "default" text, + sort integer, + length integer not null, + is_pk boolean not null, + is_nullable boolean not null, + gen_business_id integer not null + references gen_business + on delete cascade +); -create unique index ix_sys_user_email - on sys_user (email); +comment on table gen_column is '代码生成模型列表'; + +comment on column gen_column.id is '主键 ID'; + +comment on column gen_column.name is '列名称'; + +comment on column gen_column.comment is '列描述'; + +comment on column gen_column.type is 'SQLA 模型列类型'; + +comment on column gen_column.pd_type is '列类型对应的 pydantic 类型'; + +comment on column gen_column."default" is '列默认值'; + +comment on column gen_column.sort is '列排序'; + +comment on column gen_column.length is '列长度'; + +comment on column gen_column.is_pk is '是否主键'; + +comment on column gen_column.is_nullable is '是否可为空'; + +comment on column gen_column.gen_business_id is '代码生成业务ID'; + +alter table gen_column + owner to postgres; + +create index ix_gen_column_id + on gen_column (id); create table sys_dict_data ( @@ -699,8 +749,7 @@ create table sys_dict_data primary key, label varchar(32) not null unique, - value varchar(32) not null - unique, + value varchar(32) not null, sort integer not null, status integer not null, remark text, @@ -737,54 +786,6 @@ alter table sys_dict_data create index ix_sys_dict_data_id on sys_dict_data (id); -create table sys_gen_model -( - id serial - primary key, - name varchar(50) not null, - comment varchar(255), - type varchar(20) not null, - pd_type varchar(20) not null, - "default" text, - sort integer, - length integer not null, - is_pk boolean not null, - is_nullable boolean not null, - gen_business_id integer not null - references sys_gen_business - on delete cascade -); - -comment on table sys_gen_model is '代码生成模型表'; - -comment on column sys_gen_model.id is '主键 ID'; - -comment on column sys_gen_model.name is '列名称'; - -comment on column sys_gen_model.comment is '列描述'; - -comment on column sys_gen_model.type is 'SQLA 模型列类型'; - -comment on column sys_gen_model.pd_type is '列类型对应的 pydantic 类型'; - -comment on column sys_gen_model."default" is '列默认值'; - -comment on column sys_gen_model.sort is '列排序'; - -comment on column sys_gen_model.length is '列长度'; - -comment on column sys_gen_model.is_pk is '是否主键'; - -comment on column sys_gen_model.is_nullable is '是否可为空'; - -comment on column sys_gen_model.gen_business_id is '代码生成业务ID'; - -alter table sys_gen_model - owner to postgres; - -create index ix_sys_gen_model_id - on sys_gen_model (id); - create table sys_user_role ( id serial,