mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Add postgresql database support (#475)
* feat: add postgresql db supports * change: change mysql conn str create way * fix: Modify the default alembic migration file to meet multi-database support * Update settings and lint * update models * Simplify database config * Simplify the get db method * Update create db url * Updated model type adaptation * Update sql scripts * Fix models type * Adaptation to postgresql code generation * Update README.md * Fix alembic file template * Update docker scripts
This commit is contained in:
@@ -7,6 +7,7 @@ from pydantic.alias_generators import to_pascal, to_snake
|
||||
|
||||
from backend.app.generator.conf import generator_settings
|
||||
from backend.app.generator.model import GenBusiness, GenModel
|
||||
from backend.core.conf import settings
|
||||
from backend.core.path_conf import JINJA2_TEMPLATE_DIR
|
||||
|
||||
|
||||
@@ -95,8 +96,9 @@ class GenTemplate:
|
||||
'table_simple_name_zh': business.table_simple_name_zh,
|
||||
'table_comment': business.table_comment,
|
||||
'schema_name': to_pascal(business.schema_name),
|
||||
'have_datetime_column': business.default_datetime_column,
|
||||
'permission_sign': str(business.table_name_en.replace('_', ':')),
|
||||
'default_datetime_column': business.default_datetime_column,
|
||||
'permission': str(business.table_name_en.replace('_', ':')),
|
||||
'database_type': settings.DATABASE_TYPE,
|
||||
'models': models,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from backend.database.db_redis import redis_client
|
||||
from backend.database.redis import redis_client
|
||||
from backend.utils.server_info import server_info
|
||||
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from backend.common.dataclasses import IpInfo, UserAgentInfo
|
||||
from backend.common.log import log
|
||||
from backend.core.conf import settings
|
||||
from backend.core.path_conf import IP2REGION_XDB
|
||||
from backend.database.db_redis import redis_client
|
||||
from backend.database.redis import redis_client
|
||||
|
||||
|
||||
def get_request_ip(request: Request) -> str:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from backend.common.enums import GenModelMySQLColumnType
|
||||
from backend.common.enums import GenModelMySQLColumnType, GenModelPostgreSQLColumnType
|
||||
from backend.core.conf import settings
|
||||
|
||||
|
||||
def sql_type_to_sqlalchemy(typing: str) -> str:
|
||||
@@ -10,8 +11,12 @@ def sql_type_to_sqlalchemy(typing: str) -> str:
|
||||
:param typing:
|
||||
:return:
|
||||
"""
|
||||
if typing in GenModelMySQLColumnType.get_member_keys():
|
||||
return typing
|
||||
if settings.DATABASE_TYPE == 'mysql':
|
||||
if typing in GenModelMySQLColumnType.get_member_keys():
|
||||
return typing
|
||||
else:
|
||||
if typing in GenModelPostgreSQLColumnType.get_member_keys():
|
||||
return typing
|
||||
return 'String'
|
||||
|
||||
|
||||
@@ -23,6 +28,11 @@ def sql_type_to_pydantic(typing: str) -> str:
|
||||
:return:
|
||||
"""
|
||||
try:
|
||||
return GenModelMySQLColumnType[typing].value
|
||||
if settings.DATABASE_TYPE == 'mysql':
|
||||
return GenModelMySQLColumnType[typing].value
|
||||
else:
|
||||
if typing == 'CHARACTER VARYING': # postgresql 中 DDL VARCHAR 的别名
|
||||
return 'str'
|
||||
return GenModelPostgreSQLColumnType[typing].value
|
||||
except KeyError:
|
||||
return 'str'
|
||||
|
||||
Reference in New Issue
Block a user