Skip to content

Commit 78dad30

Browse files
authored
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
1 parent 4f59f45 commit 78dad30

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1643
-748
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ versions
1313
**🔥Continuously updated and maintained🔥**
1414

1515
[![GitHub](https://img.shields.io/github/license/fastapi-practices/fastapi_best_architecture)](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE)
16-
[![Static Badge](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
17-
![Static Badge](https://img.shields.io/badge/MySQL-8.0%2B-%2300758f)
18-
![Static Badge](https://img.shields.io/badge/SQLAlchemy-2.0-%23778877)
16+
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
17+
![MySQL](https://img.shields.io/badge/MySQL-8.0%2B-%2300758f)
18+
![PostgreSQL](https://img.shields.io/badge/PostgreSQL-16.0%2B-%23336791)
19+
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-2.0-%23778877)
1920
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
2021
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
2122
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
23+
![Docker](https://img.shields.io/badge/Docker-%232496ED?logo=docker&logoColor=white)
2224
[![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?logo=discord&logoColor=white)](https://discord.com/invite/yNN3wTbVAC)
2325
![Discord](https://img.shields.io/discord/1185035164577972344)
2426

README.zh-CN.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
**🔥持续更新维护中🔥**
1212

1313
[![GitHub](https://img.shields.io/github/license/fastapi-practices/fastapi_best_architecture)](https://github.com/fastapi-practices/fastapi_best_architecture/blob/master/LICENSE)
14-
[![Static Badge](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
15-
![Static Badge](https://img.shields.io/badge/MySQL-8.0%2B-%2300758f)
16-
![Static Badge](https://img.shields.io/badge/SQLAlchemy-2.0-%23778877)
14+
[![Python](https://img.shields.io/badge/python-3.10%2B-blue)](https://www.python.org/downloads/)
15+
![MySQL](https://img.shields.io/badge/MySQL-8.0%2B-%2300758f)
16+
![PostgreSQL](https://img.shields.io/badge/PostgreSQL-16.0%2B-%23336791)
17+
![SQLAlchemy](https://img.shields.io/badge/SQLAlchemy-2.0-%23778877)
1718
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
1819
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
1920
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
21+
![Docker](https://img.shields.io/badge/Docker-%232496ED?logo=docker&logoColor=white)
2022
[![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?logo=discord&logoColor=white)](https://discord.com/invite/yNN3wTbVAC)
2123
![Discord](https://img.shields.io/discord/1185035164577972344)
2224

backend/.env.example

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Env: dev、pro
22
ENVIRONMENT='dev'
3-
# MySQL
4-
MYSQL_HOST='127.0.0.1'
5-
MYSQL_PORT=3306
6-
MYSQL_USER='root'
7-
MYSQL_PASSWORD='123456'
3+
# Database
4+
DATABASE_TYPE='mysql'
5+
DATABASE_HOST='127.0.0.1'
6+
DATABASE_PORT=3306
7+
DATABASE_USER='root'
8+
DATABASE_PASSWORD='123456'
89
# Redis
910
REDIS_HOST='127.0.0.1'
1011
REDIS_PORT=6379

backend/alembic.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ script_location = alembic
77

88
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
99
# Uncomment the line below if you want the files to be prepended with date and time
10-
file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(hour).2d:%%(minute).2d:%%(second).2d-%%(rev)s_%%(slug)s
10+
file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(hour).2d_%%(minute).2d_%%(second).2d-%%(rev)s_%%(slug)s
1111

1212
# sys.path path, will be prepended to sys.path if present.
1313
# defaults to the current working directory.

backend/alembic/env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from backend.common.model import MappedBase
1717
from backend.core import path_conf
18-
from backend.database.db_mysql import SQLALCHEMY_DATABASE_URL
18+
from backend.database.db import SQLALCHEMY_DATABASE_URL
1919

2020
# import your new model here
2121
from backend.app.admin.model import * # noqa: F401
@@ -38,7 +38,7 @@
3838
target_metadata = MappedBase.metadata
3939

4040
# other values from the config, defined by the needs of env.py,
41-
alembic_config.set_main_option('sqlalchemy.url', SQLALCHEMY_DATABASE_URL)
41+
alembic_config.set_main_option('sqlalchemy.url', SQLALCHEMY_DATABASE_URL.render_as_string(hide_password=False))
4242

4343

4444
def run_migrations_offline():

backend/app/admin/api/v1/auth/captcha.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from backend.app.admin.conf import admin_settings
99
from backend.common.response.response_schema import ResponseModel, response_base
10-
from backend.database.db_redis import redis_client
10+
from backend.database.redis import redis_client
1111

1212
router = APIRouter()
1313

backend/app/admin/api/v1/log/login_log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from backend.common.security.jwt import DependsJwtAuth
1212
from backend.common.security.permission import RequestPermission
1313
from backend.common.security.rbac import DependsRBAC
14-
from backend.database.db_mysql import CurrentSession
14+
from backend.database.db import CurrentSession
1515

1616
router = APIRouter()
1717

backend/app/admin/api/v1/log/opera_log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from backend.common.security.jwt import DependsJwtAuth
1212
from backend.common.security.permission import RequestPermission
1313
from backend.common.security.rbac import DependsRBAC
14-
from backend.database.db_mysql import CurrentSession
14+
from backend.database.db import CurrentSession
1515

1616
router = APIRouter()
1717

backend/app/admin/api/v1/sys/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from backend.common.security.jwt import DependsJwtAuth
1212
from backend.common.security.permission import RequestPermission
1313
from backend.common.security.rbac import DependsRBAC
14-
from backend.database.db_mysql import CurrentSession
14+
from backend.database.db import CurrentSession
1515

1616
router = APIRouter()
1717

backend/app/admin/api/v1/sys/casbin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from backend.common.security.jwt import DependsJwtAuth
2222
from backend.common.security.permission import RequestPermission
2323
from backend.common.security.rbac import DependsRBAC
24-
from backend.database.db_mysql import CurrentSession
24+
from backend.database.db import CurrentSession
2525

2626
router = APIRouter()
2727

0 commit comments

Comments
 (0)